Lost new line character when using ReadFile #20729

Open
opened 2026-01-31 07:22:25 +00:00 by claunia · 0 comments
Owner

Originally created by @confusedsushi on GitHub (Oct 24, 2023).

Originally assigned to: @lhecker on GitHub.

Windows Terminal version

1.18.2822.0

Windows build number

10.0.22631.2361

Other Software

The problem was first noted in some other console application which is using ReadFile to read from the console. The problem was stripped down to a minimal program

#include <assert.h>
#include <windows.h>

int __cdecl main() {
  char buf[2];
  DWORD read;

  ReadFile(GetStdHandle(STD_INPUT_HANDLE), buf, 2, &read, NULL);
  assert(read == 2);
  assert(buf[0] == '\r');
  assert(buf[1] == '\n');

  ReadFile(GetStdHandle(STD_INPUT_HANDLE), buf, 1, &read, NULL);
  assert(read == 1);
  assert(buf[0] == '\r');

  ReadFile(GetStdHandle(STD_INPUT_HANDLE), buf, 1, &read, NULL);
  assert(read == 1);
  assert(buf[0] == '\n');
}

It seems the unexpected behavior first occurred with https://github.com/microsoft/terminal/pull/14745.

Steps to reproduce

Compile the program using your favorite C compiler and run it. Hit <ENTER> until the example exits.

Expected Behavior

In the good case you have to hit <ENTER> twice and the program exits gracefully.

Actual Behavior

In the bad case you have to hit <ENTER> three times and the last assert fires an error.

Originally created by @confusedsushi on GitHub (Oct 24, 2023). Originally assigned to: @lhecker on GitHub. ### Windows Terminal version 1.18.2822.0 ### Windows build number 10.0.22631.2361 ### Other Software The problem was first noted in some other console application which is using ``ReadFile`` to read from the console. The problem was stripped down to a minimal program ````cpp #include <assert.h> #include <windows.h> int __cdecl main() { char buf[2]; DWORD read; ReadFile(GetStdHandle(STD_INPUT_HANDLE), buf, 2, &read, NULL); assert(read == 2); assert(buf[0] == '\r'); assert(buf[1] == '\n'); ReadFile(GetStdHandle(STD_INPUT_HANDLE), buf, 1, &read, NULL); assert(read == 1); assert(buf[0] == '\r'); ReadFile(GetStdHandle(STD_INPUT_HANDLE), buf, 1, &read, NULL); assert(read == 1); assert(buf[0] == '\n'); } ```` It seems the unexpected behavior first occurred with https://github.com/microsoft/terminal/pull/14745. ### Steps to reproduce Compile the program using your favorite C compiler and run it. Hit ``<ENTER>`` until the example exits. ### Expected Behavior In the good case you have to hit ``<ENTER>`` twice and the program exits gracefully. ### Actual Behavior In the bad case you have to hit ``<ENTER>`` three times and the last assert fires an error.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#20729