Skip to content

WIP: fix(fd): in-process serves resolve a dup source at its list position (#2314) - #2330

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/2314-dup-source-position
Oct 31, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/2314-dup-source-position

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2314 — residual D2 of the #2305 cluster: on the in-process serve path (read -u / mapfile -u), a dup whose source fd is rebound by a later entry of the same redirect list resolved against the source's final binding instead of its at-position binding. Cyclic dups (3<&4 4<&3) diverged the same way (reported as invalid fds).

Stacking: based on PR #2313's head (master d758fd0 + the #2305 serve commit). The defect exists only on top of #2305 — bare master does not contain the serve — so this PR is stacked on #2313 and lands after it; once #2313 lands, the diff reduces to the single #2314 commit.

GNU oracle

do_redirections (redir.c:237) is one ordered walk — sequential loop at redir.c:252 (for (temp = list; temp; temp = temp->next)). Every dup copies the descriptor as bound at its own position; a cyclic pair is not a loop (second dup sees the first's result; the first saw the pre-list ambient state).

Fix (mechanism, no per-shape guards)

  • own_input_binding_scan(cmd, fd, before) — the [bug] Heredoc binding on a nested group is shadowed by the outer group's binding for the same fd #2305 resolver's ordered walk, bounded to entries before before, returning the winner's list index. own_input_binding keeps its signature as the position-blind wrapper (kind-only callers untouched).
  • The dup chases (read/mapfile serve + the availability gate) thread the bound: start at the dup's own index, resolve each source against entries before the current bound; positions strictly descend, seen-set keyed on (fd, bound).
  • Terminal serves are entry-exact for list-sourced winners: read_redirected_fd_at / mapfile_redirected_fd_input_at bound the rev-find to the winning index; read_heredoc_fd_input_at / mapfile_heredoc_fd_input_at pick the heredoc entry by the ordinal correlation materialize_async_stdin_body uses. Entry cores extracted from the existing serves; unpositioned serves unchanged for the top-level fd and the ambient fallthrough.
  • redirect_in convention: it mirrors a list entry whose position is unrecoverable — consulted only when the bounded scan yields no binding at all, reports position None; a dup sourced from it resolves its source against the full list, after which positions strictly descend.

Verification

  • Script-file probes vs GNU bash 5.3.20: 12/12 byte-match post-fix (headline, cyclic ×2, rebound-to-file, rebound-to-heredoc, heredoc-source, chain ×2, close-after-dup, mapfile headline + cyclic, unrebound control, dup-of-closed-source control incl. stderr 4: Bad file descriptor). Pre-fix, 10 of 12 diverged.
  • New pins tests/issue2314_dup_source_position_regressions.rs: 12 passed (scrubbed-fd runner).
  • Controls: issue2305 16 passed, issue2292 18 passed, issue2235 11 passed.
  • Local builds used RUSTFLAGS="-C debuginfo=0" (canonical debuginfo=2 lib compiles are OOM-killed on this box); full regression + lib suites and canonical-profile verification are the collector's / CI's.

GNU applies a command's redirect list as one ordered walk:
do_redirections (redir.c:237) loops the list in order
(redir.c:252 `for (temp = list; temp; temp = temp->next)`), so a
dup copies its source descriptor as bound AT THE DUP'S OWN
POSITION. Later entries that rebind the source cannot change
what an earlier dup copied, and a cyclic pair (`3<&4 4<&3`) is
not a loop — the second dup sees the first's result, the first
saw the pre-list ambient state.

The #2305 serve resolved a dup's source with a whole-list
last-wins scan (position discarded) and served the terminal
file/heredoc by rev-find, so on `read -u 3 a 3<&4 4<inner.txt`
fd 3 read inner instead of fd 4's pre-list file, and the cyclic
pair chased 3->4->3 until the fd was reported invalid.

Thread positions through the serve instead:

- own_input_binding_scan(cmd, fd, before) bounds the ordered
  walk to the entries before `before` and returns the winner's
  list index; own_input_binding keeps its signature as the
  position-blind wrapper for kind-only callers.
- The dup chases (read/mapfile serve and the availability gate)
  start at the dup's own index and resolve each source against
  the entries before the current bound — positions strictly
  descend, seen-set keyed on (fd, bound).
- List-sourced winners are served entry-exactly:
  read_redirected_fd_at / mapfile_redirected_fd_input_at bound
  the rev-find to the winning index; read_heredoc_fd_input_at /
  mapfile_heredoc_fd_input_at pick the heredoc entry by the
  ordinal correlation materialize_async_stdin_body uses
  (same-fd, same-stringness document entries before the winner).
  Entry cores extracted from the existing serves; the
  unpositioned serves are unchanged for the top-level fd and
  the ambient fallthrough.
- redirect_in convention: it mirrors a list entry whose
  position is unrecoverable, so it is consulted only when the
  bounded scan yields no binding at all and reports position
  None; a dup sourced from it resolves its source against the
  full list, after which positions strictly descend.

Probes (GNU bash 5.3.20, script files): headline, cyclic both
directions, source rebound to file and to heredoc, heredoc
source, two chained-dup shapes, close-after-dup all byte-match
GNU; unrebound-dup and dup-of-closed-source controls unchanged.
Pins: tests/issue2314_dup_source_position_regressions.rs (12).
Controls green: issue2305 (16), issue2292 (18), issue2235 (11).

Stacked on PR #2313 (the defect exists only on top of the
#2305 serve); lands after it.

Refs #2314
@caomengxuan666
caomengxuan666 changed the base branch from fix/2305-heredoc-correlation to master October 31, 2026 05:13
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 31, 2026 08:25
@caomengxuan666
caomengxuan666 merged commit 45f2220 into master Oct 31, 2026
24 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2314-dup-source-position branch November 2, 2026 01:09
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.

1 participant