Skip to content

test: add fail-first regression pins for indirect expansion on name 0 (Refs #3784) - #3795

Merged
caomengxuan666 merged 5 commits into
masterfrom
fix/3784-indirect-expansion-zero
Nov 17, 2026
Merged

caomengxuan666 merged 5 commits into
masterfrom
fix/3784-indirect-expansion-zero

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

根因

rubash #3784:${!...} 间接展开对名字 0 的两面错误。

  • 面 1:${!0} / ${!00} 应间接经过 $0 的值(shell 路径,非合法变量名),GNU 报 <$0>: invalid variable name、rc 1、无 stdout;rubash 现静默展空、rc 0。
  • 面 2:${!#} 应展为最后一个位置参数,$# 为 0 时即 $0 本身;rubash 现展空。

GNU 锚点

bash 5.3 subst.c(vendored tree):

  • parameter_brace_expand_indir(subst.c:7883):t 来自 parameter_brace_find_indir(subst.c:7839),即展开源参数本身——名字 0/00 对应 $0 的值,# 对应 $# 的值(无参数时为 "0",即 $0)。
  • 无效 t 由 valid_brace_expansion_word(subst.c:7583)判定,报 <t>: invalid variable name(subst.c:7947);该错误对非交互 shell 是致命的(subst.c:4296 exp_jump_to_top_level)。
  • $0 为合法名字时(BASH_ARGV0 导入,variables.c:1547-1555),${!0} 读该名字对应的变量。

本 PR 删除了什么

无删除。本 PR 只新增回归钉文件 tests/issue3784_indirect_expansion_zero.rs(17 个测试,覆盖两面 + 边界对照);产品代码未动。

状态

UNVERIFIED-BY-CAPTAIN — 钉为 fail-first,待 X299 构建验证后上修法。

Refs #3784

… (Refs #3784)

GNU anchors (bash 5.3 subst.c): parameter_brace_expand_indir
(subst.c:7883) resolves t via parameter_brace_find_indir
(subst.c:7839); an invalid t reports '<t>: invalid variable name'
(subst.c:7947). ${!0}/${!00} indirect through $0's value (the
shell path); ${!#} with $#=0 resolves through name "0", i.e. $0.

UNVERIFIED-BY-CAPTAIN
@caomengxuan666

Copy link
Copy Markdown
Member Author

【Y3-AUDIT】PR #3795 第1轮 — 建议:PENDING(缺修法腿:钉已验证合格且 fail-first 有效,但产品修法未落,合入会带进 10 条新红)
head: 3d371fb base(merge-base): c3bf91e 文件: 1 个 +231/-0
关联 issue: #3784(open)

机制核查:

  • GNU 锚点属实:subst.c:7839 parameter_brace_find_indir 取展开源参数本身(0/00→$0 值、#→$# 值,无参时 "0");subst.c:7883 parameter_brace_expand_indir;subst.c:7583 valid_brace_expansion_word 判非法名;subst.c:7947 报 <t>: invalid variable name;subst.c:4296 非交互 fatal jump;variables.c:1547-1555 BASH_ARGV0 使 $0 成合法名。
  • diff 仅 +tests/issue3784_indirect_expansion_zero.rs(231 行),产品代码未动——与正文「只新增回归钉…UNVERIFIED-BY-CAPTAIN…待修法」自述一致,非虚报。
  • 无无关改动;无桥接层;标题/正文/commit 仅 Refs #3784,无关单词+编号同句。

腿:

  • 钉自跑(head = base + 测试文件,产品树与 merge-base 一致):issue3784_indirect_expansion_zero 7 passed / 10 failed
    • 红名 = {bang_zero_bare_errors, bang_zero_zero_bare_errors, bang_zero_zero_default_errors, bang_zero_default_errors_inside_subshell, bang_zero_valid_name_expands_target, bang_zero_zero_valid_name_expands_target, bang_zero_default_valid_name_applies_operator, bang_hash_no_args_is_dollar_zero, bang_hash_pattern_no_args_is_dollar_zero, bang_hash_pattern_with_args_is_last_arg}——恰为 issue 两面(${!0}/${!00} 非法名诊断 + ${!#}/${!#%*} 的 $0 回退/末参)未实现行为
    • 绿名 = 7 个控制格(${!#} 有参=末参、${!n}、${!01-unset}→$1、${!?}=$0、${!1}/${!5} 空——与 GNU 同空的边界对照)
  • fail-first:merge-base 无此文件;产品代码 diff 为零 → head 红即 base 红(构造性等价)
  • lib 双边:diff 不含 src/ → 与 base 完全相同,差集 {}(构造性成立)
  • -D warnings / fmt / 卫生:测试文件经 cargo test 编译无警告;rustfmt --edition 2021 --check rc=0
  • CI:head 3d371fb 各任务状态以 gh pr checks 为准(钉红属预期 fail-first;产品零改动不引入新账)
  • GNU 对照:WSL bash 5.3.20(1)-release,脚本 target/devin-dispatch/probes/gnu-3784.sh + gnu-3784-err{,2,3,4}.sh:
    • set -- a b c; echo "[${!#%*}]" → [c] ✓(钉期望一致)
    • BASH_ARGV0=target; target=hello; echo "[${!0-unset}]" → [hello] ✓
    • 裸 ${!0} / ${!00} / ${!0-word}(含子 shell 内)→ stderr <脚本>: line N: <脚本>: invalid variable name,stdout 空,该命令 rc=1、脚本继续——钉断言(含 invalid variable name、含 $0 值、不含 bad substitution、code 1)逐条吻合
    • ${!#}/${!#%*} 无参=$0(前一轮探针已核)

钉质量专项:断言分层正确(stdout 空 + 诊断词 + $0 名 + 排除 bad substitution + rc);控制格覆盖「GNU 同样为空」的边界,非假绿形态;17 钉与 issue 两面一一对应。

F 点:无(钉本身)

未跑/VOID 的腿:lib 双边与独立 base fail-first 构建(diff 仅 +tests/ 文件,构造性等价)

…ails (Refs #3784)

GNU parameter_brace_find_indir (subst.c:7839-7881) resolves every source
kind to `t` before the operator applies (subst.c:7883-7952), and a
positional source resolves through the parameter itself:

- all-zero spellings (`${!0}`, `${!00}`, `${!000}`) name `$0` — the
  script name — never positional_window[0] and never a variable lookup
  (the leading-zero guard treated `00` as a NAME, so it silently
  expanded empty instead of reporting `<t>: invalid variable name`);
- any other leading-zero text (`${!010}`) stays a variable name, as GNU
  does (empty when unset), so the guard is now `index == 0` rather than
  a raw `== "0"` string test;
- `${!#%pat}` resolves the `#` holder through indirect_holder_target_name
  (count -> `0` with no arguments -> script name) instead of requiring a
  plain shell name, so the removal applies to `${0%pat}` / `${N%pat}`;
- the error scan and the operator resolution share the same rule.

Partial: `issue3784_indirect_expansion_zero` 14/17.  Still open:
`${!00-unset}` (the `-` operator arm never routes through the indirect
target) and the quoted `"${!#%*}"` argument shape (unquoted form is
correct) — both documented on the PR.

GNU oracle: WSL bash 5.3.20(1) script files (target/probe3784.sh,
target/p7.sh, target/p8.sh) for the leading-zero and holder shapes.
@caomengxuan666

Copy link
Copy Markdown
Member Author

【o1 收网】判词 PENDING(14/17,剩 2 个根因):新 commit 3b15a4781 已推 origin(不改写历史,git ls-remote 核 head)。

head 3b15a47(父 = 3d371fb + 合入 origin/master);关联 #3784 保持 OPEN。

已修(GNU 锚点:subst.c:7839-7881 parameter_brace_find_indir → :7883-7952 parameter_brace_expand_indir)

  • all-zero 位置源 = $0:${!0}/${!00}/${!000} 一律按 $0(BASH_ARGV0 优先,variables.c:1547-1555)解析 t;原实现 starts_with('0') && != "0" 把 00 当变量名,静默展开空且不报错。现改为 index == 0 判定,${!00} 报 <t>: invalid variable name rc 1(与 ${!0} 同形)。
  • 前导零规则保持 GNU:${!010}/${!07} 仍按变量名(未置位 → 空),不被当位置号。
  • ${!#%pat} holder 解析:indirect_pattern_removal 不再要求 ref_expr 是 shell name,改走 indirect_holder_target_name(# → 计数 → 无参时 0 → $0;? → 状态;@/* → join;数字 → 位置;named → 值)。GNU oracle:无参 ${!#%*} = $0,三参 = c。
  • 错误扫描与求值两条腿共用同一 all-zero 规则(parameter_expansion_error_in_word_context 的数字源分支 + indirect_parameter_operator_value)。

仍红 3 格(2 个根因,未修,已定位)

  1. bang_zero_zero_default_errors:${!00-unset} 仍输出 unset。值路径已把 t 解析成 $0(裸 ${!00} 已正确报错 rc 1),但 -/:- 词算子臂(expand_braced_ops if let Some((var_name, word)) = split_once_outside_subscript(name, '-'))直接用 parameter_operator_value("!00"),没有走 indirect 目标解析;GNU 是先 find_indir 解 t 再对 ${t<op>} 重派发(subst.c:7955-7961)。正确修法:has_indirect_parameter_word_operator 命中的 form 统一重写成 ${t<tail>} 再进算子族(不是给 - 臂加个案)。
  2. bang_hash_pattern_no_args_is_dollar_zero / bang_hash_pattern_with_args_is_last_arg:形如 [ "${!#%*}" = "$0" ] 的引号参数位展开为空,而 x=${!#%*}、echo "${!#%*}"、printf "<%s>" "${!#%2}" 均正确(无引号参数位也正确)。即双引号参数位的 ${<indirect>%*} 走了与赋值/echo 不同的分支并把值清空;%2/%"*" 不复现,%* 才复现 → 与 * 作为模式的 quoted-pattern/silent gate 相关。

腿

  • 钉 issue3784_indirect_expansion_zero:14 passed / 3 failed(Windows;失败名如上)
  • GNU oracle:WSL /usr/local/bin/bash 5.3.20(1) 脚本文件 target/probe3784.sh、target/p7.sh、target/p8.sh,逐字节核过 ${!00}/${!000}/${!007}/${!010}/${!#%*} 五形
  • fmt:涉改 3 文件 rustfmt --check 通过;-D warnings 未单跑(本轮以 build 为准)
  • fail-first:未单跑(base 3d371fb 的钉为 17 格新增,PR 原文已给首跑红名)

未跑/VOID:lib 双边、warnings、CI 逐格、剩余 3 格的修复。判 PENDING:钉未全绿,不合入。

… (Refs #3784)

parameter_brace_find_indir (subst.c:7839) expands the source through
parameter_brace_expand_word, whose valid_number branch (general.c:248 ->
subst.c:7684) reads get_dollar_var_value (subst.c:3707): index 0 is $0,
never positional $1. `00`/`000` are numerically 0 and name $0 too, and
leading zeros are otherwise insignificant (`x=010; ${!x}` is $10 — GNU
5.3.20 probe). The bare `${!0}` path used positional_window[index-1], so
it read $1 (usually empty); the same source feeds every operator through
get_var_and_type (subst.c:8529), so pattern removal, case modification,
substring, `:=` binding and the subst.c:10164 nounset bound check now
resolve the holder first and share one numeric_parameter_value lookup.

Also gate the `${!prefix*}`/`${!prefix@}` name-list fast path on
legal_variable_starter (subst.c:9974): `${!#%*}` is the `%` operator on
`${!#}`, never a variable-name list.

Tests: tests/issue3784_indirect_expansion_zero.rs (29 pins) plus the
indirect/parameter/array regression files.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 17, 2026 08:29
@caomengxuan666
caomengxuan666 merged commit 2c24215 into master Nov 17, 2026
9 of 12 checks passed
@caomengxuan666

Copy link
Copy Markdown
Member Author

【o1 收网验证 · 建议 PASS】head b6365011a

接手时远端已从审计时的 3d371fbb 前进到 b6365011a(含 3b15a4781/b6365011a 两条产品修法 + 两次 master 合并);按规程未覆盖远端历史,直接复核该 head(本地另做的等价修法作废、未推)。

机制核查(GNU 锚点):

  • Face 1:parameter_brace_find_indir(subst.c:7839-7881)展开的是源参数本身,digit 0 得到 $0 文本,再按参数引用重展开;非法 t 报 <t>: invalid variable name(subst.c:7947,非交互致命)。head 的 expand_braced_special / 预扫描 / indirect_parameter_operator_value 三处均以 $0 起步。
  • Face 2:${!#} 的 t 是计数文本 "0",即 $0;%/@ 等尾运算符作用于 t 所指参数。head 的 # 分支走 parameter_error_value(count);${!prefix*}/${!prefix@} 名单形式要求 prefix 是合法变量名(subst.c:9978-10007),!#%* 不会被误判为名单。

腿:

  • 钉 issue3784_indirect_expansion_zero 29/29(含 head 新增的 12 格)
  • fail-first:base 3d371fbb(钉 17 格版本)6 failed / 11 passed;红名 = bang_hash_pattern_、bang_zero_valid/zero_default/zero_zero_,与本次修复面一致
  • GNU 对照(WSL GNU bash 5.3.20,脚本文件):23 形状矩阵(bare/运算符 × $0 合法/路径形 × !# 0/1/3 参数 × %/@Q///:- 尾 × 未置位/置位)归一化后逐字节一致;正例另用 -c ... target 让 GNU 的 $0=target 复核 → A=[hello] B=[hello] C=[hello] 与 rubash 同形
  • lib 双边:head 仅 api::consumer_tests::issue2084_session_end_restores_host_process_state(本机 __RUBASH_CURRENT_LINE 注入,环境绑定,CI 绿)
  • 相邻切片全绿:issue1147 2/2、2608 5/5、2649 12/12、2712 11/11、1566 12/12、529 8/8、439 11/11、371 9/9、3022 13/13、3501 68/68
  • RUSTFLAGS=-D warnings cargo check 净;PR 改动 9 个源文件 rustfmt 全净
  • CI(run 44047201473)归因:Rust tests 红 = scripts/check-utf8-boundary-hygiene.sh 报 src/executor/strmatch.rs 5 处(来自 master commit 9211151cd fix(glob): export strmatch for loadables and route GLOBIGNORE through it (Refs #3605) #3630,基线红,非本 PR);census PR 80 vs master 同窗口 run 44045193308 84,PR-only 仅 indexed_star_at_a_quoted_is_one_word(登记 flake);goldens 两边同为 golden_issue1230_exec_redirect_undo + issue_753::case_9 + upstream_suite_slice_snapshots(基线)
  • 未跑腿:WSL Linux 原生构建(改动为纯参数解析面,unix-gated 套件无关)

关联 #3784:本 PR 覆盖该 issue 两个 face 的全部形状(${!0}/${!00} 致命 + 有效 $0 正例 + ${!#} 空参回 $0 + %/@ 尾),合入后关单;伞 #3605 保持 OPEN。

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