Repository navigation
fix(stdin): read fd 0 in binary mode across the remaining text-mode applets (#1202, #1212, #1211) - #1218
Merged
Conversation
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.
This was referenced Oct 17, 2026
This was referenced Oct 17, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 first0x1A(Ctrl-Z) and collapsesCRLFtoLF, 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 thatcat/wc/trand the checksum family already use. File-operand paths already open binary, so no output-side change is needed here (thesort-class trailing-\rtrim some line readers do is a separate, deliberate choice tracked by #1202).Closes #1212 and #1211. Follows up #1202.
Commits (one per cluster)
fix(file_io): add set_stdin_binary helperfix(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, ptxfix(stdin): ... record/structural applets (#1212)— comm, join, cmp, csplit, split, tsort, numfmt, du, factor, patch, less, morefix(stdin): ... line-ending converters (#1211)— d2u, dos2unix, u2d, unix2dosBefore → after (pipe form, hex; GNU coreutils 9.4 reference)
headabc\x1adef\n61 62 6361 62 63 1a 64 65 66 0asortabc\x1adef\n61 62 63 0a61 62 63 1a 64 65 66 0agrep -a BAAAA\x1aBBBB\nAAAA 1a BBBB 0ahexdump -C -vabc\x1adef\n… 1a 64 65 66 0axxdabc\x1adef\n… 631a 6465 660acolAAAA\x1aBBBB\nAAAAAAAABBBBmore/lessabc\x1adef\n61 62 6361 62 63 1a 64 65 66 0acmp - fAAAA\x1aBBBB\nvs same fileEOF on - which is emptycsplit - /line2/line1\x1aline2\nline3\nmatch not found, exit 1split -l 1 -factor12 18\x1a20 30\nnumfmt1000\n2000\n\x1a3000\ninvalid numberd2u/dos2unixa\r\nb\r\n\x1ac\r\na\nb\na\nb\n\x1ac\nu2d/unix2dosa\nb\n\x1ac\na\r\nb\r\na\r\nb\r\n\x1ac\r\nAlready 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/*_issue1211tests (all fail before, pass after).du --files0-fromis fixed but has no dedicated test — an NTFS filename cannot carry0x1A, and the sweep covers it.Adjacent (handed to the write-side sweep, not fixed here)
sort/uniq/head/tailnow keep\ron stdin, matching GNU and their own operand form.\r(cut,grep,fold,expand,unexpand,paste,fmt,pr,column,ptx) still do so for stdin and operands alike; that is a line-ending wording choice, tracked by the 0x1A stdin truncation is not checksum-specific: 16 line/file applets read stdin in text mode; head gives three different answers #1202.xxd -pstill wraps at 32 hex chars (GNU 60) and the checksum*marker — already filed as xxd -p wraps at 32 hex chars/line (GNU 60); checksum output prints a false binary marker #1205.