Skip to content

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

Closed

Description

@YO4

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.

Activity

  1. added
    Issue-BugIt either shouldn't be doing this or needs an investigation.
    on Mar 4, 2022
  2. ghost added
    Needs-TriageIt's a new issue that the core contributor team needs to triage at the next triage meeting
    on Mar 4, 2022
  3. ghost added
    Needs-Tag-FixDoesn't match tag requirements
    on Mar 4, 2022
  4. added this to the 22H2 milestone on Mar 7, 2022
  5. ghost removed
    Needs-Tag-FixDoesn't match tag requirements
    on Mar 7, 2022
  6. removed
    Needs-TriageIt's a new issue that the core contributor team needs to triage at the next triage meeting
    on Apr 8, 2022
  7. lhecker commented on Jul 27, 2023

    @lhecker
    Member

    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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-InputRelated to input processing (key presses, mouse, etc.)Issue-BugIt either shouldn't be doing this or needs an investigation.Needs-Tag-FixDoesn't match tag requirementsPriority-2A description (P2)Product-ConhostFor issues in the Console codebase

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions