Repository navigation
fix(executor): apply redirections left to right (#699) - #725
Merged
Merged
Conversation
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.
This was referenced Oct 17, 2026
This was referenced Oct 17, 2026
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.
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 #699.
Fixes four of the five findings (R1.5 was already fixed on
origin/master); the residual is listed below.R1.1
2>&1 >fileorderGNU
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 injected2>&1onto every body leaf, so it re-resolved fd 1 after the later>filehad 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>&1and the{ cmd; } 2>&1 | nextpipeline 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
cddiagnosticTwo causes fixed:
2>/1>&2fd digit as a "numbered" (fd>2) redirect and stripped the mirror field, defeating both the per-leaf splice andcd's redirected-io binding;cd's Default sink wrote straight to the real stderr, bypassing an enclosing2>e/2>&1. It now routes through the enclosing fd state (using the live fd-1 alias for2>&1, so pipeline captures work).R1.5 bare
cat— already fixedorigin/masteralready truncates/does not doublecat 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, so2>&1is dropped. Call-sitef 2>&1 >/dev/nullis correct. Out of this PR's scope.{ exec > f; echo x >&2; } 2>&1(a body-levelexecrebinding fd 1 before a later leaf): still resolves the dup too late (pre-existing, rubash#170 nest4 class in the brace path).cat f >> ftruncation is fixed but GNU also printscat: f: input file is output file; rubash stays silent.Verification
/usr/local/bin/bash, owner-compiled baseline) script-file probes; thePATHbashis the Niubash shim and was not used as the oracle.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 --checkclean;cargo test --lib679/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).issue647has a pre-existing PATH-dependent failure that also fails on the unmodified base.