Skip to content

fix(read): resolve /dev/stdin + EOF-without-delimiter in the read builtin (#700, #701) - #715

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/700-read-dev-stdin
Oct 17, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/700-read-dev-stdin

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes two read-builtin divergences found by a differential audit against GNU bash. One PR, one commit per issue (the two are not cleanly separable: #701 R3.1 shares the #700 redirect fix).

  • fix(read): resolve /dev/stdin instead of blocking on it (#700)
  • fix(read): report EOF without a delimiter on the seekable read path (#701)

Fixes #700
Fixes #701

Issue #700 — read < /dev/stdin hung forever

read resolved /dev/stdin through shell_path_to_windows, which maps the name to the CONIN$ console device on Windows; the builtin then blocked on the console instead of the piped fd 0. GNU redir.c opens the /dev/stdin alias device file, which resolves to a dup of fd N, so the builtin reads that descriptor (read.def:294). The operand is now resolved to its descriptor via the existing redirect_target_fd_and_move helper and served through the same fd-table/dup machinery as <&N. This also fixes /dev/fd/N and /proc/self/fd/N (issue #701 R3.1), which previously opened a nonexistent filesystem path and read empty.

Issue #701 R3.2 — rc=0 at EOF without a trailing newline

FdTable::read_file_bytes_buffered (the seekable-file fast path, read.def:737 zreadc + :940 zsyncfd) served the trailing partial record but never set the fd-table eof flag, so a file bound to fd 0 by a compound command or an inherited descriptor reported rc=0 at EOF. The simple-command < f form is served on a different path that already set the flag, hence only the compound/fd shapes diverged and while read ran one extra iteration. The buffered reader now tracks delimiter_found / limit_reached and sets read_eof_no_delimiter with the same rule as the byte-wise (:553) and in-memory (:454) paths.

Reproduction before / after

Reference: WSL GNU bash 5.3.0 (the bash on PATH is the Niubash shim). niu baseline was 1.4.0 / e785c3c8f610; current origin/master (8f596779) still reproduced every case (no commit since the report touched these paths).

Case niu before this PR GNU bash
printf 'SIN\n' | niu -c 'read -r z < /dev/stdin; echo got=[$z]' hang (rc 124) got=[SIN] got=[SIN]
{ read -r z < /dev/stdin; echo got=[$z]; } < somefile hang got=[SIN] got=[SIN]
exec 6< f; read -r z < /dev/fd/6; echo "[$z]" [] [DATA] [DATA]
printf 'SIN\n' | niu -c 'read -r z < /dev/fd/0; echo "[$z]"' [] [SIN] [SIN]
printf 'abc' > f; { IFS= read -r l; echo rc=$?; } < f rc=0 rc=1 rc=1
printf 'abc' > f; exec 9< f; IFS= read -r l <&9; echo $? rc=0 rc=1 rc=1
while IFS= read -r l; do n=$((n+1)); done < f (abc, no newline) n=1 n=0 n=0
while IFS= read -r l; do n=$((n+1)); done < f (aaa\nbbb) n=2 n=1 n=1

Guard: the simple-command form IFS= read -r l < f (already correct) still reports rc=1; a newline-terminated file still reports rc=0.

Tests

New:

  • tests/issue700_read_dev_stdin.rs — /dev/stdin with piped stdin and with file stdin, /dev/fd/N and /proc/self/fd/N for an exec 6< f descriptor, /dev/fd/0 from a pipe. Every case runs under a 15 s wall-clock guard, so a regression of the hang fails the test instead of blocking CI.
  • tests/issue701_read_eof_no_delimiter.rs — both R3.2 shapes (compound and inherited fd), the while read count for terminated and unterminated trailing records, plus newline-terminated and simple-command guards.

Both files fail on the pre-fix tree (2 hangs + 3 wrong values for #700; 4 failures for #701) and pass after.

Ran (focused, as requested):

  • cargo test --test issue700_read_dev_stdin --test issue701_read_eof_no_delimiter — 11 passed.
  • cargo test for issue275, issue276, issue277, issue279, issue280, issue324_mapfile_redirect, issue429_pipeline_stdin_redirect, issue647_function_redirect_stdin — all passed except issue647_function_redirect_stdin_regressions::function_call_file_redirect_survives_external_spawn, which fails identically on clean origin/master in this environment (external true resolved from the installed Niubash winuxcmd path is mangled to C:Userscmx... → command not found). Pre-existing/environmental, untouched.
  • cargo test --lib fd_table:: (4) and cargo test --lib windows_impl:: (8) — passed.
  • cargo fmt --check — clean.

Left alone / notes

  • read < /dev/fd/N with a closed descriptor returns rc=1 silently; GNU reports N: No such file or directory. Pre-existing, outside the reported cases.
  • read_input_is_regular_file / redirected_input_fd still recognize only the &N spelling for their checks (timeout arming, Bad-fd diagnostics); behavior for the reported cases is unaffected.
  • Change is local to src/executor/read_io.rs (redirect resolution) and the record reader in src/executor/fd_table.rs; the general redirection engine is untouched.

The read builtin resolved a `/dev/stdin` input redirect through
shell_path_to_windows, which maps the name to the CONIN$ console device
on Windows. The builtin then blocked forever on the console instead of
reading the piped fd 0, and the same path made `/dev/fd/N` open a
nonexistent filesystem path and read empty (niubash#701 R3.1).

GNU redir.c opens the `/dev/stdin` `/dev/fd/N` and `/proc/self/fd/N`
alias device files, which the kernel resolves to a dup of fd N
(redir.c:1153), so the builtin reads that descriptor (read.def:294).
Resolve the redirect operand to its descriptor with the existing
redirect_target_fd_and_move helper and serve it through the same
fd-table/dup machinery as `<&N`. `<&N` itself is unchanged: the manual
`&` parse is replaced by the same helper.

Reference: WSL GNU bash 5.3.0. Regression cover in
tests/issue700_read_dev_stdin.rs (piped stdin, file stdin, `/dev/fd/N`
and `/proc/self/fd/N` for an `exec 6< f` descriptor), with a wall-clock
guard so the historical hang fails the test instead of blocking CI.
…701)

GNU read.def sets eof and returns EXECUTION_FAILURE when the input is
exhausted before the delimiter (read.def:762-768, :949). The
seekable-file fast path in FdTable::read_file_bytes_buffered
(read.def:737 zreadc + :940 zsyncfd) served the trailing partial record
but never set the fd-table eof flag, so a file bound to fd 0 by a
compound command (`{ read l; } < f`) or an inherited descriptor
(`read l <&9`) reported rc=0 at EOF-without-newline. The simple-command
`< f` form is served on the read_shell_input_file path, which already
set the flag; that is why only the compound/fd shapes diverged. The
classic `while read` loop therefore ran one extra iteration.

Record delimiter_found / limit_reached in the buffered reader and set
read_eof_no_delimiter with the same rule the byte-wise (:553) and
in-memory (:454) paths use. The `-n`/`-N` limit break is not EOF, and a
served record is still required before the flag is set.

Reference: WSL GNU bash 5.3.0. Regression cover in
tests/issue701_read_eof_no_delimiter.rs (compound and inherited-fd
shapes, the while-loop count for terminated and unterminated trailing
records, plus newline-terminated and simple-command guards).
@caomengxuan666
caomengxuan666 merged commit 99ab98b into master Oct 17, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/700-read-dev-stdin branch October 18, 2026 09:29
caomengxuan666 added a commit that referenced this pull request Oct 18, 2026
…flat-region fire, return attribution (rubash#720)

Three facets of the set -E ERR adjudication at subshell boundaries, all
byte-pinned against WSL GNU bash 5.3.0(1) (stdin oracle):

- Form 1, nested subshell fire count: GNU marks a user subshell's WHOLE
  body CMD_NO_FORK when it is a single subshell command
  (execute_cmd.c:1799-1806) and dispatches it in place — no
  make_child/wait_for — so the middle frame of ( (false) ) never
  adjudicates and GNU fires twice, not three times. Rubash now scopes a
  subshell_fork_collapse flag around exactly that body shape (node form
  in execute_subshell_command_with_redirects, flat form at the ast_exec
  region open) and skips the aggregate ERR adjudication under it,
  including the set -e early conversion (command_execute.rs).

- Form 2, f() ( ) flat region: the completion ERR fire belongs to the
  frame that waited (execute_cmd.c:756-778, no &&/|| exemption, LINENO
  = the subshell's close-paren line per execute_cmd.c:696, BASH_COMMAND
  = the region's print_cmd text per the :703-710 fork preamble). The
  forkless region had no boundary fire — f() ( false ); f fired twice
  where GNU fires three. The parser now stamps the opener's end_line
  with the ) line, and every region close path runs
  close_flat_region_err_adjudication after the parent-state restore.

- Form 4, return-in-until attribution: a return N unwinding through
  dispatch is the builtin's own completion (return.def:112-115); the
  dispatcher gate fired with the loop condition's stale status and lost
  the N. The gate now passes Err(Return) through to the function/source
  boundary, which stamps exit_code = N before the call's own gate
  adjudicates — ERR:7 / exit 7, matching GNU.

Form 3 (cat <(false), the procsub copy path) stays tracked in #720 —
it is the PR #715 read/procsub reaping area, not a trap-context fix.

Companion #713 second pass: docs/audit/context-inheritance.md (the
context x property inheritance/isolation table), nested-context-probes
extended 24 -> 45 cases, and two new defect tickets found by the audit
(#825 -T DEBUG not inherited into $( ), #826 compound
redirection-failure diagnostic bypassing the ordered fd table).

Tests: tests/issue720_err_trap_nested_context_regressions.rs (12 cases,
one per form/edge, GNU transcripts pinned). Goldens 70/70, issue797
8/8, full-suite bidirectional comparison against origin/master shows no
new failures.
caomengxuan666 added a commit that referenced this pull request Oct 18, 2026
…flat-region fire, return attribution (rubash#720)

Three facets of the set -E ERR adjudication at subshell boundaries, all
byte-pinned against WSL GNU bash 5.3.0(1) (stdin oracle):

- Form 1, nested subshell fire count: GNU marks a user subshell's WHOLE
  body CMD_NO_FORK when it is a single subshell command
  (execute_cmd.c:1799-1806) and dispatches it in place — no
  make_child/wait_for — so the middle frame of ( (false) ) never
  adjudicates and GNU fires twice, not three times. Rubash now scopes a
  subshell_fork_collapse flag around exactly that body shape (node form
  in execute_subshell_command_with_redirects, flat form at the ast_exec
  region open) and skips the aggregate ERR adjudication under it,
  including the set -e early conversion (command_execute.rs).

- Form 2, f() ( ) flat region: the completion ERR fire belongs to the
  frame that waited (execute_cmd.c:756-778, no &&/|| exemption, LINENO
  = the subshell's close-paren line per execute_cmd.c:696, BASH_COMMAND
  = the region's print_cmd text per the :703-710 fork preamble). The
  forkless region had no boundary fire — f() ( false ); f fired twice
  where GNU fires three. The parser now stamps the opener's end_line
  with the ) line, and every region close path runs
  close_flat_region_err_adjudication after the parent-state restore.

- Form 4, return-in-until attribution: a return N unwinding through
  dispatch is the builtin's own completion (return.def:112-115); the
  dispatcher gate fired with the loop condition's stale status and lost
  the N. The gate now passes Err(Return) through to the function/source
  boundary, which stamps exit_code = N before the call's own gate
  adjudicates — ERR:7 / exit 7, matching GNU.

Form 3 (cat <(false), the procsub copy path) stays tracked in #720 —
it is the PR #715 read/procsub reaping area, not a trap-context fix.

Companion #713 second pass: docs/audit/context-inheritance.md (the
context x property inheritance/isolation table), nested-context-probes
extended 24 -> 45 cases, and two new defect tickets found by the audit
(#825 -T DEBUG not inherited into $( ), #826 compound
redirection-failure diagnostic bypassing the ordered fd table).

Tests: tests/issue720_err_trap_nested_context_regressions.rs (12 cases,
one per form/edge, GNU transcripts pinned). Goldens 70/70, issue797
8/8, full-suite bidirectional comparison against origin/master shows no
new failures.
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.

read builtin: empty from /dev/fd/N, and rc=0 at EOF under compound/fd redirection (while read off-by-one) read builtin hangs forever on /dev/stdin

1 participant