Repository navigation
fix(cat): deliver VEOF (^D) on the cooked console face - #1454
Merged
Merged
Conversation
Root cause: cat's live-stdin arms read the console handle with plain ReadFile (src/commands/cat.cpp fast-passthrough and transform loops). Windows cooked mode (ENABLE_LINE_INPUT) has no line discipline, so the 0x04 keystroke enters the console line buffer as a plain character, ReadFile returns only on Enter, and an interactive cat never observes EOF - it swallows every line typed afterwards until ^C (exit 0xC000013A). Fix: shared input-layer helper file_io::read_stdin_record() (the file_io stdin layer next to stdin_is_bad/set_stdin_binary), armed only when stdin is a console handle with ENABLE_LINE_INPUT, filtering the records ReadFile delivers: - 0x04 at line start (empty canonical buffer) -> zero-byte read (EOF); the unread tail of that console record is re-injected into the console input queue via WriteConsoleInput so the next reader - the shell - receives it, the way the Unix tty queue does; - 0x04 mid-line -> the pending prefix is delivered as its own record (no newline), the record continues; - every other byte passes through, so pipe/file input (where the helper is not armed) and non-^D console bytes stay byte-identical. cat's two live-stdin ReadFile sites now call the helper; the ReadFile contract (TRUE + got==0 at EOF) is preserved. GNU anchor: termios VEOF line discipline - ^D with an empty canonical buffer ends the stream; ^D after pending characters flushes them to the reader without a newline (verified against WSL GNU bash 5.3.20 under the same ConPTY shape: cat + hello + ^D -> cat exits 0). Known limits: mid-line flush bytes are handed over only when the next Enter completes the console record (earlier release would need raw-mode discipline emulation); std::cin-based whole-input readers and other applet-private loops (head, dd) still need to adopt the helper; if WriteConsoleInput fails the EOF tail is dropped. ConPTY evidence (niubash session, matched baseline/fixed builds): baseline keeps running after ^D, swallows 'second', dies only at ^C (cat-rc=-1073741510); fixed prints 'hello' and exits 0 (cat-rc=0), and the shell observes 'second: command not found' via the queue pushback. Fixes #1450
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.
Problem
On a Windows cooked console (real ConPTY interactive face),
catnever ends on ^D (VEOF): the 0x04 keystroke enters the console line buffer as a plain character,ReadFilereturns only on Enter, and an interactivecatswallows every subsequently typed line until ^C kills it (exit0xC000013A=-1073741510).Under GNU (termios line discipline), ^D with an empty canonical buffer ends the input stream, and ^D after pending characters flushes them to the reader without a newline. Windows cooked mode has no line discipline, so the behavior is reimplemented at the WinuxCmd input layer — the disposition already recorded in #1450.
Root cause
cat's two live-stdin arms read the console handle with plainReadFile:src/commands/cat.cppfast-passthrough loop (hIn)src/commands/cat.cpptransform loop (hLiveIn)Fix
Shared input-layer helper
file_io::read_stdin_record()(insrc/utils/file_io.cppm, next to the other stdin primitivesstdin_is_bad/set_stdin_binary), armed only when stdin is a console handle withENABLE_LINE_INPUT; pipe/file input falls through to plainReadFileunchanged. On the armed face it filters the records the console delivers:WriteConsoleInputso the next reader — the shell — receives it, the way the Unix tty queue retains input typed after EOF;\r\n), so non-^D bytes are byte-identical to the old path.cat's two live-stdinReadFilesites now call the helper; theReadFilecontract (TRUE+got == 0at EOF) is preserved, so the surrounding loops are unchanged apart from the call.GNU anchor
termios VEOF semantics, verified live against WSL GNU bash 5.3.20 under the same ConPTY relay shape:
cat+hello+ ^D →catexits 0, prompt returns (gnu-cat-rc=0).Evidence (ConPTY battery, niubash session, matched baseline/fixed builds from this same tree)
cat+hello+ ^D +second+ ^Csecond; dies only at ^C,cat-rc=-1073741510catends at ^D; shell reportsniu: second: command not found(queue pushback), GNU-paritycat+hello+ ^D →echo cat-rc=$?echo/exitforevercat-rc=0abc+ ^D +def+ Enter + ^D →catof captured fileabcdef(prefix flushed, remainder continues — GNU read-sequence semantics)cat.*cases)nul_txt_reads_file_issue1333) reproduces identically on the pristine-main baseline binary (pre-existing, environment-bound)Fixes #1450