Skip to content

fix(executor): redirection leftovers from #699 (#726) - #783

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/issue726-redirect-leftovers
Oct 17, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/issue726-redirect-leftovers

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

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); the bash on PATH is the
Niubash 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_redirections over tc->redirects; the call's list is
installed 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
the definition 2>&1 >err never reached the body.

f(){ echo OUT; echo ERR >&2; } 2>&1 >err; f 1>out

stdout stderr err out
GNU stdout_tail – OUT ERR
rubash before ERR + stdout_tail – (empty) OUT
rubash after stdout_tail – OUT ERR

The definition's whole ordered output list now binds the real fd table
(open_compound_output_redirects, redir.c:767-955), nested after the
call-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>&1 is not clobbered by an injected call-site
2>e:

f(){ echo OUT; echo ERR >&2; } 2>&1; f 2>e → GNU prints OUT/ERR and
leaves e empty; rubash before wrote ERR to e.

Numbered (input and output) definition legs keep their existing path.

R2 — a body-level exec escaped the compound's ordered redirects

The 2>&1 splice added in #725 was replayed on every body leaf and resolved
its source at leaf time. A body-level exec is invisible to static
analysis, so:

probe GNU rubash before
{ exec > f; echo x >&2; } 2>&1 prints x, f empty writes x into f
{ exec > h; echo y >&2; } 2>&1 | cat prints y writes y into h
{ exec 2> g; echo x1 >&2; } 2>&1 x1 in g x1 on stdout

The replay is redundant: the fd-table binding from
open_compound_output_redirects is the group-entry dup2 (redir.c:767-955)
and is authoritative for both builtins (apply_ordered_output_redirects
seeds from the fd table) and spawned children (plan_external_stdio seeds
from it; injected_redirect_fd_is_bound). The DuplicateOutput admission
was 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 / while compounds, fd-3 swaps,
>&file (r_err_and_out).

R3 — same-file cat f >> f warning (not landed)

GNU prints cat: f: input file is output file (and exits 1). This is a
cat.c diagnostic, not shell machinery. rubash's inline cat already detects
the shape — external_file_builtins.rs::parse_cat_argv drops any operand
whose text equals a redirect target via cat_redirect_targets — but it
drops it silently and leaves $? 0. Emitting the GNU diagnostic means
changing the inline external-cat emulation (and the pipeline inline cat in
pipeline_exec.rs), which the task scoped out ("only if it can be done
without touching the external cat"). Left unlanded; design above.

Tests

  • tests/issue726_function_definition_redirects.rs
  • tests/issue726_compound_exec_rebinding.rs

Verification

cargo fmt --check clean. Full cargo test --no-fail-fast: only the
pre-existing environment failures remain (identical list on a pristine
origin/master build: cli_tests external-helper/tilde/passwd cases,
issue647, issue680, path_contract_boundary_verbatim, regression
upstream-snapshot). No new failures.

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.
@caomengxuan666
caomengxuan666 merged commit 527bb98 into master Oct 17, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/issue726-redirect-leftovers 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.

redirect leftovers from #699: definition-level dup splice lost, runtime exec escapes group order, missing cat same-file warning

1 participant