Repository navigation
fix(#586): -s stdin-script entry becomes the canonical on-demand library reader - #593
Merged
Merged
Conversation
The -s stdin-script entry must read commands on demand - never pre-buffer the stream. GNU's contract (vendored bash 5.3): the parser's buffered stream over a non-seekable stdin has capacity one (input.c:381-397 fd_to_buffered_stream, 'size = fd_is_seekable(fd) ? min(st_size, MAX_INPUT_BUFFER_SIZE) : 1', flagged B_UNBUFF at input.c:192-193), and a seekable script stream is rewound to the first unconsumed byte before every fork (input.c:552-565 sync_buffered_stream, from jobs.c:2298-2302 make_child and input.c:296-316 check_bash_input). A consumer that reads fd 0 - a $(exec cat)/$(cat) capture, a bare read, a top-level exec cat - therefore sees exactly the bytes after the last parser-submitted command, consumes them as data, and those bytes never fall back to code. rubash#583 fixed the child-stdin channel for the -c entry; the -s contract lived only inside the binary's main.rs, so embedding hosts could not run it and a host -s route that slurps stdin up front (read_to_string/BufReader) turns every later line the consumers never saw back into code - $(exec cat) captures empty and the unconsumed data lines execute as commands (the 'curl | bash -s' install-script hazard the issue pins). Move the incremental driver verbatim into script_driver::run_stdin_script (byte-wise read_unbuffered_line off the raw OS handle, the OS file position as the single shared cursor) and delegate the binary's -s route to it. tests/issue586_stdin_script_on_demand_regressions.rs pins the matrix: comsub exec cat / plain cat / bare read / top-level exec cat, code resuming after a consumed point, and the seekable-file shape - every expected output byte-verified against WSL GNU bash 5.3.0.
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.
Closes #586.
Root cause
The
-sstdin-script contract in GNU bash: the parser reads fd 0 on demand, one complete command at a time, and never pre-buffers the stream.third_party/bash/input.c:381-397fd_to_buffered_stream,size = fd_is_seekable(fd) ? min(st_size, MAX_INPUT_BUFFER_SIZE) : 1, flaggedB_UNBUFFatinput.c:192-193), so a piped-sscript has no read-ahead at all; a seekable script stream may buffer up to 8192 bytes but is rewound to the first unconsumed byte before every fork (input.c:552-565sync_buffered_stream, invoked fromjobs.c:2298-2302make_child andinput.c:296-316check_bash_input).$(exec cat)/$(cat)capture, a bareread, a top-levelexec cat— observes the stream from the parser's exact position, consumes what it wants as data, and those bytes never fall back to code.rubash#583 fixed the exec child-stdin channel for the
-centry, but the-sdriver lived only inside the binary'ssrc/main.rs, unreachable from the library. A host-sroute that slurps stdin up front (read_to_string/ any BufReader) reproduces the issue exactly: the consumers hit EOF (v=[]), and every later line the consumers never saw is re-armed as code — MARKER lines execute as commands, rc 127. Verified on the installed product, which routes-sthroughstd::io::stdin().read_to_string()(niubash/src/main.rs:612-614); the niubash follow-up is to run the engine driver exposed by this PR instead of slurping.Fix
src/main.rsinto the library asrubash::script_driver::run_stdin_script(src/script_driver.rs), with the GNU anchors documented at the definition. It reads one byte per syscall off the raw OS handle (read_unbuffered_line), leaving the OS file position as the single shared cursor — child processes inherit the descriptor and continue after the last parser-consumed newline, byte-for-byte the input.c/zread model.src/main.rsdelegates to it (both-sandbash < fileentries).tests/issue586_stdin_script_on_demand_regressions.rspins the matrix, each expectation byte-verified against WSL GNU bash 5.3.0(1):$(exec cat)eats the remainder as data (the issue's own repro →one, rc 0, no MARKER diagnostics)exec catcats the remaining lines as data (echo neverprinted by cat, never executed)catchild and$(cat)see the same on-demand remainderreadeats exactly the next line, and code resumes after the consumed point (TAILstill executes → rc 127)read+eval "$(exec cat)") → rc 127 with the remainder delivered to evalVerification
cargo check— cleanRUSTFLAGS="-D warnings" cargo check— cleancargo fmt --check— cleanRUST_TEST_THREADS=1 cargo test --no-fail-fast: branch 4856 passed / 42 failed vs origin/master baseline 4849 passed / 42 failed (the +7 are the new tests); failure sets identical, bidirectionalcommempty (the 42 are the known local-environment failures).