Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
YO4 opened this issue Mar 4, 2022 · 1 comment
Closed
Labels
Area-Input Related to input processing (key presses, mouse, etc.) Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Tag-Fix Doesn't match tag requirements Priority-2 A description (P2) Product-Conhost For issues in the Console codebase
Milestone

Comments

@YO4
Copy link

YO4 commented 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.

@YO4 YO4 added the Issue-Bug It either shouldn't be doing this or needs an investigation. label Mar 4, 2022
@ghost ghost added Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Needs-Tag-Fix Doesn't match tag requirements labels Mar 4, 2022
@zadjii-msft zadjii-msft added the Product-Conhost For issues in the Console codebase label Mar 7, 2022
@zadjii-msft zadjii-msft added this to the 22H2 milestone Mar 7, 2022
@zadjii-msft zadjii-msft added Area-Input Related to input processing (key presses, mouse, etc.) Priority-2 A description (P2) labels Mar 7, 2022
@ghost ghost removed the Needs-Tag-Fix Doesn't match tag requirements label Mar 7, 2022
@DHowett DHowett removed the Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting label Apr 8, 2022
@zadjii-msft zadjii-msft modified the milestones: 22H2, Backlog Jul 5, 2023
@lhecker
Copy link
Member

lhecker commented 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 lhecker closed this as completed Jul 27, 2023
@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs-Tag-Fix Doesn't match tag requirements label Jul 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Input Related to input processing (key presses, mouse, etc.) Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Tag-Fix Doesn't match tag requirements Priority-2 A description (P2) Product-Conhost For issues in the Console codebase
Projects
None yet
Development

No branches or pull requests

4 participants