Skip to content

fix(arith): trap top-level $ reads under set -u before expansion erases them (Refs #3251) - #3465

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3251-arith-dollar-nounset-bypass
Nov 15, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3251-arith-dollar-nounset-bypass

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

根因

GNU expand_arith_string(subst.c:3981,经 execute_cmd.c:3936 / eval_arith_for_expr :3171)在展开阶段即捕获未绑定的 $name 读数(subst.c:11023 unbound_variable: → err_unboundvar),但本仓的内嵌参数展开把该读数静默展为空,致使展开后的 nounset 扫描永远看不到名字,(( $undef )) 在 set -u 下静默 rc 0。

修法

在任何展开步骤抹除名字之前,先对原始表达式做一次 $" 读数预扫描,沿用 #3199 下标扫描的 param_expand 规则(first_unbound_dollar_read),把按文本顺序最左的陷阱记入 #3199 S2 槽,由既有扫描点优先上报。下标内的单引号段先掩掉:其内是字面 key(b['$undef']应为 operand error 而非 nounset),而顶层的单引号在算术文本里只是数据,其内的$" 读数仍应捕获。裸 $N" 位置参数按 GNU 渲染带 $(subst.c:10500-10512 uerror),与 ${N}`(裸数字)区分。

仅作用于展开型入口((( ))、$(( ))、arith-for);let" 操作数已展开,$name" 在其内仍是 evalexp 数据,不动。

GNU 锚点

  • subst.c:3981 expand_arith_string;subst.c:11023-11027 unbound_variable;subst.c:10500-10512 positional uerror;expr.c:1190-1216 expr_streval;execute_cmd.c:3936 / :3171

验证(进行中)

  • GNU 5.3.20 oracle 27 格 + 追加探针:除嵌套 $(( )) 框内陷阱(c13/p01/p02/s04-s07/d02-d05/x02,嵌入式纯求值器吞掉框内失败,续修中)与既有残留(c12 let"、c14/c26 :?`、q02 单引号下标键、s01 单引号裸名,待 base 对照确认是否既有)外,其余与 GNU 逐字一致。
  • 回归钉 tests/issue3251_arith_dollar_nounset_regressions.rs 待补;lib 全量、-D warnings、hygiene 待跑。

UNVERIFIED-BY-CAPTAIN

本 PR 删除了什么

无删除。仅新增算术 nounset 预扫描与 $N 诊断渲染修正;无公开 API、文档或行为删除。

Refs #3251

…es them (#3251)

GNU expand_arith_string (subst.c:3981, via execute_cmd.c:3936 /
eval_arith_for_expr at :3171) traps an unbound $name read during
expansion (subst.c:11023 unbound_variable: -> err_unboundvar), but
this tree's embedded-parameter walk expands the read to empty
without trapping, so the post-expansion nounset scan never saw the
name and (( $undef )) went silent (rc 0).

Scan the RAW expression before any expansion pass erases it, with
the same param_expand rules as the #3199 subscript scan
(first_unbound_dollar_read), and record the leftmost trap in the
#3199 S2 slot the existing scan point consults first. Single-quoted
spans inside subscripts are masked out first: they are literal keys
there (b['$undef'] is an operand error, not nounset), while a
single quote at the top level is data in arith text and the $ read
inside it still traps. A plain $N positional renders with the $
(GNU subst.c:10500-10512 uerror), unlike braced ${N} (bare digit).

Applies to the expanding entries only ((( )), $(( )), arith-for);
let operands arrive already expanded, so a $name there stays
evalexp data.

GNU anchors: subst.c:3981 expand_arith_string, subst.c:11023-11027
unbound_variable, subst.c:10500-10512 positional uerror,
expr.c:1190-1216 expr_streval, execute_cmd.c:3936/3171.

Refs-only: #3251. UNVERIFIED-BY-CAPTAIN.

本 PR 删除了什么:无删除。仅新增算术 nounset 预扫描与
$N 诊断渲染修正;无公开 API、文档或行为删除。
…3251)

The #3251 raw-text scan skipped every `$(...)` span, on the claim
that each is a separate frame whose own machinery traps inside it.
That holds for a `$(...)` command substitution (the subshell traps),
but not for a nested `$((...))` arithmetic expansion: the enclosing
expansion expands it inline (GNU subst.c:3981 expand_arith_string
recurses into it), and no separate entry ever scans it — so
`(( $(( $undef )) ))` went silent (rc 0) even after the first fix
(probe c13). Recurse first_unbound_dollar_read_arith into the inner
text of a `$((` span (re-masking first, so a quoted subscript key
inside the nest stays literal); `$(...)` is still skipped. The doc
comment's "separate frame" claim for `$((...))` is corrected with it.

Regression nails: tests/issue3251_arith_dollar_nounset_bypass.rs,
32 tests (26 trap cells + 6 controls) over the lane's probe matrix
(c01-c27/q01-q15/x01; c13/x01 exercise the recursion; c11 omitted,
already nailed by the #3199 pin). GNU anchors: subst.c:3981/11023/
10500-10512, execute_cmd.c:3171/3936, expr.c:1150.

Verification this round: rustfmt --check clean on both touched
files; marker-lint OK; utf8-boundary hygiene clean; GNU anchor line
numbers re-verified. NOT run (environment-blocked, see REPORT.md
§8-§9): nail double-sided, GNU three-way re-probe, full lib
double-sided, RUSTFLAGS="-D warnings" cargo check — local write
wall intermittent (EDQUOT), Simular box disk 100% with its shell
channel down, old-server route with it, X299 direct not yet
available. Deferred to the captain review round on X299.
UNVERIFIED-BY-CAPTAIN.

Refs-only: #3251. 本 PR 删除了什么:无删除。仅新增嵌套算术
扫描递归与 32 个回归钉;无公开 API、文档或行为删除。
@caomengxuan666

Copy link
Copy Markdown
Member Author

Captain review — FAIL (rework needed)

Verified the PR head a163fbf1 itself (merge-base e50cea96, computed with git):

1. The nested-arith cell is still red. Against the lane's 32-test nail
(overlaid from the local commit, since the PR ships no nail — see 2), head is
31/32 with the sole failure arith_command_nested_arith_dollar_unbound
(c13): set -u; (( $(( $undef )) )) stays silent (rc 0) where GNU aborts
(rc 127, undef: unbound variable). Base is 12/32 with 20 fail-first reds, so
the fix works for the (( )) entry — but the nested $(( )) recursion is
missing from this head. The local rework commit c71bf551 ("recurse nounset
scan into nested $(( ))") exists on the lane branch but was never pushed;
please push it (or an equivalent) so c13 traps like GNU.

2. The PR contains no regression nail. a163fbf1 touches exactly one file
(src/executor/arithmetic/mod.rs, +134/−1). Please include
tests/issue3251_arith_dollar_nounset_bypass.rs (32 tests) in the PR.

What passed on this head: hygiene trio all green on a163fbf1
(cargo fmt --check, check-marker-literals.py, check-utf8-boundary-hygiene.sh);
GNU anchors re-verified against bash 5.3 source (subst.c:3981, 11023-11027,
10500-10512, execute_cmd.c:3171/3936, expr.c:1150/1190-1216).
CI on this head: 8 pass; the 3 reds are issue2859 on Linux Rust tests
(jobs/timing — base CI at e50cea96 is green, so not proven pre-existing; the
captain will settle it with a lib bilateral on the reworked head) and
issue2875 on macOS + goldens (known missing---bin rubash CI trap — void,
not a product verdict).

Owed on the reworked head (blocked this round by the local write wall,
battery round all-fail):
full lib bilateral, RUSTFLAGS="-D warnings" check,
three-way GNU probe re-run (incl. q02, whose earlier log predates the
subscript-quote mask), CI re-read. The §6 residuals (:? operator c14/c26,
let c12, q02, x02) should be filed as separate issues (:? first).

Rework: (1) push the c13 recursion fix, (2) add the 32-test nail file,
then the captain re-verifies on the new head. Full evidence:
CAPTAIN-VERIFY.md in the review dossier.

@caomengxuan666

Copy link
Copy Markdown
Member Author

Update: the rework requested in the review above has landed — head is now c71bf551 (nested-$(( )) recursion + the 32-test nail in the PR). The FAIL above refers to the superseded head a163fbf1 and is moot. Re-review of the new head is in progress (X299 legs banked: pin 32/32, lib name-set empty); Linux legs and new-head CI characterization are outstanding.

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】

PR #3465 第1轮 — 建议:PASS
head: c71bf55 base(merge-base): e50cea9 文件: 2 个 +570/-6
关联 issue: #3251(状态:open,我核过的时间:本地当前)

机制核查:

  • 根因正确:旧扫描在 $ 后遇 ( 一律 skip_arith_paren_span,把 $(( )) 嵌套算术当成独立 frame 跳过,但 GNU subst.c:3981 expand_arith_string 对嵌套 $(( )) 是内联递归展开的(execute_cmd.c:3936 execute_arith_command / execute_cmd.c:3171 eval_arith_for_expr 均先经 expand_arith_string),嵌套里的 $name 读取同属本层展开序列。新代码判别 $( ( 双括号则剥壳递归扫描内层文本(先跑 subscript 单引号掩码,保持 b['$undef'] 键字面性),外层仍按 param_expand 规则走。
  • 顺带修正位置参数诊断拼写:$1 未绑时报 $1: unbound variable(GNU subst.c:10500-10512 param_expand uerror = "$"+digit),braced ${1} 仍报裸数字——与 GNU 实测一致。
  • 新增 mask_arith_subscript_single_quoted_spans:只掩下标内单引号段(arrayfunc.c:1368-1378 array_expand_index 把下标整体展开,'...' 保住键),顶层单引号不掩(GNU 视算术文本如双引号,'$undef' 顶层照样 trap,q01)——类别级判别,非症状补丁。
  • 无关改动:无(仅 arithmetic/mod.rs + 钉文件)。
  • 队长上轮 c13 FAIL 点(set -u; (( $(( $undef )) )) 静默)已在 head 修复:钉中 arith_command_nested_arith_dollar_unbound 过,实跑见下。

腿(逐项):

  • 钉自跑:issue3251_arith_dollar_nounset_bypass 32/0(head,Windows,全部通过)。
  • fail-first:base e50cea9 放同钉 → 12 passed / 20 failed。红集={assign_rhs, assoc_subscript, assoc_subscript_empty_array, bare, bare_plus, braced, braced_positional, cmdsubst_plus, dollar_unbound_aborts, double_quoted, guarded_word, guarded_word_positional, leading_beats_subscript, nested_arith(=c13), nested_leading_wins, positional, single_quoted, subscript_positional, ternary, two_unbound_first_wins},与「base 完全没有 raw-text 扫描机制」一致;c13 在 base 红、在 head 绿——队长上轮唯一失格项已消除。
  • lib 双边:head 失败名集 {issue2084_session_end_restores_host_process_state} / base 同集 / 差集 ∅(环境性基线,两树同复现)。
  • -D warnings:lib check 干净 0 警告;--tests 面撞既有无关 dead-code(tests/issue2302_bash_source_bare_name_regressions.rs,cfg(unix) 测试在 Windows 下 helper 全 dead;归属 commit 2630e0e,非本 PR 文件),本 PR 两文件 0 警告。
  • 卫生:rustfmt --check 两文件 FMT-OK;marker-lint OK(117 consts);utf8-boundary clean。
  • CI:红任务归因——Rust tests / Rust tests (macOS) / GNU goldens 三项均为已知基线 issue2875(日志实证 consumer_tests.rs:2911 Unrecognized option: 'c',nextest 27/1246 fail-fast 停,故 issue3251 stem 在 Linux CI 未跑到);其余 API ledger / compat CLI / cross-target×2 / shell tests / Windows compile / niubash build 全绿。
  • GNU 对照:WSL GNU bash 5.3.20(1)(/usr/local/bin/bash)。32 格逐格以 bash -c "$(cat f)" 复跑:26 格未绑读取均 rc=127 + <name>: unbound variable(含位置参数 $1: unbound variable 拼写、首个未绑者先报的两格次序断言);6 格 bound/对照均 rc=0 + AFTER 输出。补验:同形状跑脚本文件 rc=1(GNU -c 与脚本文件退出码路径不同,钉断言取 -c=127 正确)。c13 实测:set -u; (( $(( $undef )) )) → undef: unbound variable rc=127,与 head 一致。脚本:target/probes-3465/{run_gnu_c.sh,cases-gnu/}。

F 点:无(队长前轮 F 点 c13 已消除)。

续作记录:未动代码。

未跑/VOID 的腿:

  • Linux 侧钉 stem:CI 被 issue2875 fail-fast 截停未跑;钉已在 Windows 全绿 + GNU WSL 逐格验证,建议队长 Linux 车道抽跑 stem 复核即可,不构成阻塞。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 15, 2026 02:06
@caomengxuan666
caomengxuan666 merged commit dad4a6b into master Nov 15, 2026
8 of 11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 17, 2026
Pin only; the #3295 fix lane waits for PR #3465 to merge. Preserved as a draft per captain order so the pin is not held only in a local tree. UNVERIFIED-BY-CAPTAIN.

Co-authored-by: 曹梦轩 <[email protected]>
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