interaction of _read on fd 0 in terminal #20802

Closed
opened 2026-01-31 07:24:28 +00:00 by claunia · 5 comments
Owner

Originally created by @spk121 on GitHub (Nov 12, 2023).

Originally assigned to: @lhecker on GitHub.

Windows Terminal version

1.19.2682.0

Windows build number

Version 10.0.22621.2428

Other Software

No response

Steps to reproduce

Compile the attached, run, and press enter multiple times. Behavior is the same with MSVC in Visual studio 2022 and with latest MinGW with GCC using 64-bit UCRT.

#include <io.h>
#include <stdio.h>
#include <errno.h>


int main()
{
  int fd = 0;
  printf("fd = %d\n", fd);

  int ret;
  char buf[128];

  while (1)
    {
      errno = 0;
      ret = _read (fd, buf, 1);
      printf("ret = %d, errno = %d, char = %u\n", ret, errno, buf[0]);
    }
  return 0;
}

Expected Behavior

I expected the behavior found on MinGW's mintty, when I press the enter key multiple times.

fd = 0

ret = 1, errno = 0, char = 10

ret = 1, errno = 0, char = 10

ret = 1, errno = 0, char = 10

ret = 1, errno = 0, char = 10

Actual Behavior

This is what happens in Terminal when I press the enter key multiple times on a recent Terminal

fd = 0


ret = 1, errno = 0, char = 13
ret = -1, errno = 13, char = 13


ret = 1, errno = 0, char = 13
ret = -1, errno = 13, char = 13


ret = 1, errno = 0, char = 13
ret = -1, errno = 13, char = 13


ret = 1, errno = 0, char = 13
ret = -1, errno = 13, char = 13
Originally created by @spk121 on GitHub (Nov 12, 2023). Originally assigned to: @lhecker on GitHub. ### Windows Terminal version 1.19.2682.0 ### Windows build number Version 10.0.22621.2428 ### Other Software _No response_ ### Steps to reproduce Compile the attached, run, and press enter multiple times. Behavior is the same with MSVC in Visual studio 2022 and with latest MinGW with GCC using 64-bit UCRT. ``` #include <io.h> #include <stdio.h> #include <errno.h> int main() { int fd = 0; printf("fd = %d\n", fd); int ret; char buf[128]; while (1) { errno = 0; ret = _read (fd, buf, 1); printf("ret = %d, errno = %d, char = %u\n", ret, errno, buf[0]); } return 0; } ``` ### Expected Behavior I expected the behavior found on MinGW's mintty, when I press the enter key multiple times. ``` fd = 0 ret = 1, errno = 0, char = 10 ret = 1, errno = 0, char = 10 ret = 1, errno = 0, char = 10 ret = 1, errno = 0, char = 10 ``` ### Actual Behavior This is what happens in Terminal when I press the enter key multiple times on a recent Terminal ``` fd = 0 ret = 1, errno = 0, char = 13 ret = -1, errno = 13, char = 13 ret = 1, errno = 0, char = 13 ret = -1, errno = 13, char = 13 ret = 1, errno = 0, char = 13 ret = -1, errno = 13, char = 13 ret = 1, errno = 0, char = 13 ret = -1, errno = 13, char = 13 ```
claunia added the Issue-BugIn-PRArea-InputNeeds-Tag-FixArea-Server labels 2026-01-31 07:24:28 +00:00
Author
Owner

@spk121 commented on GitHub (Nov 12, 2023):

Apparently something having to do with handling of CR LF line endings in terminal, but, that _read errors with EACCES is unexpected.

@spk121 commented on GitHub (Nov 12, 2023): Apparently something having to do with handling of CR LF line endings in terminal, but, that `_read` errors with `EACCES` is unexpected.
Author
Owner

@zadjii-msft commented on GitHub (Nov 13, 2023):

I'm gonna reckon that this maybe regressed due to #15783. I'll tag @lhecker to investigate. Thanks for the minimal repro!

@zadjii-msft commented on GitHub (Nov 13, 2023): I'm gonna reckon that this maybe regressed due to #15783. I'll tag @lhecker to investigate. Thanks for the minimal repro!
Author
Owner

@zadjii-msft commented on GitHub (Nov 13, 2023):

maybe #15662?

@zadjii-msft commented on GitHub (Nov 13, 2023): maybe #15662?
Author
Owner

@spk121 commented on GitHub (Nov 14, 2023):

I noticed that if you change the _read call to _read(fd, buf, 2) it works fine and always returns 1 character of ASCII 10.. It is like the _read needs a two character buffer to hold (i presume) "\r\n" before some sort of cooked logic turns it into "\n" before storing it into buf.

@spk121 commented on GitHub (Nov 14, 2023): I noticed that if you change the `_read` call to `_read(fd, buf, 2)` it works fine and always returns 1 character of ASCII 10.. It is like the `_read` needs a two character buffer to hold (i presume) "\r\n" before some sort of cooked logic turns it into "\n" before storing it into `buf`.
Author
Owner

@lhecker commented on GitHub (Nov 15, 2023):

I believe this is a duplicate of #16223. I haven't checked how _read() is implemented but I bet it's a wrapper around ReadFile()...

@lhecker commented on GitHub (Nov 15, 2023): I believe this is a duplicate of #16223. I haven't checked how `_read()` is implemented but I bet it's a wrapper around `ReadFile()`...
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#20802