Skip to content

WIP: fix(expansion): stop assignment pre-scan expanding operator words (#1953) - #1958

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/1953-nested-default-blowup
Oct 28, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/1953-nested-default-blowup

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

WIP: #1953 nested ${a:-…} exponential blowup — freeze-exception audit fix

Fixes #1953

Root cause

The :=/= assignment pre-scan (apply_parameter_assignment_expansion_with_context, src/executor/parameter_words.rs) split every ${...} body on its first top-level =/:= and expanded the split's "value" before any apply arm validated the left-hand side. For the c14 construction ${a:-x=${a:-…}} — a :- default word carrying a literal x= segment plus a nested expansion — the split names a:-x as the "target" and the rest of the default word as the "value", so the pre-scan fully expanded the nested fragment and then discarded the result when every apply arm rejected the name. One extra full inner expansion per nesting level: depth 10 = 1778ms, depth 15/20/25 never finished (>20s, zero output) on the frozen tip, vs GNU bash 5.3.20 millisecond-class at depth 400.

The phantom evaluation is observable without nesting too: a command substitution inside an unselected default word ran twice (GNU: zero), and three times when selected (GNU: once) — the pre-scan passes plus the real pass.

GNU anchor: parameter_brace_expand (subst.c:9777) extracts the parameter name first and dispatches on the FIRST operator character (subst.c:9886-9917); parameter_brace_expand_word (subst.c:7663) expands an operator's word only when that operator selects it. An =/:= inside another operator's word is word text, never an assignment operator.

Fix (mechanism, not shape)

Both pre-scan arms now admit a split only when its left-hand side is syntactically an assignment target — plain shell name, subscripted name (the admission apply_array_element_parameter_assignment itself uses), or either behind the ! indirect prefix (parameter_assignment_target_form). Otherwise the fragment is not an assignment fragment and the pre-scan leaves it entirely to the real pass. +39 lines, one file. Genuine binds (scalar / element / associative / cross-fragment ordering) verified unchanged against GNU.

Verification (all expectations GNU bash 5.3.20)

  • Depth ladder on the exact c14 form: d10 1778ms → ~20ms class; d15/d20/d25 complete, output byte-identical to GNU (len=22/32/42/52).
  • Phantom probes: unselected word side effect 2× → 0×; selected 3× → 1×; both now match GNU.
  • New pins: tests/issue1953_nested_default_blowup_regressions.rs (6 tests: ladder 10/15/25, depth-100 polynomial-growth + absolute-cap, side-effect once/never, =/:= text in a default word is not a bind, genuine binds still apply).
  • Gates: lib baseline identity vs frozen tip, diffcorpus 306 ⊆ {ms10}, full regression tree-vs-tree, -D warnings — results land in the cluster REPORT and a follow-up comment here.

Known residual (NOT this PR)

${v=x:=y}: GNU binds v to x:=y; rubash produces the right expansion value but never binds (the pre-scan's := arm claims the fragment with an invalid composed name and returns before the = arm). Pre-existing on the frozen tip, unchanged by this PR — flagged for a separate issue.

…words (#1953)

The `:=`/`=` pre-scan (apply_parameter_assignment_expansion_with_context)
split every ${...} body on its first top-level `=`/`:=` and expanded the
split's "value" BEFORE any apply arm validated the left-hand side. For a
body whose real operator comes first — ${a:-x=${a:-...}} (the c14
construction: a `:-` default word carrying a literal `x=` segment plus a
nested expansion) — the split names `a:-x` as the "target" and the rest
of the default word as the "value", so the pre-scan fully expanded the
nested fragment and then discarded the result when every apply arm
rejected the name. Per nesting level that is one extra full expansion
of the inner fragment: depth 10 cost 1778ms and depth 15 never finished
on the frozen tip, while GNU bash 5.3.20 is millisecond-class at depth
400. The phantom evaluation was also observable without nesting: a
command substitution inside an unselected default word ran twice, and
three times when selected (pre-scan passes + the real pass), where GNU
runs it zero / one times.

GNU anchor: parameter_brace_expand (subst.c:9777) extracts the parameter
name first and dispatches on the FIRST operator character
(subst.c:9886-9917), and parameter_brace_expand_word (subst.c:7663)
expands an operator's word only when that operator selects it. An
`=`/`:=` inside another operator's word is word text, never an
assignment operator.

Fix the invariant instead of the symptom: both pre-scan arms now admit
a split only when its left-hand side is syntactically an assignment
target (plain shell name, subscripted name — the admission
apply_array_element_parameter_assignment itself uses — or either behind
the `!` indirect prefix). Otherwise the fragment is not an assignment
fragment and the pre-scan leaves it entirely to the real pass. Genuine
binds (scalar, element, associative, cross-fragment) are unchanged.

Regression pins (tests/issue1953_nested_default_blowup_regressions.rs),
all expectations measured against GNU bash 5.3.20: depth ladder
10/15/25 byte-identical, depth-100 polynomial-growth + absolute-cap
sanity, word side effect runs exactly once when selected and never
when unselected, `=`/`:=` text inside a default word is not a bind,
and genuine assignment fragments still bind.
@caomengxuan666

Copy link
Copy Markdown
Member Author

门账汇总(同条件双边对照,工件 ~/workspace/fix-1953/logs/,全卷 REPORT 见工作区 fix-1953/REPORT.md):

  • lib:pristine 904/1/3 == 修复树 904/1/3,失败集逐名同(唯一 ownership_unary_operators_check_existing_file,既有基线)。
  • regression 全量:pristine 167/4 == 修复树 167/4,失败集逐名同(1887/1888 缺 vendored fixture 的环境项 + eco_completion + issue_759),零新增。
  • diffcorpus 306:修复二进制 306/306 全过、new_failures=[](工件 JSON 内含二进制与 oracle 绝对路径)。
  • -D warnings:钉 target 构建零警告;新钉 6/6 过。
  • 计时(同机交错中位数,debug profile):d5 13.2→8.7ms、d10 1036.5→9.1ms、d15 >20s 超时→8.4ms、d25 超时→10.3ms、d50 超时→17.1ms(GNU 参照 1.7–3.0ms);全梯输出与 GNU 逐字节同(len=2d+2)。注:confirm-a 的 d10=1778ms 为其负载窗数字,本窗同尖复测为 1036.5ms,形状一致。

残留一条(PR 正文已注):${v=x:=y} 真绑定缺失为冻结尖既有、与本条同源不同面,已在 REPORT §6 给出双边实证,请主会话另行立案。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 28, 2026 12:09
@caomengxuan666
caomengxuan666 merged commit ccbc848 into master Oct 28, 2026
11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/1953-nested-default-blowup branch October 30, 2026 01:41
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.

[bug] 嵌套 ${a:-…} 默认词展开指数爆炸:深度 15 不终止(GNU 深度 400 毫秒级)

1 participant