Skip to content

fix(stdin): read fd 0 in binary mode across the remaining text-mode applets (#1202, #1212, #1211) - #1218

Merged
caomengxuan666 merged 5 commits into
mainfrom
fix/stdin-binary-sweep
Oct 17, 2026
Merged

caomengxuan666 merged 5 commits into
mainfrom
fix/stdin-binary-sweep

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Sweep of the stdin read side for the #1195/#1200 CRT text-mode defect: fd 0 defaults to text mode, so a pipe or < redirect stops at the first 0x1A (Ctrl-Z) and collapses CRLF to LF, while the same bytes via a file operand are correct. This PR fixes every remaining applet found on that path.

One shared switch, file_io::set_stdin_binary(), is called on each tool's stdin route — the same per-tool _setmode(_fileno(stdin), _O_BINARY) idiom that cat/wc/tr and the checksum family already use. File-operand paths already open binary, so no output-side change is needed here (the sort-class trailing-\r trim some line readers do is a separate, deliberate choice tracked by #1202).

Closes #1212 and #1211. Follows up #1202.

Commits (one per cluster)

  1. fix(file_io): add set_stdin_binary helper
  2. fix(stdin): ... line/record applets (#1202, #1212) — head, tail, sort, uniq, sed, grep, hexdump, xxd, fold, expand, unexpand, nl, paste, fmt, pr, col, strings, shuf, column, ptx
  3. fix(stdin): ... record/structural applets (#1212) — comm, join, cmp, csplit, split, tsort, numfmt, du, factor, patch, less, more
  4. fix(stdin): ... line-ending converters (#1211) — d2u, dos2unix, u2d, unix2dos

Before → after (pipe form, hex; GNU coreutils 9.4 reference)

cmd payload before after GNU
head abc\x1adef\n 61 62 63 61 62 63 1a 64 65 66 0a same
sort abc\x1adef\n 61 62 63 0a 61 62 63 1a 64 65 66 0a same
grep -a B AAAA\x1aBBBB\n (no match) AAAA 1a BBBB 0a same
hexdump -C -v abc\x1adef\n 3 bytes 8 bytes … 1a 64 65 66 0a same
xxd abc\x1adef\n 3 bytes … 631a 6465 660a same
col AAAA\x1aBBBB\n AAAA AAAABBBB same
more / less abc\x1adef\n 61 62 63 61 62 63 1a 64 65 66 0a same
cmp - f AAAA\x1aBBBB\n vs same file exit 1, EOF on - which is empty exit 0, no output same
csplit - /line2/ line1\x1aline2\nline3\n match not found, exit 1 exit 0 exit 0
split -l 1 - same 1 part 2 parts 2 parts
factor 12 18\x1a20 30\n factors 12, 18 factors 12, 30 same
numfmt 1000\n2000\n\x1a3000\n exit 0, 2 lines exit 2, invalid number exit 2
d2u/dos2unix a\r\nb\r\n\x1ac\r\n a\nb\n a\nb\n\x1ac\n (no GNU)
u2d/unix2dos a\nb\n\x1ac\n a\r\nb\r\n a\r\nb\r\n\x1ac\r\n (no GNU)

Already correct and left alone: dd (raw handle), tac, cat, wc, tr, od, base64, cut, rev.

Tests

ctest -R "^(head|tail|…|u2d)\." over the 36 touched suites: 857/857 passed. 35 new *_issue1212 / *_issue1211 tests (all fail before, pass after). du --files0-from is fixed but has no dedicated test — an NTFS filename cannot carry 0x1A, and the sweep covers it.

Adjacent (handed to the write-side sweep, not fixed here)

fd 0 defaults to the CRT's text mode, where reads stop at the first 0x1A
(Ctrl-Z) byte and collapse CRLF to LF. Unix filters consume raw bytes, so
expose a single switch that the applets call before their stdin read,
mirroring the per-tool _setmode idiom cat/wc/tr and the checksum family
(#1195) already use.

Refs #1202, #1212
#1212)

head, tail, sort, uniq, sed, grep, hexdump, xxd, fold, expand, unexpand,
nl, paste, fmt, pr, col (the sixteen from #1202) plus strings, shuf,
column and ptx read fd 0 with the CRT's default text mode, so a pipe or
'<' redirect stopped at the first 0x1A and collapsed CRLF while the same
bytes via a file operand were correct.

Call file_io::set_stdin_binary() on each stdin path (the file-operand
paths already open binary). Regression tests mirror *_issue1212 in each
tool's unit dir and assert the pipe form matches the operand form.

Refs #1202, #1212
…#1212)

comm, join, cmp, csplit, split, tsort, numfmt, du, factor, patch, less
and more read stdin as data but took the CRT text stream: cmp compared an
empty stdin, csplit/split never saw data past 0x1A, tsort/numfmt/factor
stopped early, and more/less hid the rest. Switch fd 0 to binary on each
stdin path; the file-operand forms were already byte-correct.

Regression tests mirror *_issue1212. du --files0-from has no dedicated
test (its list can only carry 0x1A in a byte no NTFS filename allows) but
is fixed and covered by the sweep.

Refs #1212
…1211)

d2u, dos2unix, u2d and unix2dos read fd 0 through the CRT, so a piped
payload lost every line after the first 0x1A. Read binary and convert
explicitly; d2u/dos2unix/unix2dos already handled CR correctly, while
u2d now strips a trailing CR per record before re-adding CRLF so an
already-CRLF input cannot double it.

Regression tests mirror *_issue1211.

Refs #1211
The NUL-separated list is data: before the fix a 0x1A after the first entry
silently ended the list (exit 0); now the following entry is read and its
access failure is reported.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant