Skip to content

fix(executor): bad-substitution diagnostic names the enclosing word for assignment-shaped words (Refs #3441) - #3442

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3441-bad-subst-diagnostic-word
Nov 13, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3441-bad-subst-diagnostic-word

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #3441 — UNVERIFIED-BY-CAPTAIN

根因

bad substitution 诊断把赋值形参数词的 RHS 片段当成所引的词:echo a=${-x} 报
${-x}: bad substitution,GNU 报 a=${-x}: bad substitution。

GNU 锚点(~/workspace/rubash-audit/build-gnu/bash-5.3/):

  • subst.c:10278(parameter_brace_expand 的 bad_substitution 缺省臂)报的是
    string——expand_word_internal 正在展开的整词;调用点 subst.c:10800。
  • expand_word_internal 从不对参数位置的赋值形词在 = 处切分再展开
    (W_ASSIGNMENT 只管 tilde,subst.c:11432 附近的 assignoff 跟踪)。
    只有真正的赋值语句才把 RHS 当独立词展开(subst.c:4357
    expand_string_assignment)——所以 x="a=${-x}" 与 x=a=${-x} 两侧同洁。

rubash 的错位在 expand_word_mut_with_context(src/executor/parameter_core.rs):
赋值形词被在 = 处切分,RHS 单独走 walker;walker 的 ${ 臂用
PendingDiagnosticWordGuard 把 walker 的词(即 RHS)记为 pending 诊断词,
展开期坏名门 take 该记录报出。一次本不存在的分词把诊断词重定根到了 RHS。

修法(机制级,两处)

  1. src/executor/parameter_core.rs:赋值切分臂(非 compound 分支)展开 RHS 前,
    用 PendingDiagnosticWordGuard::new(word) 把整词记为 pending 诊断词。
    Compound 分支刻意排除:GNU 在那里引元素词(a=([0]=${-x}) 报 [0]=${-x}),
    需元素边界跟踪(见 REPORT §6 观察 2)。
  2. src/executor/expand_word.rs:PendingDiagnosticWordGuard::new 改为
    install-if-absent——已有外层记录时不覆盖,否则 RHS walker 的 ${ 臂会把记录
    重写回 RHS。嵌套片段仍保自己的词:外层片段的门求值在任何嵌套算子词展开前就
    take 掉记录,算子词展开时槽已空。

回归钉

新 stem tests/issue3441_bad_subst_diag_word_regressions.rs(26 个 test):
19 个修法形(a=${-x}、a=b${-x}、a=${-x}b、a=${x[0]y}、
a+=${-x}、declare/printf/[[ ]]/for/case 等位置)+
7 个守卫形(${-x}、pre${-x}post、a=${x[}、${x:-a=${-y}}、
a=${x:-${-y}}、x=a=${-x}、合法 ${-} 静默)。

Fail-first:同一断言复刻为 shell harness 跑在修前二进制上,6 PASS / 19 FAIL
(19 个修法形全红、所引词恰为 issue 所报的片段形;6 个守卫形全绿)。
修后绿跑待复核轮(本机禁构建)。

本 PR 删除了什么

什么也没删除。只改两处:PendingDiagnosticWordGuard::new 的安装语义
(replace → install-if-absent)与赋值切分臂新增一个 guard 绑定;无公开面变动,
无文件删除,无行为删除。

验证状态

  • 已做:rustfmt --check、marker-lint OK、UTF-8 边界 hygiene clean、
    GNU 对照探针 5 批全量比对(脚本见 ~/workspace/fix-3441/probe3441*.sh)。
  • 未做(须复核轮):新 stem 的 base fail-first 与 head 全绿、全量 lib 与 base
    逐数对齐、RUSTFLAGS="-D warnings" cargo check --locked --lib --bins、
    CI 首验。本 PR 即为 CI 首验载体。
  • 详见 ~/workspace/fix-3441/REPORT.md(含 5 条相邻观察:多报诊断/compound
    元素词/模式词 ${}/ ${v:?${-x}} / 引号形归属 fix(expand): drop QUOTED_EXPANSION_SITE at the display decode boundary (Refs #3433) #3437)。

…or assignment-shaped words (Refs #3441)

GNU subst.c:10278 parameter_brace_expand reports the whole word
expand_word_internal was expanding, and expand_word_internal never
splits an assignment-shaped argument word at '=' (subst.c:4357 only
expands the RHS as its own word for a genuine assignment statement).
Rubash's expand_word_mut_with_context split a=${-x} at '=' and
expanded the RHS alone, so the diagnostic named the fragment.

Record the whole word as the pending diagnostic word across the
split (non-compound RHS only: GNU names the element word for
compound values), and make PendingDiagnosticWordGuard install-if-
absent so the RHS walker's ${} arm cannot re-root the record at
the RHS. Nested operator words keep their own word: the outer
fragment's gate consumes the record before they expand.

Regression nails: tests/issue3441_bad_subst_diag_word_regressions.rs
(26 tests; fail-first 19 red / 6 guard-green on the pre-fix binary).

UNVERIFIED-BY-CAPTAIN
@caomengxuan666

Copy link
Copy Markdown
Member Author

Captain review — FAIL

Reviewed PR #3442 on its own evidence. The fix works for the shapes it covers, but its own regression pin fails 2/26 on the head, and both are genuine GNU divergences — the guard never reaches those word positions.

What passed

F1 (product) — guard coverage is incomplete

The fix installs PendingDiagnosticWordGuard only in the assignment-split arm of parameter_core.rs. Case-subject words and for-list words never pass through that arm, so the diagnostic still names the fragment:

  • case a=${-x} in *) echo hi;; esac — GNU: a=${-x}: bad substitution; head: ${-x}: bad substitution
  • for w in a=${-x}; do echo "$w"; done — GNU: a=${-x}: bad substitution; head: ${-x}: bad substitution

Both verified directly against GNU bash 5.3.20. Failing pin cells: issue3441_case_subject_names_whole_word, issue3441_for_list_word_names_whole_word.

Rework: extend guard installation to the case-subject and for-list word expansion paths (or the shared word-expansion entry those paths use), keeping the 24 passing and 7 guard cells green. Re-review re-runs the full leg set.

Note: the -D warnings forced recheck is incomplete — it died on disk quota while writing one test target's metadata (71 rustc calls, zero warning diagnostics, product lib clean under -D warnings). The full-suite result is owed to the re-review round.

Full evidence: ~/workspace/review-3442/CAPTAIN-VERIFY.md (§15).

@caomengxuan666

Copy link
Copy Markdown
Member Author

Captain review — PR #3442 (Refs #3441): FAIL — one item, F1

Reviewed head 516b6e21 (3 files, +258/−2) on a single head→base→head tree with GNU bash 5.3.20 as oracle. CI is all green (10 pass + 1 skipping), hygiene (fmt / marker-lint / utf8-boundary) green three times, and the interval c28a9dd..8a73f079 (live tip) has no overlap with this PR's 3 files.

Legs (all with real test result lines, 2026-11-13 00:00–00:20Z):

F1 (product, incomplete fix coverage) — the only FAIL item:

The guard is installed only in parameter_core.rs's assignment-splitting arm. case subject words and for-list words don't take that arm, so the diagnostic still names the fragment:

  • case a=${-x} in *) echo hi;; esac — GNU: a=${-x}: bad substitution; head: ${-x}: bad substitution
  • for w in a=${-x}; do echo "$w"; done — GNU: a=${-x}; head: ${-x}

Both are also red on base (part of the 19), so this is uncovered surface, not a regression. The other 17 repair forms and all 7 guard forms are green on head.

Rework: install the guard on the case-subject and for-list word expansion paths (or their shared word-expansion entry), keeping 24 green + 7 guard green. Re-review re-runs the full leg table including the complete -D warnings pass. Not merging on this head.

…or case/for-list words (Refs #3441)

GNU subst.c:10278 names string — the whole word expand_word_internal was expanding — for a bad substitution. The case subject (execute_cmd.c:3686 expand_word_leave_quoted) and for-list words (execute_cmd.c:3017 expand_words_no_vars -> subst.c:13219 expand_word_list_internal) both reach rubash's non-mut expand_word, whose assignment split re-rooted the diagnostic at the RHS alone, so case a=${-x} and for w in a=${-x} reported ${-x} instead of a=${-x} (review F1 on PR #3442).

Install the whole-word PendingDiagnosticWordGuard in expand_assignment_word — the non-mut mirror of the mut arm in parameter_core.rs, with the same compound/quoted exclusions (GNU names the element word for compound values) — and make with_pending_diagnostic_word install-if-absent so the walker's ${} arm keeps the enclosing word instead of re-rooting it at the RHS. Nested operator words still keep their own word: the outer fragment's gate evaluation takes the record before any nested operator word expands.
@caomengxuan666

Copy link
Copy Markdown
Member Author

Captain re-review: PASS (new head c04bc781, rework of F1 verified from scratch)

Re-ran all legs on a fresh build from c04bc781 on X299-2607 (driver log r3442r-logs, DRIVER_DONE 2026-11-13T14:06:08+08:00):

  • Pin stem 26/26 on head, 7/26 on base — both F1 cells green by name (case_subject, for_list_word); the 19 base failures are exactly the repair forms. Fail-first exact.
  • Lib bilateral 1349/1/4 both sides, single failure the same name on head and base (issue2084_session_end_restores_host_process_state) — name-set diff ∅.
  • Adjacent 6 stems bilateral green (1/8/4/5/3/11); [bug] bad substitution diagnostic leaks PUA marker U+E31B for assignment-like words with ${-...} #3433 overlay 2/1 both sides, same failing test.
  • Hygiene green: rustfmt on the changed files, marker-lint, utf8-boundary all rc=0 (full-tree cargo fmt hits Windows os error 206 with zero diff output — environment, not a violation).
  • -D warnings: full --tests check ran 226 rustc invocations; the only errors are pre-existing dead-code/unused-imports in test files untouched by this PR (issue2883, regression, issue2278, issue2737 — all byte-identical on base, base fails the same way). Product lib is clean (lib-only check rc=0) and the new pin stem checks clean on its own (rc=0).
  • GNU probes 10/16 vs Linux GNU 5.3.20 (was 8/14; the two new matches are exactly the F1 forms). The 6 divergences are the known pre-existing/excluded set (quoted PUA leak = [bug] bad substitution diagnostic leaks PUA marker U+E31B for assignment-like words with ${-...} #3433, compound element word, multi-report, pattern words, :? ordering).

Two corrections to the rework report: the GNU figure is 10/16, not 12/16 (16−6=10), and the head-vs-base probe diff is five forms, not four (multi_assign's first word also becomes the whole word on head).

CI on the new head shows the same 3 red jobs as master be610327 (rotating issue2859/issue2875 job-control flakes); nothing names this PR's face. The new pin stem does not run in CI — this review's X299 legs are the evidence.

Verdict: PASS. F1 fixed; all §10 merge conditions hold. §8 O1–O4 remain owed post-merge filings.

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