Skip to content

fix(executor): redir-fd slice — redirect setup side effects, closed-fd targets, exec persistent close/move - #1666

Merged
caomengxuan666 merged 6 commits into
masterfrom
integrate/redir-fd
Oct 25, 2026
Merged

caomengxuan666 merged 6 commits into
masterfrom
integrate/redir-fd

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

fix6 redir-fd slice (6 commits, one per issue, cherry-picked from the family branch; baseline f6b5a3f → current master):

Each fix carries a regression test file whose tests are named after the issue, with expectations written to GNU bash 5.3.20 behavior (20 tests total).

Fixes #1590
Fixes #1591
Fixes #1592
Fixes #1593
Fixes #1594
Fixes #1595

…(rubash#1593)

GNU redir.c do_redirections opens every redirection of a command in
list order before the command runs, and a failed open is a
redirection_error (redir.c:149-176): the command never runs and the
status is 1. The read builtin only probed its fd-0 input, so
`read l >f1` created nothing and `read l >sub/f1` failed silently;
the emulated external commands (cat/sed/mkdir/touch/...) execute
in-process and bypassed both the spawn path's fd>=3 open loop and
the command_output_redirect_fails precheck, so `cat 3>f` created
nothing and `cat 3>sub/f` ran on with status 0.

Add apply_redirect_setup_side_effects (builtin_redirects.rs): an
ordered walk over cmd.redirects performing only the open side
effects — skipping {var} entries, injected group redirects, and
non-plain-file targets (close forms, fd dups, /dev/fd aliases, the
null device, procsub targets) — reporting the first failure through
the routed redirect diagnostic and short-circuiting with status 1.
Invoke it at the top of execute_read and in
handle_external_file_builtins just before dispatch.

Regression tests: tests/issue1593_redirect_setup_side_effects_regressions.rs
…`&-` (rubash#1594)

The parse of `cat 1>&-` was already correct (kind=CloseOutput,
target `&-`), but the emulated cat output paths opened the mirror
target as a literal filename: `cat 1>&-` created a file named `&-`
and exited 0. GNU runs redir.c r_close_this first, so cat's first
write fails EBADF: `cat: standard output: Bad file descriptor`,
status 1, no file.

Guard the three literal opens in the cat output machinery
(write_cat_output and open_cat_output_sink in external_finish.rs,
and the heredoc arm of external_cat in external_file_builtins.rs):
an is_closed_redirect_target target returns the shared
cat_closed_stdout_error instead of an open, which the command
boundary renders as one diagnostic line with status 1 — the same
shape as the existing report_cat_closed_stdin convention. Sweep
result: `<&-` and the close forms at every other fd position
(echo/printf `>&-`, `cat <&-`, `cat 2>&-`) already matched GNU;
only the cat output position was broken.

Regression tests: tests/issue1594_close_redirect_target_regressions.rs
…h#1595)

`oe &>f1 2>f2` sent both streams to f1 and left f2 empty: the
function-call applier propagated the fd-2 legs through the separate
mirror slots, injecting the genuine redirect_err block before the
synthetic redirect_err_append leg of the combined entry — the
combined leg always won fd 2 regardless of list position.

GNU redir.c do_redirections applies the call's list strictly left
to right and the later binding wins; the `&>` fd-2 leg is a dup of
fd 1 taken at its own position (redir.c:899-900 splits
r_err_and_out; :1030-1037). Rewrite apply_function_call_redirects
(function_calls.rs) as a single ordered pass over call_cmd.redirects
that performs each entry's create side effect in order, remembers
only the LAST write leg per descriptor (the injectors keep the
first leg they plant, so selection — not injection — carries the
ordering), and injects those two legs at the end. A combined
entry's fd-1 leg is rewritten to a plain fd-1 Append before
injection: replayed as CombinedOutput on a body leaf it would
re-bind fd 2 as well, resurrecting the combined target over the
later genuine `2>` leg during the leaf's own ordered walk.

Verified against GNU bash 5.3.20 on both orders plus six mixed
shapes (`>a &>b 2>c`, `2>a >b &>c`, `&>>a 2>b`, `&>a 2>>b`,
`>a 2>b &>c`, `&>a &>b`) — byte-identical file outcomes.

Regression tests: tests/issue1595_combined_redirect_rebind_regressions.rs
… (rubash#1590)

`exec 3<d` followed by `exec 3>&-` left fd 3 fully readable:
the exec-only redirect walker closed by spelling direction —
CloseOutput/`>&-` went through close_persistent_output_fd (clears
only the write endpoint) and CloseInput/`<&-` through
close_persistent_input_fd (only the read endpoint) — and the move
forms closed their source the same one-sided way. The surviving
endpoint kept `read -u 3`, `<&3` dups, and spawned children working
on a descriptor GNU considers closed. The {var} move applier had
the same one-sided source close.

GNU redir.c: r_close_this is close(redirector) and the dup/move
case of do_redirection_internal ends with close(redir_fd) — a
descriptor is one object and closing it kills both directions.
Route every one of these closes through the existing
close_persistent_fd, which runs the output side's procsub/coproc
teardown, clears the input ledger, closes the fd-table entry, and
sets the closed key: the exec walker's Output/DuplicateOutput
close branches, CloseOutput, the Input close branch, CloseInput,
all four move-source closes, the ambient resolver's close branch,
and both {var} move-source closes in
execute_dynamic_fd_var_redirect.

Regression tests: tests/issue1590_exec_persistent_close_move_regressions.rs
…on (rubash#1591)

`exec 3<d; exec {b}>&3-` left b unset forever: the DuplicateOutput
arm of execute_dynamic_fd_var_redirect required the source to be
open FOR WRITE (and DuplicateInput required open-for-read), so a
move from a read-only descriptor — the common case — silently
returned without allocating or assigning.

GNU redir.c's dup/move case with REDIR_VARASSIGN copies the
descriptor wholesale via fcntl(F_DUPFD) and never consults its
direction; redir_varassign then binds the fresh descriptor and a
move closes the source. Replace both direction gates with
is_open (either direction), matching the numbered exec move arm,
which already judged by is_open.

Regression tests: tests/issue1591_exec_var_move_assign_regressions.rs
…h#1592)

The -u availability gate reported every failure as
`read: N: invalid file descriptor: Bad file descriptor`. GNU
distinguishes the two EBADF cases: a descriptor that is not open
at all fails read.def:378-380's sh_validfd check and is an
"invalid file descriptor", while an open but write-only
descriptor passes that check and fails in the read itself —
read.def:911's "read error". Pick the wording at the diagnostic
site from fd_table.is_open(fd); status stays 1 and the
`: Bad file descriptor` suffix is unchanged.

Regression tests: tests/issue1592_read_u_writeonly_fd_regressions.rs
@caomengxuan666
caomengxuan666 merged commit 9c90ab9 into master Oct 25, 2026
5 of 8 checks passed
@caomengxuan666
caomengxuan666 deleted the integrate/redir-fd branch October 30, 2026 01:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment