Skip to content

fix(arith): diagnose empty-expanded subscripts in expand entries (Refs #3252) - #3913

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3252-arith-subscript-diagnostics
Nov 18, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3252-arith-subscript-diagnostics

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

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).

Verification: NOT RUN in this lane — all build routes are down
(Simular box hibernated, wallet empty; old server reachable only via
the box; X299 reverse tunnel refused on 2666; local VM builds
prohibited). Compile, fail-first, and the nail + lib/base legs are
owed; see REPORT ~/workspace/fix-3252/REPORT.md §7.

本 PR 删除了什么:

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

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 已逐名裁完:

自带钉一格红(必修)
issue3252_arith_subscript_diag_no_nounset::assoc_command_postinc_empty_subscript_lock(tests/issue3252_arith_subscript_diag_no_nounset.rs:386):(( C[$undef]++ )) 下钉期望 stderr 含一次 `C[]': not a valid identifier,实得 C[]: bad array subscript。其余 18 格绿。请以 GNU 5.3.20 oracle 实测裁定:若 GNU 的措辞确为 bad array subscript,则钉期望错了,改钉并附 oracle 证据;若 GNU 确为 not a valid identifier,则产品诊断措辞要改。注意这与 #3833 的教训同款(钉把错误期望写死),先验 oracle 再动手。

其余

  • census 78:除自带钉外零新增;goldens 基线二名;CLI 全绿。Rust tests 全绿。
  • 基线(13:19)在 #3901/#3902/#3916 之前:修钉后请 rebase 到现尖 e39c44d 再推,队长按合并形态重裁。

…#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).

Verification: NOT RUN in this lane — all build routes are down
(Simular box hibernated, wallet empty; old server reachable only via
the box; X299 reverse tunnel refused on 2666; local VM builds
prohibited). Compile, fail-first, and the nail + lib/base legs are
owed; see REPORT ~/workspace/fix-3252/REPORT.md §7.

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

UNVERIFIED-BY-CAPTAIN
…#3252)

GNU 5.3.20 on `(( C[$undef]++ ))`: `C[]: bad array subscript` x2 + `` `C[]': not a valid identifier `` x1, rc 1, and no binding is created. rubash resolved the empty bracket eagerly to Key(""), losing the token: reads reported only the two badsub lines and ++ silently bound C[""]. Keep the name[] TOKEN as a new EmptyBracket variant and map it to ArithLValue::InvalidElement at both match points (the indexed a[] shape): reads report badsub x2 via lvalue_value, the bind reports invalid-id via set_lvalue. GNU anchors: expr.c:1183/1224 (reads), expr.c:1052 -> variables.c:3403 sh_invalidid (bind), arrayfunc.c:1398.
@caomengxuan666
caomengxuan666 force-pushed the fix/3252-arith-subscript-diagnostics branch from 1526771 to c5c0958 Compare November 18, 2026 17:12
@caomengxuan666

Copy link
Copy Markdown
Member Author

队长续作:续作车道的修复设计已由队长应用并推送。新头 c5c0958(rebase 到 f983851 后 + fix 提交):lvalue.rs 一文件,EmptyBracket 变体 → InvalidElement(读报 badsub ×2、绑定报 invalid-id,indexed a[] 同形);rustfmt --check 通过。服务器腿(基底 fail-first、修后 19/19、lib 对齐、-D warnings)待链路恢复后补跑;头 CI 出后队长再裁。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 18, 2026 18:32
@caomengxuan666
caomengxuan666 merged commit 5880bd4 into master Nov 18, 2026
9 of 12 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 18, 2026
## 根因

`exec` 替换当前 shell,而不是开启新一层,所以 GNU 在 exec 之前把 SHLVL 减 1;rubash 把 SHLVL 原样透传给被 exec 的程序。

复现:`SHLVL=5 exec /bin/sh -c 'echo $SHLVL'` → GNU `4`,rubash(修前)`5`。

## GNU 锚点

- `builtins/exec.def:193-201`:`if (cleanenv == 0 && (subshell_environment & SUBSHELL_PAREN) == 0) adjust_shell_level (-1);`,随后 `maybe_make_export_env()` 重建导出环境,再 `shell_execve`;exec 失败时 `adjust_shell_level (1)` 恢复(exec.def:~235)。
- `variables.c:848-890`(`adjust_shell_level`):SHLVL 缺失/空/非数字按 0 计;新值 = 旧值 + change,下限钳制到 0;≥ 1000 时警告并重置为 1。
- 另有 `execute_cmd.c:5813-5817`(nofork 尾部路径)用同一调整,与之对应的是已有的 `exec_replace_adjusted_shlvl`(#1497/#1832)。

## 改动(只限 `src/builtins/exec.rs`)

- `external_exec_process`(三路调用点的唯一 choke 点:真替换 `replace_process_image` + 两个模拟 spawn)新增 `in_subshell_paren` 参数;在 `!clean_env` 时把 `SHLVL` 盖章为减 1 后的值(缺失/空/非数字按 0,下限 0)。shell 自身的 SHLVL 绝不动,失败恢复因此是天然 no-op。
- 子 shell 判定:rubash 没有 SUBSHELL_PAREN 位,用 `subshell_login_cleared` 闩锁(`( )` 区域入口:ast_exec 平坦区、compound_exec 作用域子 shell、comsub 体内被剥离的 `( )` 组)近似;普通 `$( )` 不置位,后台 `&`/procsub 重生子进程从全新 executor 启动故不置位。真替换路径因 `exec_image_replacement_applies` 要求 `subshell_depth == 0` 而不可能处于子 shell,传 `false`。
- `exec printenv` 内部仿真同步报调整后的 SHLVL(含原本未导出/缺失时,adjust 的 bind + auto-export 语义)。
- 回归钉:`tests/issue3310_exec_shlvl_decrement_regressions.rs`(21 个用例:seed、top/env/bash、0、缺失、unset、非数字、`exec -c`、paren、comsub、普通 spawn、brace 组、未导出、前缀赋值、printenv 各面)。

## 已知残差(未钉死,见 REPORT)

- 管道元素面:`SHLVL=5 exec ... | cat` GNU 为 4;本修法用的闩锁在管道元素上也被置位(pipeline_exec.rs),故仍跳过递减、得 5。彻底修需要一个真正的 stage 标记,不宜用 fd 形状启发式硬凑,留待后续单处理。
- `adjust_shell_level` 的 ≥ 1000 警告并重置为 1 一臂未复现——它在同族端口(`initialize_shell_level`、`exec_replace_adjusted_shlvl`)里同样缺失,单面加警告会分裂拼写,留待全族统一处理。

## 队长续作(122b23c8):钉期望对齐 GNU

续作车道交付物缺失(无工作区、无 REPORT、无推送),队长从其会话记录找回调查结论并亲手复核:钉的 `plain_spawn_keeps_level` 一格期望 `SHLVL=6` 是 GNU-错的——队长以 GNU 5.3.20 在钉的精确 harness 形态(`rubash -c <script>`,env_clear + PATH + SHLVL=5)下实测:`-c` 形子进程得 5,脚本文件形得 6,live 均为 6。产品合 GNU,钉错。本提交仅改该格期望为 5 并附 oracle 证据注释(+8/−1)。见 fix-3310b/REPORT.md。

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

census 79,自带钉 21/21 绿;新增两格皆为既判抖动族(issue1902 fd-alias、issue2883 read 提示),本 PR 的 test-only 改动执行不到该面;goldens 基线二名;CLI 全绿;macOS 全绿;其余全绿。合入时基底 f983851 与 #3913 合入点 5880bd4 零交集(程序化核验),故未 update-branch。服务器腿(lib 双边、-D warnings)待链路恢复后补跑;合入依据为上述已裁定的头 CI。

Refs #3310

## 本 PR 删除了什么

什么也没删除:只新增一个 `SHLVL` 盖章分支、一个 `in_subshell_paren` 参数、一个 9 行 helper 与 21 个回归钉;无文件删除,无公开面增减(`docs/api-ledger.md` 未动),无行为删除。
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