Skip to content

fix(executor): ${v:?${-x}} reports only the nested bad substitution - #3855

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/3502-question-error-bad-substitution-word
Nov 17, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/3502-question-error-bad-substitution-word

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #3502

Summary

${v:?${-x}} printed two diagnostics — ${-x}: bad substitution and a spurious v: line — where GNU prints only the first.

GNU ordering (subst.c):

  • parameter_brace_expand_error (subst.c:8216-8239) expands the :?/? message word via expand_string before it can report_error("%s: %s", name, temp).
  • A bad substitution inside that word fails valid_brace_expansion_word (subst.c:7583-7597) and takes the bad-substitution fallback (subst.c:10042-10057, 10272-10288), which longjmps — the outer v: report never runs.

Rubash's diagnostic pre-scan parsed the :? operator first and reported the outer name: word form, emitting the extra v: line after the inner bad substitution had already been reported.

Fix: in the pre-scan (parameter_errors.rs), before parse_parameter_error_operator, check the whole inner word with the existing braced_name_is_bad_substitution (the valid_brace_expansion_word port). A bad-substitution word now returns the bad-substitution result immediately — display subject is the whole :? message word (pre ${-x} post: bad substitution, matching GNU's expand_string granularity), status 1 — so the operator path that produced v: is never reached.

What this PR removes

The spurious second diagnostic line for this operand family; nothing else.

Validation

  • New pin tests/issue3502_question_error_bad_substitution_word.rs: 7 tests, all pass on head.
  • fail-first on merge-base 6ecb7cab: 5 failed / 2 passed — red set is exactly the bad-substitution-ordering assertions; the 2 green are preserved-behavior guards (nested ${x:?inner} still reports x: inner rc 127; ${v:?${-}} still reports v: <flags> rc 127).
  • lib two-sided cargo test --lib: head 1376/1/4 vs base 1376/1/4, failure-name set difference empty (shared failure api::consumer_tests::issue2084_session_end_restores_host_process_state reproduces identically on base — environmental).
  • RUSTFLAGS="-D warnings" cargo check --all-targets: clean.
  • rustfmt clean on touched files; check-marker-literals.py OK; check-utf8-boundary-hygiene.sh clean.
  • WSL GNU Bash 5.3.20 (/usr/local/bin/bash, 9c465866) script-file probes, 12 shapes (:?/?/:-/=/+/% operators, unset/empty/set v, whole-word and fragment bad substitutions, nested :?, flags parameter, bad-substitution-before-? ordering): stdout+stderr+rc byte-identical modulo the program-name prefix.

…Refs #3502)

GNU parameter_brace_expand_error (subst.c:8216-8239) expands the :?
message word first; a bad substitution inside it reaches
valid_brace_expansion_word's failure (subst.c:7583-7597,
10042-10057) and longjmps before the outer `name: word` report can
run, so `${v:?${-x}}` prints only `${-x}: bad substitution` — never a
trailing `v: ` line. The pre-scan now checks the whole word with
braced_name_is_bad_substitution before parsing the :? operator,
returning the bad-substitution result instead of falling into the
operator path that emitted the spurious outer report.
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】i1 审计 — 建议:FAIL(#3855)

PR #3855 第1轮 — 建议:FAIL
head: 4abbde14f8325e2d7796222440a2c0cbafec5538 base(merge-base): 6ecb7caba23c765735647b62e6f4792b7be8fd7a 文件: 2 个 +129/-0
关联 issue: #3502(open,我核过的时间:2026-11-17 23:05 +0800)

机制核查:

  • 修复方向本身与 GNU 一致:parameter_brace_expand_error(third_party/bash/subst.c:8216-8239)先 expand_string(value) 再 report_error,内部 bad substitution 使外层 name: word 报告不可达;valid_brace_expansion_word(subst.c:7583-7597)门槛与 expand_word.rs:1118 实路径共用 braced_name_is_bad_substitution,结构自洽。
  • 但新增检查缺少 funsub 豁免:src/executor/parameter_errors.rs:3009 对整个 inner 跑 braced_name_is_bad_substitution,对空白起头的 ${ cmd; } funsub 体(inner=case x in x) esac; / echo 4;)判为非法名 → bad substitution。前一行空白检查(parameter_errors.rs:2984-2990)明确用 !word_contains_current_shell_command_substitution(word) 豁免该形态,新检查没有等价豁免。
  • diff 其余部分无可疑/无关改动;无桥接/翻译层。

腿(逐项给数字与名字):

  • 钉自跑:issue3502_question_error_bad_substitution_word 7 passed / 0 failed(head,本地 Windows)。
  • fail-first:base 6ecb7caba + 原样钉文件 → 5 failed / 2 passed,红名 = {bad_substitution_before_question_error_wins, question_mark_bad_substitution_names_the_whole_message_word, question_mark_bad_substitution_word_reports_inner_only, question_mark_null_bad_substitution_word_reports_inner_only, question_no_colon_bad_substitution_word_reports_inner_only},与 PR 声称一致;base 上 stderr 实录为 ${-x}: bad substitution + 多余 v: 行,bug 复现属实。
  • lib 双边:head 1376 passed / 1 failed / 4 ignored(失败名 api::consumer_tests::issue2084_session_end_restores_host_process_state,环境性)/ base 1376 / 1 / 4 同名失败 / 差集 ∅。
  • 补充回归腿(cli_tests + regression stems,PR 未跑):
    • head cargo test --test cli_tests arithmetic_for:5 中 2 红 = malformed_parameter_expansion_in_arithmetic_for_is_a_syntax_error(exit 127,期望 0)、stdin_script_stops_after_arithmetic_for_syntax_error_without_errexit(exit 1,期望 0)。
    • head cargo test --test regression golden_funsub_valsub:FAILED(exit Some(1),期望 Some(0))。
    • base 6ecb7caba 同三测全绿(cli 5/5 ok,golden ok)。
  • -D warnings:未跑(判 FAIL 已由回归腿决定,返工后复审补跑)。
  • 卫生:rustfmt --check 触及文件 clean;check-marker-literals.py OK;check-utf8-boundary-hygiene.sh clean(注:在 base 树+拷入钉文件上跑,parameter_errors.rs 为 base 版)。
  • CI(head 4abbde14,run 44100322550):红任务 3 个——Compatibility CLI tests(上述 2 测,本 PR 引入)、GNU regression goldens and canaries(golden_funsub_valsub 本 PR 引入 + issue_753::case_9_declare_indexed_operand_binds_nothing、upstream_suite_slice_snapshots 与 master 同红=基线)、Regression pin stem census(失败名集与 master run 44096234749 逐名 diff 完全一致=基线;新钉 stem 7/7 PASS)。对照 master CI:Compat CLI 在 master 上是绿的,goldens 在 master 上只红那 2 个。
  • GNU 对照:WSL /usr/local/bin/bash GNU Bash 5.3.20(1)-release,脚本文件 scratch-i1/3502-cmp.sh,15 形状(:?/?/:-/= 邻接、unset/empty/set、消息词片段、嵌套 :?、flags 参数、badsubst 前置序、单引号)stdout+stderr(去程序名前缀)+rc 与 head target/debug/rubash.exe 15/15 逐字节一致。脚本与输出存 scratch-i1/3502-cmp-{gnu,rub}.out。注:Windows 对照,Linux 专属结论待队长 oracle 复核。

F 点(建议 FAIL):

  • F1: src/executor/parameter_errors.rs:3009 新增 braced_name_is_bad_substitution(inner) 检查对空白起头 funsub 体误判。复现:for (( ${ case x in x) esac; };; )); do break; done; echo after → head exit 127(-c)/1(-s),无 after;base 与 GNU exit 0 输出 after。同机制使 v=${ echo 4; } 等 funsub 形态报 bad substitution(golden_funsub_valsub 红)。GNU 应然:${ cmd; } 是 funsub(parse.y SX_FUNSUB / subst.c:6917 function_substitute),空白体不是参数名,不得走 bad-substitution 门;修法方向=给新检查加与 :2984-2990 空白检查一致的 word_contains_current_shell_command_substitution 豁免(或按 GNU 语义限定检查范围),并补 funsub 回归钉。

续作记录:本轮未动代码(审计判 FAIL,返工属 g1 lane)。

未跑/VOID 的腿:

  • -D warnings cargo check:未跑(FAIL 判词已定;返工复审时补)。
  • 卫生两脚本在 base 树上执行(head 版 parameter_errors.rs 差异为纯新增 22 行,不影响 lint 结论的置信度有限——返工后需在 head 重跑)。

@caomengxuan666
caomengxuan666 merged commit ad949be into master Nov 17, 2026
9 of 12 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 17, 2026
… the scan can own (Refs #3864)

The #3855 pre-scan gate judges every ${...} inner as a braced name
and reports the scan's whole word with status 1, but GNU's bad
substitution default (subst.c:10276-10288) reports the WORD BEING
EXPANDED with the error class of the context. The scan may only emit
that verdict when it coincides with the real expansion's, so the gate
now defers to the real expansion when (a) the word is a compound
assignment value — GNU expands elements individually
(arrayfunc.c:557) and names the element word, which the scan's
whole-word display cannot name (issue3500); (b) the name head is a
$'...' literal the real expansion decodes — subst.c:7583 judges the
extracted NAME (issue2650); (c) the verdict is POSIX-fatal —
subst.c:10285-10286 FORCE_EOF when POSIX + non-interactive + compat
> 43, which status 1 cannot express (audit B4-F08). All seven #3502
pins and the #3869 funsub pins are preserved by construction: no
deferral fires on their words.

UNVERIFIED-BY-CAPTAIN: no build/test executed in this lane (build
chain down); head census re-run parked for the captain.
caomengxuan666 added a commit that referenced this pull request Nov 18, 2026
… the scan can own (Refs #3864)

## Summary

Refs #3864. PR #3855 added a `braced_name_is_bad_substitution(inner)` check to the `parameter_expansion_error_in_word_context` pre-scan so a bad substitution inside a ` :?` message word surfaces as the inner error (rubash#3502: `${v:?${-x}}` prints only `${-x}: bad substitution`). The check judges the whole `${...}` inner as a braced name and reports the scan's whole word with status 1 — but the scan may only emit that verdict when it coincides with what the real expansion would report. Where it does not, the scan pre-empts the correct diagnostic:

- Compound assignment values (issue3500, whole stem): GNU expands elements individually (`arrayfunc.c:557 expand_compound_array_assignment`) and the diagnostic names the element word (`\\[0\\]=${-x}`); the scan names the raw parenthesised value.
- `$'name'` extquote names (issue2650): `subst.c:7583 valid_brace_expansion_word` judges the extracted NAME; the real expansion decodes `$'x1'` to `x1` in a quoted context, where the gate on the raw literal misjudges it.
- POSIX fatal class (audit B4-F08): `subst.c:10285-10288` makes a bad substitution FORCE_EOF when POSIX + non-interactive + compat > 43; the scan's status 1 downgrades it to DISCARD.

The funsub faces (issue2763/issue2761/issue3092/issue3823, golden_funsub_valsub, the arithmetic-for CLI case) are already fixed by #3869, which exempts funsub bodies at the top of this same scan loop; this PR does not touch that exemption.

## Repair

The gate now fires only when the scan's verdict coincides with the real expansion's: it defers (returns nothing, letting the real expansion report) when any of these holds —

1. the scanned word is (or contains) a compound assignment value (parser compound marker, or a leading `(`): GNU's diagnostic is per-element, which the scan's whole-word display cannot name;
2. the span's name head is a `$'...'` literal that `resolve_dollar_quoted_parameter_name` decodes: the gate must judge the decoded name;
3. the POSIX-fatal predicate holds (POSIX mode, non-interactive, `compatibility_level > 43`, mirroring the fatal arm in `command_execute.rs`): the scan's status 1 cannot express FORCE_EOF.

Everything else is unchanged: the gate still fires for bare bad names (`${-x}`, `${}`, `${(M)y}`), still pre-empts a later `:?` error left-to-right, and still fires inside `:?`/`:-` operator-word recursions — so all seven #3502 pins and the #3869 funsub pins keep passing by construction.

## What this PR removes

Nothing from the product surface: the #3855 gate check stays; only its three over-broad firing slices are deferred to the real expansion. No new diagnostic text, no new error class.

## Validation

**UNVERIFIED-BY-CAPTAIN** — no build or test was executed in this lane: the build chain is down (X299 tunnel refused, Simular box wallet empty, local build banned: MemAvailable ~503M, below the 1.5G gate). `cargo test` / `cargo check` / lib+base alignment / `RUSTFLAGS="-D warnings"` check are all parked; the captain re-runs the head census after the chain recovers.

- New nails `tests/issue3864_parameter_errors_regression.rs` (11 tests): issue3500 element words (indexed/assoc/nested/valid guard), issue2650 extquote top-level + bare forms, POSIX fatal + compat43 guard, funsub-valsub shapes, #3502 inner-only + pin-7 ordering guards. Written fail-first, before the repair; fail-first is by code analysis, not execution (see above).
- GNU oracle probes (local `/bin/bash` 5.2.21; stems already carry 5.3.20 verification): `"[${$'x1'}]"` -> `[hello]`; `a=([0]=${-x})` -> `\\[0\\]=${-x}: bad substitution` rc 1; POSIX `echo ${}` -> fatal, no ALIVE, rc 1; `${v:?${-x}}` -> only `${-x}: bad substitution` rc 1.
- `rustfmt --check` clean on both touched files.
- Evaluation note: reverting #3855 outright was considered and rejected — it would reopen #3502 (7 pins). The scoped repair keeps #3502 green while clearing the mechanically attributable clusters. Three singles in #3864 (issue2029, issue2771, issue2761 x2) have no `${` span in their scripts that can trip the gate, so no `parameter_errors.rs` change can clear them; they are recorded in the lane REPORT as unattributed (likely census flakes / other-window commits) for the captain's census re-run to confirm.

## Captain review (PASS, merged)

Original head 19159b6, captain-extracted from its CI logs:
census total 79 (era baseline), issue3500 19/19, own pins
issue3864 11/11, goldens exactly the two baseline names; the
darwin leg was cancelled with zero steps (never ran, not a red).

Merge-shape head 0a73b4f (update-branch over master 0863639;
the only master-side touch of parameter_errors.rs since the
merge-base was PR #3871's +6/-2 arith-label hunk near line 1417,
~1000 lines from this PR's hunks), captain-extracted:
- issue3500 19/19 PASS, issue3864 11/11 PASS, issue2650 10/10,
  issue2763 6/6, issue201 2/2, issue2771 5/5, audit stems 40/40.
- Census total 81, goldens exactly the two baseline names.
- Two non-blocking cells, both adjudicated churn: the CLI leg's
  sole failure is c_command_keeps_named_coproc_output_fds_distinct
  (2 PASS / 2 FAIL across four era heads, uncorrelated with any
  diff), and census cell issue2029
  head_pipe_fold_leaves_the_remainder is FAIL here but PASS on
  both the original head and on PR #3870's head (which carries
  #3855 without this fix) — not owned by this change.

Fail-first basis (stated honestly): the lane's 11 pins were
written pre-fix but not executed pre-fix (no build route); their
fail-first rests on code analysis, corroborated by the tip census
showing the same shapes red before this PR and green after.

What this removes: the unscoped #3855 gate call in
parameter_expansion_error_in_word_context — the scan emitted
whole-word status-1 verdicts for compound values, $'...' name
heads and the POSIX-fatal class. It is replaced by the
deferral-aware gate; each deferral restores the pre-#3855
behavior for that slice. No other behavior is deleted.
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] ${v:?${-x}} 多报一行 v: (:? 与 bad substitution 竞发次序)

1 participant