Skip to content

fix(executor): consume fatal arith evalerror at the [[ ]] command boundary (Refs #3382) - #3444

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3382-cond-operand-fatal-leak
Nov 15, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3382-cond-operand-fatal-leak

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #3382

现象

[[ $(( 1/0 )) = x ]]
echo after

GNU:除零诊断后 echo after 正常执行。rubash:致命标志在 [[ ]] 操作数求值后泄漏到下一命令,echo after 被 discard(与 #3287 同机制——触发命令边界未整条消费 evalerror_arith_pending;但 [[ ]] 分派缺自己的 abort 检查,标志甚至能越过下一个 [[ ]])。

根因(GNU 源码先行)

GNU expr.c 的 evalerror 打印诊断后 sh_longjmp(evalbuf, 1);evalexp(expr.c:429-445)的 setjmp 捕获返回 0,失败的算术展开产出 &expand_wdesc_error,调用方跳 DISCARD——eval.c:111 reader_loop 只丢弃当前逻辑行的剩余部分;execute_cmd.c:4132 execute_cond_command 执行测试,GNU 不存在跨命令的错误状态。rubash 把算术错误记在三面旗(arithmetic_expansion_error / arithmetic_fatal_error / arithmetic_nonfatal_error)+ evalerror_arith_pending 里,而 [[ ]] 的两条分派路径(command_prepare.rs 的 with-redirects 包装与 command_dispatch_late.rs 的 Conditional 臂)从不消费它们,于是下一命令的 abort_on_expansion_errors 把无辜命令丢弃。

修复

  • src/executor/conditional.rs:新增共享 helper take_conditional_arith_evalerror——三面旗作为一条错误记录整体消费(nounset 除外,它走自己的 FORCE_EOF 路径),并把 evalerror_arith_pending 置位以武装 DISCARD 类([bug][regression] set -e: arithmetic expansion error exits the whole shell instead of discarding the current command (#1253) #1518:errexit 提升跳过该类,set -e 下 DISCARD 本身不退出)。
  • src/executor/command_prepare.rs:with-redirects 包装在 [bug] Slice error inside [[ ]] does not abandon rest of line (GNU skips it; sibling of #1240) #1422 检查之前消费并返回 ExpansionFailure(1)(DISCARD 类);arith 优先于 conditional_error——arith 致命意味着后续操作数仍跑过,conditional_error 可能后设,而 DISCARD 必须武装 errexit 豁免。
  • src/executor/command_dispatch_late.rs:Conditional 臂同样消费(nounset 检查先行,FORCE_EOF 永不降级为 DISCARD)。
  • src/executor/ast_exec.rs:列表循环的 DISCARD 臂吸收跳跃后消费 evalerror_arith_pending,豁免随丢弃行终结——set -e 下后续 false 仍退出(否则豁免泄漏,错误地豁免后续失败)。

回归钉

tests/issue3382_bracket_operand_fatal_leak.rs(17 格,GNU bash 5.3.20 oracle 逐字节对账):canary、状态 1、同行丢弃/次行执行、&&/|| 链、if 条件、set -e 不退出 + 后续失败仍退出、set -u nounset 仍致命、右操作数、取反、连续两条、[[ ]]/(( ))/let/赋值后续均干净、#3287 赋值对照。

本 PR 删除了什么

无删除。本 PR 为纯新增(+423/−0):一个 helper、两处边界消费、一个 DISCARD 吸收点、一个钉文件;无函数、无文件、无公开 API 被删除或改名。

UNVERIFIED-BY-CAPTAIN

…ndary (Refs #3382)

A fatal $(( )) inside a [[ ]] operand latched the arithmetic error
flags past the [[ ]] command: the NEXT command was discarded by
abort_on_expansion_errors instead of running (same family as #3287,
but the [[ ]] wrapper never consumed the flags at all). GNU's evalerror
jumps DISCARD for the current command only (expr.c evalerror ->
eval.c:111 reader_loop), so the [[ ]] command itself owns the failure:
take_conditional_arith_evalerror consumes the three flags as one
error record at the [[ ]] boundary (with-redirects wrapper and the
late-dispatch arm) and returns the DISCARD class, and the list loop
consumes evalerror_arith_pending when it absorbs the DISCARD so the
errexit exemption ends with the discarded line.

UNVERIFIED-BY-CAPTAIN
caomengxuan666 added a commit that referenced this pull request Nov 14, 2026
…efs #2764)

Refs #2764.

## 根因
case 模式词里的未加引号 <(...) / >(...) 被原样收作模式文本,模式展开路径从不执行它:体不跑、也不拼出 /dev/fd/N 模式。同一位置的命令替换 $(...) 早已执行。

## 修法
- `expand_case_pattern`(src/executor/compound_exec.rs):在任一展开分支跑之前,复用词管线的替换执行载体 `materialize_for_list_process_substitutions` 把 fd 路径拼进模式文本与 raw 文本。派生用 `WordMetadata::new` 对 raw 文本的 quote-aware 扫描,加引号的 span 从不在派生表里,故保持字面量;每 attempt 重新 materialize,与 GNU 的 per-attempt 重展开一致。
- 分支判定与下游消费一致:`quoted` 只算一次;有 procsub 时用 `case_pattern_raw_has_outside_quotes` 排除体内部引号(体内的引号属于子进程,随 splice 消失),下游复用同一判定,不再二次判定。
- case 命令 wrapper:无重定向面与有重定向面都在 case 完成时 finish 其 >(...) 体,使作为最后一条命令的 case 也不丢 output 体。

## GNU 锚点(bash 5.3.20)
- execute_cmd.c:3712:case 循环每 pattern 调 `expand_word_leave_quoted(list->word, 0)`;3733-3736:模式不做 quote removal。
- subst.c:11365-11385:`expand_word_internal` 的 `<`/`>` 臂;引号内(`Q_DOUBLE_QUOTES`)走 `add_character`,保持字面量。
- subst.c:1311 `extract_process_subst`:原样抽体;subst.c:6362 `process_substitute`:fork 子进程、拼 `/dev/fd/N`。
- 探针:车道 probes/gnu{1,2,3,5}.sh 在 GNU 5.3.20 实测,钉期望即其实测结果。

## 回归钉
tests/issue2764_case_pattern_procsub.rs(20 tests):fd 路径匹配、变量词、<(...) 体副作用、>(...) 体副作用(case 后立即检查)、引号字面量、glob 后缀(未加引号 `*` 匹配、加引号 `'*'` 字面不匹配)、词中/前缀拼接、双 procsub 不同 fd、体输出不是模式、comsub 对照、逐 arm 重展开、extglob 候选、count-in/out/two-arms 计数(每 attempt 一遍)、Q5/Q5b/Q5m/Q5o 体内置引号匹配。

## 已验证(队长复核,卷宗 ~/workspace/review-3459/CAPTAIN-VERIFY.md §14)
- 钉头 20/20;基底 fail-first 4 passed / 16 failed(16 红全为 procsub 缺陷面)。
- GNU oracle Q5 四形逐字 GNU==HEAD;嵌套 procsub 形亦 MATCH。
- lib 双边失败名集为空(1246 passed / 0 failed / 4 ignored,双边一致);`RUSTFLAGS="-D warnings"` rc=0;fmt/UTF-8 卫生全绿。
- 合并形态重验(update-branch 后合并头 f653f36):钉 20/20;邻居 issue3276 19/19、issue3416 5/5、issue3333 18/18、case_command_head_bash_command_regressions 4/4;终头 CI(run 43610297783)三红唯一格为 issue2875(已知基线,本地 lib 双边皆过)。
- `[[ ]]` procsub 面不在本 PR 范围(卷宗 §8;待 #3444 合入后队长另立),不立案、不扩面。

## 本 PR 删除了什么
无删除:只改 `expand_case_pattern` 内的展开输入与 case wrapper 的 finish 调用;无公开面变动,无文件删除。
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3444 第1轮 — 建议:PASS
head: 305ce72 base(merge-base): c28a9dd 文件: 5 个 +403/-0
关联 issue: #3382(状态:open,我核过的时间:2026-11-15 09:0x CST)

机制核查:

  • take_conditional_arith_evalerror(conditional.rs):三面旗 expansion_error && fatal_error && !nounset 作为一条记录整体消费、置 evalerror_arith_pending 武装 DISCARD 类——对应 GNU expr.c evalerror→sh_longjmp→evalexp setjmp(expr.c:429-445)→ DISCARD 当前命令(eval.c:111),无跨命令错误态。两处 [[ ]] 边界(command_prepare.rs 包装、command_dispatch_late.rs Conditional 臂)均在 nounset 检查之后消费,FORCE_EOF 不降级,与注释一致。
  • ast_exec.rs DISCARD 吸收臂消费 evalerror_arith_pending:豁免随丢弃行终结(set -e 下后续 false 仍退出)——该消费对所有 DISCARD 源生效,属同机制必修面,非顺手扩大。
  • diff 逐文件通读:4 个 src 文件全部服务 [[ ]] 操作数算术致命泄漏到下一命令(#3287 同族:分派缺 abort 检查) #3382;无无关改动;无桥接/翻译层。
  • 可疑点:command_prepare.rs 中 arith 检查置于 [bug] Slice error inside [[ ]] does not abandon rest of line (GNU skips it; sibling of #1240) #1422 conditional_error 检查之前——conditional_expansion_error_status 在入口已 .take() 清空,早退不留残旗,顺序安全。

腿(逐项给数字与名字):

  • 钉自跑:stem issue3382_bracket_operand_fatal_leak(17 格)#![cfg(unix)] → 本机 running 0 tests,VOID;以逐字节探针等价补跑(target/probes-3444/probe_3444.py,期望式与钉文件 div_by_zero/unbound 格式逐字同,脚本绝对路径按 rubash /d/... 归一拼写):head 16/17 PASS。
  • fail-first:base c28a9dd 二进制同探针 → 6 passed / 11 failed,红名 = {canary,status1,andchain,sete,rightop,negated,double,nextbracket,nextarith,nextlet,nextassign};6 绿恰为钉注释自报的"pre-fix 已对"对照形(sameline/orchain/ifcond/sete-false/setu/assignthencond),逐名一致。
  • lib 双边:head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state}(1349/1/4)/ base {同}(1349/1/4)/ 差集 ∅(本机既有环境失败)。
  • -D warnings:RUSTFLAGS="-D warnings" cargo check --locked --lib --bins rc=0。
  • 卫生:rustfmt --check 5 个 PR 文件全 OK;marker-lint rc=0;utf8-hygiene clean。
  • CI:head 305ce72 — Rust tests(Linux)/GNU goldens/Compatibility CLI/Shell functionality/Windows compile/cross-target(linux-android)/API ledger/niubash builds 全 pass;Cross-target check (darwin) 与 Rust tests (macOS) 为 cancelled(job 级 conclusion=cancelled,时长 20m49s/22m20s,runner 超时 infra 抖动,非产品红)。Linux Rust tests 绿 = unix 钉 17 格实跑通过。
  • GNU 对照:WSL bash = GNU bash 5.3.20(1)-release(script-file 经 target/probes-3444/run_gnu.sh 逐格跑),17/17 格 rc/stdout/stderr 与钉期望一致(除脚本路径拼写随 argv 给形)。

F 点:无

续作记录:无

未跑/VOID 的腿与相邻残差:

  • cargo test --test issue3382_bracket_operand_fatal_leak 本机 VOID(cfg(unix));Linux CI 已实跑绿,探针等价 16/17。
  • 相邻残差(非本 PR 账,建议另行立项):sete 格在 Windows 侧 base 与 head 同形分歧——set -e + [[ $((1/0)) = x ]] 脚本退出(rc=1,无 after-e),GNU 为诊断后继续(rc=0)。同形存在于 base,系 [bug][regression] set -e: arithmetic expansion error exits the whole shell instead of discarding the current command (#1253) #1518 errexit 豁免面在 Windows 的既有缺口;unix 面钉已由 CI 证绿。
  • 正文 Refs #3382(只引用)合规;UNVERIFIED-BY-CAPTAIN 标注在,本报告即复核输入。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 15, 2026 02:06
@caomengxuan666
caomengxuan666 merged commit 9a5716a into master Nov 15, 2026
9 of 11 checks passed
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