Skip to content

fix(executor): apply redirections left to right (#699) - #725

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/699-redirection-order
Oct 17, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/699-redirection-order

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #699.

Fixes four of the five findings (R1.5 was already fixed on origin/master); the residual is listed below.

R1.1 2>&1 >file order

GNU redir.c:767-955 (do_redirection_internal) applies a compound command's redirect list strictly left to right, each dup2 against the fd-table state at that moment. rubash also spliced an injected 2>&1 onto every body leaf, so it re-resolved fd 1 after the later >file had rebound it — both streams landed in the file.

$ { echo OUT; echo ERR >&2; } 2>&1 >f ; echo "file=[$(cat f)]"
niw  : file=[OUT\nERR]  (nothing on stderr)
bash : ERR  file=[OUT]

Only splice an fd-dup when no later redirect in the ordered list rebinds its source; otherwise the compound binding (open_compound_output_redirects, already left-to-right) is authoritative. The safe order >file 2>&1 and the { cmd; } 2>&1 | next pipeline routing are unchanged.

R1.2 builtin write error routing

The diagnostic now goes to fd 2 as the command's own redirects bound it (builtins/common.c:320-334 sh_chkwrite -> builtin_error), not the shell's real stderr.

R1.3 closed stderr

A failed fd-2 write is dropped silently and no longer corrupts the applet's status: ls missing 2>&- is quiet with rc 2, cat missing 2>&- rc 1.

R1.4 subshell cd diagnostic

Two causes fixed:

  1. the subshell stdio-mirror prep treated an explicit 2> / 1>&2 fd digit as a "numbered" (fd>2) redirect and stripped the mirror field, defeating both the per-leaf splice and cd's redirected-io binding;
  2. cd's Default sink wrote straight to the real stderr, bypassing an enclosing 2>e / 2>&1. It now routes through the enclosing fd state (using the live fd-1 alias for 2>&1, so pipeline captures work).

R1.5 bare cat — already fixed

origin/master already truncates/does not double cat f > f / >> f. Left alone.

Remaining / found but not fixed

  • f(){ echo OUT; echo ERR >&2; } 2>&1 >err (definition-level redirect): the function-definition path never group-binds the dup and no longer splices it, so 2>&1 is dropped. Call-site f 2>&1 >/dev/null is correct. Out of this PR's scope.
  • { exec > f; echo x >&2; } 2>&1 (a body-level exec rebinding fd 1 before a later leaf): still resolves the dup too late (pre-existing, rubash#170 nest4 class in the brace path).
  • cat f >> f truncation is fixed but GNU also prints cat: f: input file is output file; rubash stays silent.

Verification

  • WSL GNU Bash 5.3.0 (/usr/local/bin/bash, owner-compiled baseline) script-file probes; the PATH bash is the Niubash shim and was not used as the oracle.
  • New tests/issue699_redirection_order.rs (7 tests) fails 6/7 on the pre-fix tree and passes after; asserts file contents, stdout, stderr and status separately.
  • cargo fmt --check clean; cargo test --lib 679/679; focused redirect/fd/cd files green (issue392, issue518, issue279, issue355, issue370, issue429, issue514, issue525, issue510, issue551, issue415, issue368, issue362, issue320, issue324, issue535, issue359, parser_redirection_tests, cat_redirect_stream_regressions, lexer_operator_tests, posixexp2_comsub_regressions). issue647 has a pre-existing PATH-dependent failure that also fails on the unmodified base.

R1.1 `{ ...; } 2>&1 >file` landed both streams in the file. GNU
redir.c:767-955 do_redirection_internal applies a compound command's
redirect list strictly left to right, each dup2 against the state at that
moment. rubash additionally spliced an injected `2>&1` onto every body
leaf, resolving fd 1 *after* the later `>file` had rebound it. Only splice
an fd-dup when no later redirect in the ordered list rebinds its source;
otherwise the compound binding (open_compound_output_redirects) is
authoritative. Same fix for the case arms; the brace/case/function stdio
splice predicates now share support_names helpers and exclude positional
DuplicateOutput. The `>file 2>&1` safe order and the `2>&1 | next`
pipeline routing it protects are unchanged.

R1.2 A builtin write failure now reports on fd 2 as the command's own
redirects bound it, not the shell's real stderr
(builtins/common.c:320-334 sh_chkwrite -> builtin_error).

R1.3 A failed fd-2 write is dropped silently and no longer corrupts the
command status (`ls missing 2>&-` is quiet with rc 2).

R1.4 A subshell cd diagnostic now honours an enclosing `2>e` / `2>&1`
binding. Two causes: the subshell stdio-mirror prep treated an explicit
`2>`/`1>&2` fd digit as a "numbered" (fd>2) redirect and stripped the
mirror, and cd's Default sink wrote straight to the real stderr. Only
fd>2 redirects are stripped now, and cd's Default channel routes through
the enclosing fd state (live fd-1 alias for `2>&1`).

Verified byte-for-byte against WSL GNU Bash 5.3.0 script-file probes.
Regression tests: tests/issue699_redirection_order.rs.
@caomengxuan666
caomengxuan666 merged commit 8af0de2 into master Oct 17, 2026
8 checks passed
caomengxuan666 added a commit that referenced this pull request Oct 17, 2026
* fix(executor): drop the injected compound dup splice (#726)

A compound command's `2>&1` was replayed as an injected dup on every body
leaf, resolving its source at leaf time. A body-level `exec` is invisible
to static analysis, so `{ exec > f; echo x >&2; } 2>&1` wrote x into f,
and `{ exec 2>g; echo x1 >&2; } 2>&1` let fd 2 escape to stdout, instead
of GNU's group-entry dup2 (redir.c:767-955 do_redirection_internal,
unwound at redir.c:949-955).

The replay is redundant: open_compound_output_redirects binds the dup on
the fd table, and that binding is authoritative for builtins
(apply_ordered_output_redirects seeds from it) and spawned children
(plan_external_stdio seeds from it; injected_redirect_fd_is_bound).
Remove the DuplicateOutput admission from is_stdout_splice_redirect /
is_stderr_splice_redirect; open/append legs are unchanged.

Pre-existing #725 guards re-asserted: safe `>f 2>&1`, `&>`/`&>>`, nested
groups, case/if/while compounds, fd-3 swaps.

Verified byte-for-byte against WSL GNU Bash 5.3.0 script-file probes.
Tests: tests/issue726_compound_exec_rebinding.rs.

* fix(executor): bind function-definition stdio redirects on the fd table (#726)

GNU applies a function definition's redirect list when the body command
runs, after the invocation's redirects (execute_cmd.c:854 do_redirections
over tc->redirects; execute_builtin_or_function installs the call's list
at execute_cmd.c:5606 before dispatch). rubash bound only the numbered
(>2) definition entries and the per-leaf stdio splice carries at most one
leg and is skipped once the call-site splice claimed the slot, so
`f(){ echo OUT; echo ERR >&2; } 2>&1 >err; f 1>out` sent OUT to stdout
instead of `err`.

Bind the definition's whole ordered output list — stdio slots included —
via open_compound_output_redirects (redir.c:767-955), nested after the
call-site binding so the definition is the inner, winning layer. Numbered
input/output legs keep their existing path. Suppress the call-site
per-leaf stdio splice for any slot the definition rebinds, so a
definition `2>&1` is not clobbered by an injected call-site `2>e`
(`f(){ echo ERR >&2; } 2>&1; f 2>e` prints ERR and leaves e empty, like
GNU).

Verified byte-for-byte against WSL GNU Bash 5.3.0 script-file probes.
Tests: tests/issue726_function_definition_redirects.rs.
@caomengxuan666
caomengxuan666 deleted the fix/699-redirection-order branch October 18, 2026 09:29
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.

redirection & fd: 2>&1 >file order, builtin write-error routing, closed-stderr status, subshell-uncaptured cd error, bare-cat same-file

1 participant