Skip to content

fix(expansion): slice special parameters in substring expansion (Refs #3063) - #3152

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3063-question-substring
Nov 9, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3063-question-substring

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #3063

问题

${?} 的子串展开在所有上下文返回空,坏偏移也静默返回空而不是 GNU 的算术错误;special 参数全族($、-、#、!、0、位置数字)在非引号词中同样切出空字符串。

  • false; echo "X${?:0:1}Y" — GNU X1Y,rubash XY。
  • false; echo "X${?:t5:h2:l}Y" — GNU rc 1 + ?: h2:l: arithmetic syntax error in expression (error token is ":l"),rubash XY rc 0 无报错。

根因(三层、一个机制)

GNU param_expand 在分发到 parameter_brace_substring(subst.c:9050,调用点 subst.c:10179)之前就用通用路径取好参数值(? 的值是 itos(last_command_exit_value),subst.c:8280),且 $? 的值永不为 NULL,subst.c:9057 的 unset 早退不适用,verify_substring_values(subst.c:8395)必然求值偏移算术。rubash 的子串机器从不咨询这个通用取值:

  1. 名字被三元规则吃掉:split_top_level_colon 把身为参数名本身的 ? 也计作三元标记。GNU 的 skip_to_delim 三元规则(subst.c:2262)只经 skiparith 作用于冒号之后的子串算术文本(subst.c:8407),名字在此之前已被提取。于是 ?:0:1 被拆成名字 ?:0,parse 死在名字门上,偏移根本没机会求值。
  2. callee 不自取 special 值:expand_braced_substring_parameter 自己不解析 special 参数名;引号/赋值调用点替它取了值,但普通词分发路径(expand_braced_indexed_parameter)没有,全族在该路径切空。同函数内 substring 臂原位于 # 前缀臂之后,而 # 臂声明所有 # 开头的名字并对非 length 形返回 None,${#:...} 永远到不了 substring 臂。
  3. 裸 ! 不被 parse 准入:parse_parameter_substring{,_mut} 的名字门只认 !name 间接形,不认作为 $! 本身的裸 !。

修法

  1. split_top_level_colon(parameter_core.rs):位于 0 号位且紧跟分隔冒号的 ? 不计作三元标记——它就是参数名本身。
  2. expand_braced_substring_parameter(expand_braced_indices.rs):在 callee 内经 expand_parameter_named_value 解析 special 参数名与位置数字(与 expand_braced_length_parameter 同形);expand_braced_indexed_parameter 的 substring 臂移到 # 前缀臂之前(parse 只准入裸 # 名字,${#arr[@]}、${#?}、${#v} 等 length 形仍原样落回后续臂)。
  3. parse_parameter_substring{,_mut}(parameter_core.rs):名字门在 !name 之外准入裸 !;未设的 $! 仍走 [bug] unset ${foo:t5:h2:l} errors arithmetically instead of short-circuiting to empty #3024 的 NULL 短路。

验证

  • 回归钉 tests/issue3063_question_substring.rs(22 格):基底 5 过 / 17 红(红格恰为缺陷格),修复后 22 过 / 0 红。
  • 邻茎 6 茎全绿(issue3024 / issue539 / issue1600 / issue758 / issue2343 / audit_b1,共 48 格)。
  • lib 头侧 1206 passed / 0 failed / 4 ignored,与基底 919562bf 的既定基线一致。
  • RUSTFLAGS="-D warnings" 的 cargo check --lib --bins 通过;cargo fmt --check 通过。
  • GNU 差分:7 份探针脚本(引号/非引号/赋值/comsub 上下文 × 全族 special 参数,含负偏移、负长度、超界、坏偏移报错形、$! 已设/未设两态)与 GNU bash 5.3.20 逐字节一致(stdout + 规范化 stderr)。

本 PR 删除了什么

删除了 expand_braced_indexed_parameter 底部一处被上移取代的重复 substring 分发臂(3 行);无其他删除。产品代码净 +52/−6(两文件),新增回归钉 326 行。

…3063)

`${?:0:1}` expanded empty on every path and bad offsets expanded
empty silently instead of reporting GNU's arithmetic error; the whole
special-parameter family (`$`, `-`, `#`, `!`, `0`, positional digits)
also sliced empty in unquoted words. Three layers of one mechanism —
the substring machinery never consulted the generic value fetch that
GNU param_expand performs before dispatching to
parameter_brace_substring (subst.c:9050, call site subst.c:10179; the
`?` value is itos(last_command_exit_value), subst.c:8280, and is never
NULL, so the subst.c:9057 unset early return never applies and
verify_substring_values, subst.c:8395, always evaluates the offsets):

1. split_top_level_colon counted the `?` that IS the parameter name
   as a ternary marker. The skip_to_delim ternary rule (subst.c:2262)
   runs via skiparith on the post-colon substring text only
   (subst.c:8407); GNU extracts the name first. `?:0:1` therefore
   split as name `?:0` and the parse died at the name gate, so offsets
   were never evaluated. A leading `?` immediately followed by `:`
   is now not counted as a ternary marker.
2. expand_braced_substring_parameter did not resolve special
   parameter names itself; the quoted/assignment call sites fetched
   the value on its behalf, but the plain-word dispatcher path
   (expand_braced_indexed_parameter) did not. The callee now resolves
   specials and positional digits through
   expand_parameter_named_value, mirroring
   expand_braced_length_parameter. In the same function the substring
   arm moved above the `#`-prefix arms, which claim every `#`-initial
   name and return None for non-length operands, hiding `${#:...}`.
3. parse_parameter_substring{,_mut} now admit a bare `!` name
   alongside the `!name` indirect form: it is the `$!` parameter
   itself, and GNU slices it like every other special parameter.
   Unset `$!` still short-circuits through the #3024 NULL test.

Pin: tests/issue3063_question_substring.rs, 22 cells — base
5 passed / 17 failed (red cells exactly the defect cells), fixed
22 passed / 0 failed. GNU differential: seven probe scripts covering
the family across quoted/unquoted/assignment/comsub contexts are
byte-identical to GNU bash 5.3.20, stderr included.
@caomengxuan666
caomengxuan666 merged commit c28e5d0 into master Nov 9, 2026
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