ReadConsoleA use with PeekConsoleInput returns a string with leading garbage #16927

Closed
opened 2026-01-31 05:27:28 +00:00 by claunia · 1 comment
Owner

Originally created by @YO4 on GitHub (Mar 4, 2022).

Windows Terminal version

1.11.3471.0 / 1.13.10395.0

Windows build number

10.0.19043.1526

Other Software

ruby.exe seems to be affected this issue.
https://bugs.ruby-lang.org/issues/18588

command prompt window also wrong.
Legacy Console mode enabled command prompt works fine.

Steps to reproduce

  1. use PeekConsoleInputA(h, &ir, 1, &n) like kbhit()
  2. Input MultiByteChar from console(type or paste)
  3. ReadFile(or ReadConsoleA)
  4. Got wrong string

test program(test.cpp)

#include <windows.h>
#include <stdio.h>

/* from ruby/win32/win32.c */
static int
is_readable_console(HANDLE h)
{
    int ret = 0;
    DWORD n = 0;
    INPUT_RECORD ir;

    if (PeekConsoleInputA(h, &ir, 1, &n) && n > 0) {
        if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown &&
            ir.Event.KeyEvent.uChar.AsciiChar) {
            ret = 1;
        }
        else {
           ReadConsoleInputA(h, &ir, 1, &n);
        }
    }
    return ret;
}

int main()
{
    HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
    unsigned char s[64];
    DWORD n;

    while (1) {
        if (is_readable_console(hInput)) break;
        Sleep(100);
    }
    //ReadFile(hInput, s, 16, &n, NULL);
    ReadConsoleA(hInput, s, 16, &n, NULL);
    for (unsigned int i = 0; i < n; i++) printf("<%02x>", s[i]);
    return(0);
}

Expected Behavior

run test.exe and input text, hit enter. hexadecimal codes are printed.

C:\src\vs\test\x64\Debug>chcp 932
現在のコード ページ: 932
C:\src\vs\test\x64\Debug>test.exe
a
<61><0d><0a>
C:\src\vs\readfile\x64\Debug>test.exe
あ
<82><a0><0d>

Actual Behavior

C:\src\vs\test\x64\Debug>chcp 932
現在のコード ページ: 932
C:\src\vs\test\x64\Debug>test.exe
a
<61><0d><0a>
C:\src\vs\readfile\x64\Debug>test.exe
あ
<a0><ff><82><a0><0d>

<a0><ff> is garbage.

Originally created by @YO4 on GitHub (Mar 4, 2022). ### Windows Terminal version 1.11.3471.0 / 1.13.10395.0 ### Windows build number 10.0.19043.1526 ### Other Software ruby.exe seems to be affected this issue. https://bugs.ruby-lang.org/issues/18588 command prompt window also wrong. `Legacy Console mode` enabled command prompt works fine. ### Steps to reproduce 1. use PeekConsoleInputA(h, &ir, 1, &n) like kbhit() 2. Input MultiByteChar from console(type or paste) 3. ReadFile(or ReadConsoleA) 4. Got wrong string test program(test.cpp) ``` #include <windows.h> #include <stdio.h> /* from ruby/win32/win32.c */ static int is_readable_console(HANDLE h) { int ret = 0; DWORD n = 0; INPUT_RECORD ir; if (PeekConsoleInputA(h, &ir, 1, &n) && n > 0) { if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown && ir.Event.KeyEvent.uChar.AsciiChar) { ret = 1; } else { ReadConsoleInputA(h, &ir, 1, &n); } } return ret; } int main() { HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE); unsigned char s[64]; DWORD n; while (1) { if (is_readable_console(hInput)) break; Sleep(100); } //ReadFile(hInput, s, 16, &n, NULL); ReadConsoleA(hInput, s, 16, &n, NULL); for (unsigned int i = 0; i < n; i++) printf("<%02x>", s[i]); return(0); } ``` ### Expected Behavior run test.exe and input text, hit enter. hexadecimal codes are printed. ``` C:\src\vs\test\x64\Debug>chcp 932 現在のコード ページ: 932 C:\src\vs\test\x64\Debug>test.exe a <61><0d><0a> C:\src\vs\readfile\x64\Debug>test.exe あ <82><a0><0d> ``` ### Actual Behavior ``` C:\src\vs\test\x64\Debug>chcp 932 現在のコード ページ: 932 C:\src\vs\test\x64\Debug>test.exe a <61><0d><0a> C:\src\vs\readfile\x64\Debug>test.exe あ <a0><ff><82><a0><0d> ``` `<a0><ff>` is garbage.
claunia added the Product-ConhostIssue-BugArea-InputNeeds-Tag-FixPriority-2 labels 2026-01-31 05:27:28 +00:00
Author
Owner

@lhecker commented on GitHub (Jul 27, 2023):

Thanks for pointing this issue out @SainoNamkho! It indeed seems to have been fixed in PR #14745. 🙂


@YO4 BTW I believe your "expected behavior" is not quite correct. This seems better to me:

a
<61><0d><0a>
あ
<82><a0><0d><0a>
            ^^^^

The output should always be terminated with \r\n and not just \r. #14745 handles this correctly and terminates it properly with \r\n = <0d><0a>. 👍

@lhecker commented on GitHub (Jul 27, 2023): Thanks for pointing this issue out @SainoNamkho! It indeed seems to have been fixed in PR #14745. 🙂 --- @YO4 BTW I believe your "expected behavior" is not quite correct. This seems better to me: ``` a <61><0d><0a> あ <82><a0><0d><0a> ^^^^ ``` The output should always be terminated with `\r\n` and not just `\r`. #14745 handles this correctly and terminates it properly with `\r\n` = `<0d><0a>`. 👍
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#16927