Skip to content

fix(parser, executor): {var}<< heredoc must not bind fd 0 (Refs #3894) - #3915

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3894-varassign-heredoc-no-fd0
Nov 18, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3894-varassign-heredoc-no-fd0

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

根因

{var}<< / {var}<<< 的 heredoc body 在 simple command 上被当作 fd 0 stdin 喂给命令;GNU 只把 body 绑到分配的 fd,命令 stdin 不受影响。

GNU 锚点:redir.c:963-968 do_redirection_internal(RX_ACTIVE 臂)——REDIR_VARASSIGN 时 redirector 为 fcntl (fd, F_DUPFD, SHELL_FD_BASE) 新分配的 fd(>= 10),redir_varassign(redir.c:1470)把变量绑到该 fd;fd 0 从不参与。heredoc 臂(redir.c:423 here_document_to_fd、:1065-1072)把 body 物化到该分配 fd 上。

rubash 缺陷:parser 把所有 fd=None 的 heredoc body 都镜像进 fd-0 stdin 镜像 cmd.heredoc(src/parser/redirections.rs fill_pending_heredoc_body),且每条 stdin 推导路径(funnel staging command_input_redirect、stdin-string 解析 stdin_string_for_command[_mut]、fd-0 胜者 fd0_stdin_redirect_winner、function/pipeline/if 的 has-own-stdin 判定)都把 fd=None 的 twin 当作 fd-0 body,未排除 fd_var.is_some()。于是 cat {v}<<A 打印 body,read x {v}<<A 读到 body(rc=0)。

修法(按机制:fd_var 条目不参与 fd0 绑定)

  • src/parser/redirections.rs:fill_pending_heredoc_body 仅把 plain fd-0 body 镜像进 cmd.heredoc;delimiter/gather/end-line 仍为 {var} 条目记录(EOF 警告是 gather 层面的事,GNU 对 {var}<< 同样警告)。
  • src/executor/shell_options.rs:fd0_stdin_redirect_winner 加 fd_var.is_none()(与已有 fd_var 排除的姊妹函数 fd0_stdin_redirect_winner_redirect([bug] 简单命令自身 fd-0 重定向不到达 fd 事实 sync([ -f /dev/stdin ] < file 假,#1902 家族) #2131)对齐);stdin_string_for_command[_mut] 的三处 twin 查找加同样排除。
  • src/executor/command_input_scope.rs:command_input_redirect 的 twin 查找加排除。
  • src/executor/function_calls.rs、src/executor/pipeline_exec.rs(含 timed followup)、src/builtins/source/simple_if.rs、src/executor/compound_exec.rs:has-own-stdin / prune 判定加同样排除。
  • src/executor/execution_misc.rs:unterminated/warned heredoc 标记同时查 twins,保证 {var}<< 缺 delimiter 时警告不丢。

未动:src/executor/trap_exec.rs、src/executor/command_dispatch.rs、apply_dynamic_fd_var_redirects 本体(在飞 PR #3902 与 #3906 的面,本单不碰);src/lexer/continuation.rs、docs/api-ledger.md 未动。

回归钉

tests/issue3894_varassign_heredoc_no_fd0.rs(12 tests):{v}<< / {v}<<< 外部命令与 builtin read 面、混合顺序(plain 与 {var} 两个方向)、双 {var} 面、function 调用面、if 条件面、pipeline 面({var} 不抢 pipe stdin,plain << 仍抢)、9<< / plain << / read << 对照、缺 delimiter 警告面。期望值均以 GNU bash 5.3.20 脚本文件实测为准(2026-11-18)。混合顺序的分配 fd 号未钉死(取决于 heredoc 传输形态),只钉 stdin 结果。

本 PR 删除了什么

无文件、无公开 API、无测试删除。行为上删除的唯一东西:{var} heredoc body 不再被当作 fd-0 stdin(即本单修的缺陷本身)。

验证

  • GNU oracle:12 钉期望逐项对 GNU bash 5.3.20 脚本文件实测(含 read x {v}<<A → x= v=10 rc=1、cat {v}<<A 无输出、混合顺序取 plain body、缺 delimiter 警告)。
  • fail-first 与修后绿、全量 lib 与 base 双边、RUSTFLAGS="-D warnings" check、rustfmt:新服务器腿上跑,见 REPORT(车道工作区根)。
  • python3 ~/workspace/batch-tools/scrubbed_cargo.py 不适用本腿(Linux 服务器原生 cargo)。

UNVERIFIED-BY-CAPTAIN

@caomengxuan666 caomengxuan666 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

队长复核:待改(不合入)。头 CI 已逐名裁完:

1. 自带钉一格红(必修)
issue3894_varassign_heredoc_no_fd0::unterminated_varassign_heredoc_still_warns(tests/issue3894_varassign_heredoc_no_fd0.rs:213):期望 stdout 为 v=\n,实得空。11/12 格绿,只有这一格红——请按修法与钉的对齐纪律处理:若是产品在未终结 heredoc 警告路径上丢了绑定/输出,修产品;若是钉期望与 GNU 不符,附 oracle 证据改钉。修完重跑本茎。

2. goldens 一格非基线红(须复核)
issue_1903::issue1903_foreign_winuxcmd_marker_is_honored:cat: f: Broken pipe,rc 1 vs 期望 0。其余 goldens 只剩基线二名(issue_753 case_9 + upstream slice)。本 PR 动了 pipeline_exec/execution_misc,须确认这格是时序抖动还是与改动相关:请重跑 goldens 一次;若复现,定位 pipe/fifo 面与本改动的关联再报。

3. 基线与合并形态

  • census 80:除自带钉一格外,新增 vs 现尖仅 issue2883(已知 census 抖动,#3902 重跑已证)与 issue3833(本 PR 基线在 #3916 之前,属继承红,rebase 后自清)。
  • CLI 唯一红为 coproc 具名 fd 抖动格。Rust tests 全绿。
  • 本 PR 基线(14:34)在 #3901/#3902/#3916 之前:请 rebase 到现尖 e39c44d 再推,随后队长按合并形态重裁。

改动面本身(parser/redirections.rs + executor 若干文件)与 #3902 的 trap_exec 面无文件冲突;根因叙述与 GNU 锚点(redir.c:963-968 / :1470 / :423)完整。

GNU redir.c:963-968 do_redirection_internal (REDIR_VARASSIGN): the
redirector is a freshly allocated fd (fcntl F_DUPFD SHELL_FD_BASE);
redir_varassign (redir.c:1470) binds the variable to it. fd 0 never
participates. rubash mirrored every fd-None heredoc body into the
fd-0 stdin mirror (cmd.heredoc) and every stdin derivation treated
an fd-None twin as an fd-0 body without excluding fd_var entries,
so `cat {v}<<A` printed the body and `read x {v}<<A` consumed it.

- parser: fill_pending_heredoc_body only mirrors plain fd-0 bodies
  into cmd.heredoc; delimiter/gather/end-line still record {var}
  entries so the EOF warning survives.
- executor: fd0_stdin_redirect_winner, stdin_string_for_command[_mut],
  command_input_redirect, and the function/pipeline/if-condition
  has-own-stdin predicates exclude fd_var twins/entries.
- execution_misc: the unterminated/warned heredoc markers are also
  consulted in the twins, so the EOF warning still fires for {var}<<.

Nails: tests/issue3894_varassign_heredoc_no_fd0.rs (12 tests: {v}<<,
{v}<<<, mixed orders, function/if/pipeline faces, controls, EOF
warning). Expectations measured against GNU bash 5.3.20 script files.

UNVERIFIED-BY-CAPTAIN
#3894)

The unterminated cell asserted stdout == "v=\n", but its script
contains no echo: GNU prints nothing on stdout for
`cat {v}<<A` + body + EOF (cat reads the untouched fd 0), warns
on stderr, rc 0. No echo line can follow an unterminated heredoc
(the rest of the file IS the body), so empty stdout is itself the
assertion that the body did not feed stdin. Product unchanged;
the EOF warning path (execution_misc twins consult, #3894) is
exercised by the stderr assertion.

Oracle: GNU bash 5.3.20 script file, stdin /dev/null.
@caomengxuan666
caomengxuan666 force-pushed the fix/3894-varassign-heredoc-no-fd0 branch from 776af6a to da03356 Compare November 18, 2026 16:11
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 18, 2026 16:40
@caomengxuan666
caomengxuan666 merged commit f983851 into master Nov 18, 2026
7 of 12 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 18, 2026
…#3252)

Without `set -u`, `(( b[$undef]++ ))` was silent on stderr: the `$undef`
inside the subscript expands to empty and the evaluation read/bound
element 0. GNU bash 5.3 reports `b[]: bad array subscript` twice plus
`((: \`b[]': not a valid identifier`, and keeps evaluating (rc 1).

Root cause: `eval_subscript_index` (value.rs) mapped every empty
expansion to index 0. GNU's law branches on the ENTRY, not the
expansion: `(( ))`, `$(( ))` and `[[ ]]` run expand_arith_string
before evalexp, so the STR token reaching evalexp is literally `b[]`
and array_variable_name fails it (arrayfunc.c:1405-1416, `ni <= ind+1`);
`let` operands arrive EXP_EXPANDED, so the token is `b[$undef]` and the
empty expansion inside array_expand_index is index 0 (`let 'b[$undef]++'`
binds b[0] silently — verified GNU 5.3.20).

Fix: thread an `expanded_entry` flag from the evaluation entries into
`ConditionalArithParser` (mod.rs / conditional.rs) and return
`SubscriptIndex::Empty` from `eval_subscript_index` when a
marker-decoded subscript is empty in an expand entry. Callers convert
it to the existing `InvalidElement` shape: reads diagnose `b[]: bad
array subscript` twice and yield 0; binds fail as
`` `b[]': not a valid identifier `` and store nothing. Plain `=` never
reads (expr.c:1395-1401), so it reports only the invalid identifier.
Whitespace-only expansions stay index 0 in every entry (the bracket
text is non-empty). Nested frames (variable-value recursion, subscript
evalexp) run with the flag off: their input text reaches subexpr
verbatim (expr.c:1241).

GNU anchors: arrayfunc.c:1405-1416 array_variable_name,
error.c:440-443 err_badarraysub, expr.c:1183/1227 expr_streval (two
lookup points), expr.c:1395-1401 (EQ peektok), expr.c:331-354
expr_bind_variable -> sh_invalidid, expr.c:1044-1054/1101 exp0
post-inc/dec, expr.c:611-614 expassign (lind == -1).

Regression nail: tests/issue3252_arith_subscript_diag_no_nounset.rs
(19 cells: (( ))/let/$(( ))/[[ ]] forms, assign/++/--/pre-inc,
bound/spaced/value-recursion/assoc controls).

## 队长续作(c5c0958c):assoc 空括号臂

续作车道以 GNU 5.3.20 oracle 证明钉是对的、产品是错的:`declare -A C;
(( C[$undef]++ ))` 下 GNU 为 badsub ×2 + `` `C[]': not a valid identifier ``
×1(rc 1),产品只出 badsub ×2 且静默绑了 `C[""]`。根因在
src/executor/arithmetic/lvalue.rs:`parse_assoc_subscript` 的空括号分支
急于记 2× BadSubscript 并返回 `Key("")`,丢了 `C[]` 词元。修复:新增
`ParsedAssocSubscript::EmptyBracket`,在两处 match 点映射为
`ArithLValue::InvalidElement`(与 indexed `a[]` 同形);读侧报 badsub
×2(expr.c:1183/1224),绑侧报 invalid-id ×1(variables.c:3403
sh_invalidid,经 expr.c:1052 expr_bind_variable)。队长亲手应用并推送,
见 fix-3252b/REPORT.md。

## 头 CI 裁定(队长,run 44356117145)

census 78,自带钉 19/19 绿;唯一新增格为 issue1902 fd-alias 抖动族
(本修法执行不到该面);goldens 基线二名;CLI 全绿;macOS job
cancelled 且 steps 为空(从未运行,非产品红);其余全绿。服务器腿
(lib 双边、-D warnings)待链路恢复后补跑;合入依据为上述已裁定的
头 CI(与 #3920/#3915 同例)。

Refs #3252

## 本 PR 删除了什么:

- 未删除任何产品代码、测试用例或钉。本 PR 只加算术下标诊断发射
  (arithmetic/{parser,value,lvalue,mod}.rs、conditional.rs)与回归钉
  tests/issue3252…;既有断言保持 GNU 真值不变。
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