Repository navigation
fix(expansion): slice special parameters in substring expansion (Refs #3063) - #3152
Merged
Merged
Conversation
…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.
This was referenced Nov 8, 2026
caomengxuan666
marked this pull request as ready for review
November 9, 2026 03:40
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #3063
问题
${?}的子串展开在所有上下文返回空,坏偏移也静默返回空而不是 GNU 的算术错误;special 参数全族($、-、#、!、0、位置数字)在非引号词中同样切出空字符串。false; echo "X${?:0:1}Y"— GNUX1Y,rubashXY。false; echo "X${?:t5:h2:l}Y"— GNU rc 1 +?: h2:l: arithmetic syntax error in expression (error token is ":l"),rubashXYrc 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 的子串机器从不咨询这个通用取值:split_top_level_colon把身为参数名本身的?也计作三元标记。GNU 的skip_to_delim三元规则(subst.c:2262)只经skiparith作用于冒号之后的子串算术文本(subst.c:8407),名字在此之前已被提取。于是?:0:1被拆成名字?:0,parse 死在名字门上,偏移根本没机会求值。expand_braced_substring_parameter自己不解析 special 参数名;引号/赋值调用点替它取了值,但普通词分发路径(expand_braced_indexed_parameter)没有,全族在该路径切空。同函数内 substring 臂原位于#前缀臂之后,而#臂声明所有#开头的名字并对非 length 形返回 None,${#:...}永远到不了 substring 臂。!不被 parse 准入:parse_parameter_substring{,_mut}的名字门只认!name间接形,不认作为$!本身的裸!。修法
split_top_level_colon(parameter_core.rs):位于 0 号位且紧跟分隔冒号的?不计作三元标记——它就是参数名本身。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 形仍原样落回后续臂)。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 红。919562bf的既定基线一致。RUSTFLAGS="-D warnings"的cargo check --lib --bins通过;cargo fmt --check通过。$!已设/未设两态)与 GNU bash 5.3.20 逐字节一致(stdout + 规范化 stderr)。本 PR 删除了什么
删除了
expand_braced_indexed_parameter底部一处被上移取代的重复 substring 分发臂(3 行);无其他删除。产品代码净 +52/−6(两文件),新增回归钉 326 行。