Repository navigation
fix(executor): redir-fd slice — redirect setup side effects, closed-fd targets, exec persistent close/move - #1666
Merged
Merged
Conversation
caomengxuan666
force-pushed
the
integrate/redir-fd
branch
from
October 25, 2026 17:25
6e34a65 to
6739b38
Compare
…(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
force-pushed
the
integrate/redir-fd
branch
from
October 25, 2026 19:12
6739b38 to
67dd156
Compare
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.
fix6 redir-fd slice (6 commits, one per issue, cherry-picked from the family branch; baseline f6b5a3f → current master):
readand emulated external commands now perform GNUdo_redirectionsopen side effects: targets are created/truncated, and an open failure is diagnosed with exit status 1 and the command does not run (previouslyread >fcreated nothing andcat 3>sub/fzsilently ran with rc=0).cat 1>&-no longer treats&-as a filename: writing to a closed fd fails with an EBADF diagnostic and status 1, creating no file.&>f1followed by2>f2(and the reverse order) resolve exactly as GNU's ordereddo_redirections.execpersistent close/move now closes the whole descriptor (both directions), so a closed fd is gone forread -u,<&Ndup, and child-process inheritance.exec {var}>&N-/<&N-dup/move no longer gates on the source fd's direction; the target variable is always assigned when the source is open (GNUF_DUPFDsemantics).read -uon an open-but-write-only fd now reportsread error(GNU read.def EBADF path);invalid file descriptoris kept for genuinely unopened fds.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