Skip to content

fix(executor): bind {var} heredoc at its list position in redirect-only exec (Refs #3893) - #3898

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3893-exec-var-heredoc-fd-allocation
Nov 18, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3893-exec-var-heredoc-fd-allocation

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Refs #3893.

根因

execute_stdio_only_exec_redirect(src/executor/trap_exec.rs)的 fd_var 分支对 HereDoc/HereString 只消费 heredoc_twins,真正的 execute_dynamic_fd_var_heredoc 绑定推迟到 walk 后的尾部循环,且 occupied 取全量 command_own_numbered_slots。同 list 后续条目的 undo-save(已 open 的 redirector,如 14<<B)先认领低位槽位,尾部绑定只得高一位:exec 14<<X {v}<<A 14<<B 给 v=11,GNU 给 v=10。

修法

fd_var 分支对 HereDoc/HereString kind 于 walk 位置直接调用 execute_dynamic_fd_var_heredoc(occupied_slots 即前缀语义,与 command_own_numbered_slots 文档的 "only the entries before the allocation point count" 一致);已绑定的 twin 记入 bound_fd_var_heredoc_twins,尾部循环只处理未被消费的 twin(无 redirects twin 的 store)。通用 fd_var applier(execute_dynamic_fd_var_redirect)本无 HereDoc/HereString 臂(对该 kind 是 no-op),故 walk 位置绑定是配对 twin 的唯一绑定;绑定失败时沿用本分支原有的错误处理(diagnostic + EX_REDIRFAIL 语义)。漏斗面(apply_dynamic_fd_var_redirects)未动。

GNU 锚点

  • redir.c:237 do_redirections:单一有序 walk。
  • redir.c:1065-1072 heredoc/here-string 臂:here_document_to_fd 在该条目位置物化 body。
  • redir.c:1082 REDIR_VARASSIGN:fcntl(fd, F_DUPFD, SHELL_FD_BASE);redir.c:1113 redir_varassign 绑定变量。
  • redir.c:1091:后条目对已 open redirector 的 undo-save(add_undo_redirect,F_DUPFD 自 SHELL_FD_BASE,redir.c:1303-1308)落在 var 分配之后。
  • (issue 正文所引 redir.c:940 是文件打开臂的同型 F_DUPFD,redir.c:965;heredoc 臂的对应行是 redir.c:1082。)

回归钉

tests/issue3893_exec_var_heredoc_fd_allocation.rs(6 tests):issue 原文形状、leftover open fd 变体、here-string 变体、以及三项 control(无 save 时的精确值、双 var、单 var)。全部期望已用 GNU bash 5.3.20 脚本探针逐字节核对。

验证(新服务器 Linux,cargo/rustc 1.99.1)

  • Fail-first(base bb27eb2 + 钉文件):3 红(list-position / leftover-open / here-string,均 v=11 vs 期望 v=10)、3 绿。
  • 修后:钉 6/6 绿;相邻 stem 全绿(issue3578_exec_multi_heredoc 14/14、issue3578_dual_heredoc_under_limit 6/6、issue1313_fd_var_redirect_regressions 2/2、issue3195_heredoc_fd_limit 19/19)。
  • lib:1280 passed、0 failed、4 ignored。
  • RUSTFLAGS="-D warnings" cargo check --locked --lib --bins 与 --test issue3893_... 均为 rc=0。
  • rustfmt --check(两改动文件)、check-marker-literals.py、check-utf8-boundary-hygiene.sh 均为 rc=0。

本 PR 删除了什么

什么也没删除:新增一个测试文件,修法只改 src/executor/trap_exec.rs 内 execute_stdio_only_exec_redirect 一处(+70/−22 行)。

UNVERIFIED-BY-CAPTAIN

#3893)

Redirect-only exec must allocate a {var} heredoc / here-string
descriptor at its list position in GNU's single ordered walk
(redir.c:237 do_redirections): the heredoc arm materializes the
body (redir.c:1065-1072) and dups it with fcntl F_DUPFD from
SHELL_FD_BASE for REDIR_VARASSIGN (redir.c:1082) before
redir_varassign binds the variable (redir.c:1113), so a later
entry's undo-save for an already-open redirector (redir.c:1091)
claims the slot after the var's. The tail-only bind gave v=11
for 'exec 14<<X {v}<<A 14<<B' where GNU gives v=10.

Nail only; the fix follows as a second commit on this branch.
Fail-first is demonstrated on the server against the base tip
with this file overlaid (see REPORT.md).
…ly exec (Refs #3893)

execute_stdio_only_exec_redirect deferred every {var}<< / {var}<<<
bind to the tail loop, after the whole redirect list had walked,
so a later entry's undo-save for an already-open redirector
claimed the low slot first: 'exec 14<<X {v}<<A 14<<B' gave v=11
where GNU gives v=10. Bind the heredoc in the fd_var branch at
its walk position instead (occupied_slots is the walk prefix),
and let the tail loop bind only the twins the walk did not
consume.

GNU anchors: redir.c:237 do_redirections (single ordered walk);
redir.c:1065-1072 heredoc arm (here_document_to_fd at the entry's
position); redir.c:1082 REDIR_VARASSIGN fcntl F_DUPFD from
SHELL_FD_BASE; redir.c:1113 redir_varassign; redir.c:1091 the
later entry's add_undo_redirect save. The generic fd_var applier
has no HereDoc/HereString arm, so the walk-position call is the
only bind for paired twins; unpaired stores still bind at the
tail, and the funnel face (apply_dynamic_fd_var_redirects) is
untouched.
@caomengxuan666 caomengxuan666 changed the title test(issue3893): pin {var} heredoc fd allocation at list position (Refs #3893) fix(executor): bind {var} heredoc at its list position in redirect-only exec (Refs #3893) Nov 18, 2026
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 18, 2026 11:25
@caomengxuan666
caomengxuan666 merged commit 9787804 into master Nov 18, 2026
9 of 12 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 18, 2026
…(rubash#3892)

队长复核 PASS 并合入(合并形态验证)。

修法:apply_dynamic_fd_var_redirects 按列表序把 heredoc_redirects 与 cmd.redirects 配对走,在第一个失败的非 {var} 重定向处停(non_fd_var_redirect_fails_at 前沿判定),{var} 条目在失败点之前者存活、之后者永不绑定——对齐 GNU do_redirections + undo_partial_redirects(redir.c:237 有序走);exec 被拒时同样不回滚已绑定的 {var}。

验证:原头 CI 裁清(census 81、新钉 10 PASS、goldens 基线二名、CLI 唯一红为旧基线上的 #3905 格)。因与 #3898 同触 trap_exec.rs,走了 update-branch 合并形态重跑:合并形态 census 79,与现尖 census 逐名对比零新增(issue2883 一格首跑翻红、重跑转绿,裁 census 抖动;issue3833 红为钉期望错误,与本 PR 无关,另由 #3916 对齐);CLI 仅 coproc 具名 fd 抖动格;goldens 基线二名;Rust tests 全绿。新钉 issue3892 与兄弟面 issue3893 在合并形态 census 中均 PASS。

本 PR 删除了什么:
- trap_exec.rs 内 apply_dynamic_fd_var_redirects 的旧无序应用路径被前沿语义实现替换(+323/−56 的净变更含新判定函数);redirection.rs 一行签名调整;未删除任何文件或测试。
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