Skip to content

fix(#586): -s stdin-script entry becomes the canonical on-demand library reader - #593

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/586-stdin-script-buffering
Oct 13, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/586-stdin-script-buffering

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #586.

Root cause

The -s stdin-script contract in GNU bash: the parser reads fd 0 on demand, one complete command at a time, and never pre-buffers the stream.

  • Vendored bash 5.3 anchors: a non-seekable stdin gets a capacity-one buffered stream (third_party/bash/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), so a piped -s script 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-565 sync_buffered_stream, invoked from jobs.c:2298-2302 make_child and input.c:296-316 check_bash_input).
  • Therefore a consumer that reads fd 0 — a $(exec cat)/$(cat) capture, a bare read, a top-level exec 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 -c entry, but the -s driver lived only inside the binary's src/main.rs, unreachable from the library. A host -s route 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 -s through std::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

  • Move the incremental stdin-script driver verbatim from src/main.rs into the library as rubash::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.rs delegates to it (both -s and bash < file entries).
  • New tests/issue586_stdin_script_on_demand_regressions.rs pins 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)
    • top-level exec cat cats the remaining lines as data (echo never printed by cat, never executed)
    • plain cat child and $(cat) see the same on-demand remainder
    • bare read eats exactly the next line, and code resumes after the consumed point (TAIL still executes → rc 127)
    • modernish stdin-script shape (read + eval "$(exec cat)") → rc 127 with the remainder delivered to eval
    • seekable (regular-file) stdin shape

Verification

  • cargo check — clean
  • RUSTFLAGS="-D warnings" cargo check — clean
  • cargo fmt --check — clean
  • Full RUST_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, bidirectional comm empty (the 42 are the known local-environment failures).
  • GNU oracle side-by-side (pipe and file stdin) re-run on the built engine for heredoc + consumer shapes — byte-identical.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[cli][P1] -s stdin-script pre-buffers the whole stream: $(exec cat) hits EOF and unconsumed data lines execute as code (#583 -c/-s gap)

1 participant