Repository navigation
fix(executor): redirection leftovers from #699 (#726) - #783
Merged
Merged
Conversation
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.
…le (#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 was referenced Oct 19, 2026
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.
Fixes #726.
Redirection leftovers found while verifying #699 / PR #725. Verified
byte-for-byte against WSL GNU Bash 5.3.0 script-file probes (stdout, stderr,
file contents and
$?asserted separately); thebashon PATH is theNiubash shim and is circular here.
R1 — function definition-level stdio redirects were dropped
GNU stores a definition's redirect list on the function's body command and
applies it when the body runs, after the invocation's redirects
(
execute_cmd.c:854 do_redirectionsovertc->redirects; the call's list isinstalled at
execute_cmd.c:5606before dispatch). rubash bound only thenumbered (>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
the definition
2>&1 >errnever reached the body.f(){ echo OUT; echo ERR >&2; } 2>&1 >err; f 1>outerroutstdout_tailOUTERRERR+stdout_tailOUTstdout_tailOUTERRThe definition's whole ordered output list now binds the real fd table
(
open_compound_output_redirects,redir.c:767-955), nested after thecall-site binding so the definition is the inner, winning layer. The
call-site per-leaf stdio splice is suppressed for any slot the definition
rebinds, so a definition
2>&1is not clobbered by an injected call-site2>e:f(){ echo OUT; echo ERR >&2; } 2>&1; f 2>e→ GNU printsOUT/ERRandleaves
eempty; rubash before wroteERRtoe.Numbered (input and output) definition legs keep their existing path.
R2 — a body-level
execescaped the compound's ordered redirectsThe
2>&1splice added in #725 was replayed on every body leaf and resolvedits source at leaf time. A body-level
execis invisible to staticanalysis, so:
{ exec > f; echo x >&2; } 2>&1x,femptyxintof{ exec > h; echo y >&2; } 2>&1 | catyyintoh{ exec 2> g; echo x1 >&2; } 2>&1x1ingx1on stdoutThe replay is redundant: the fd-table binding from
open_compound_output_redirectsis the group-entry dup2 (redir.c:767-955)and is authoritative for both builtins (
apply_ordered_output_redirectsseeds from the fd table) and spawned children (
plan_external_stdioseedsfrom it;
injected_redirect_fd_is_bound). TheDuplicateOutputadmissionwas removed from
is_stdout_splice_redirect/is_stderr_splice_redirect;open/append legs are unchanged.
Pre-existing #725 guards re-asserted: call-site dups, safe
>f 2>&1,&>/&>>, nested groups,case/if/whilecompounds, fd-3 swaps,>&file(r_err_and_out).R3 — same-file
cat f >> fwarning (not landed)GNU prints
cat: f: input file is output file(and exits 1). This is acat.cdiagnostic, not shell machinery. rubash's inline cat already detectsthe shape —
external_file_builtins.rs::parse_cat_argvdrops any operandwhose text equals a redirect target via
cat_redirect_targets— but itdrops it silently and leaves
$?0. Emitting the GNU diagnostic meanschanging the inline external-cat emulation (and the pipeline inline cat in
pipeline_exec.rs), which the task scoped out ("only if it can be donewithout touching the external cat"). Left unlanded; design above.
Tests
tests/issue726_function_definition_redirects.rstests/issue726_compound_exec_rebinding.rsVerification
cargo fmt --checkclean. Fullcargo test --no-fail-fast: only thepre-existing environment failures remain (identical list on a pristine
origin/masterbuild:cli_testsexternal-helper/tilde/passwd cases,issue647,issue680,path_contract_boundary_verbatim,regressionupstream-snapshot). No new failures.