Repository navigation
fix(executor): bind {var} heredoc at its list position in redirect-only exec (Refs #3893) - #3898
Merged
Merged
Conversation
#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
marked this pull request as ready for review
November 18, 2026 11:25
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 一行签名调整;未删除任何文件或测试。
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.
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(无redirectstwin 的 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 锚点
do_redirections:单一有序 walk。here_document_to_fd在该条目位置物化 body。fcntl(fd, F_DUPFD, SHELL_FD_BASE);redir.c:1113redir_varassign绑定变量。add_undo_redirect,F_DUPFD 自 SHELL_FD_BASE,redir.c:1303-1308)落在 var 分配之后。回归钉
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)
RUSTFLAGS="-D warnings" cargo check --locked --lib --bins与--test issue3893_...均为 rc=0。本 PR 删除了什么
什么也没删除:新增一个测试文件,修法只改
src/executor/trap_exec.rs内execute_stdio_only_exec_redirect一处(+70/−22 行)。UNVERIFIED-BY-CAPTAIN