Skip to content

fix(expand): drop QUOTED_EXPANSION_SITE at the display decode boundary (Refs #3433) - #3437

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/3433-bad-subst-pua-leak
Nov 14, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/3433-bad-subst-pua-leak

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #3433 — UNVERIFIED-BY-CAPTAIN

根因

bad substitution 诊断把带 PUA 标记 U+E31B 的词原文印到 stderr——仅当词为赋值形(<name>= 紧贴展开点前)且坏展开以 - 开头(${-x}、${-n-unset})。

链路:mark_quoted_ifs_from_raw(command_prepare.rs)在引号段内的展开点前插 QUOTED_EXPANSION_SITE(U+E31B),供 walker 给该展开结果做 IFS 保护(#2706;GNU subst.c:11862 add_quoted_string)。walker 见标记即消费,但诊断引的是 walk 前录下的整词(PendingDiagnosticWordGuard,embedded_mutations.rs),经 bad_substitution_display → decode_to_visible_text 渲染;该法定解码器有 E31A(COMPOUND_ASSIGNMENT_MARKER)的丢弃臂、缺 E31B,标记直落 stderr。

GNU 锚点:subst.c:10278 report_error(_("%s: bad substitution"), string)——诊断引正在展开的整词原文;subst.c:11229 expand_word_internal 跟踪各展开点引号状态、subst.c:11862 add_quoted_string、subst.c:11552(引号段结果“will be split, but this portion should not be”);GNU 的 dequote 在边界完成(subst.c:4807 dequote_string),与本修法的解码器边界同构。

修法

机制修(非个案):法定解码器 decode_to_visible_text(src/locale.rs)的丢弃臂补 U+E31B,与同族 E31A 同待遇。裸 E31B 恒为协议(用户键入的 $'\uE31B' 经 E400 转义入境,解码器 E400 臂先恢复它,丢弃臂看不到它——marker_leak_golden.rs 的字面往返钉仍成立)。另订正 markers.rs 中 E31B“永不越 walker、故无解码边界”的旧断言(诊断词是 walk 前录的),并给解码器补单元钉(裸 E31B 丢弃、E400 转义往返)。

触发边界(GNU 5.3.20 oracle 实测):a=/aa=/A= 紧贴展开点前漏,a${-x}/=${-x}/a-b${-x} 不漏;真赋值语句(x="a=${-x}")两侧同洁。回归钉 tests/issue3433_bad_substitution_pua_leak.rs:4 个种子形 + 4 个边界形(含双点形)断言逐字节消息与无 EE 8C 9B,7 个对照形断言消息与洁净,另断言用户字面 E31B 往返。

fail-first 证据(种子级):GNU 5.3.20 跑 seed_r108c.sh 14/14; verdict 二进制(master 6c09b5c4)4 FAIL(恰为 4 个无 PUA 断言)+ 10 PASS。新钉的 base/head 双边腿待队长复核轮上 X299/box 跑(本轮 box 构建槽满,本地腿未跑)。

相邻观察(未入本单,请队长裁):未加引号的 echo a=${-x}——GNU 报 a=${-x}: bad substitution,rubash 报 ${-x}: bad substitution(诊断引片段而非整词),与 PUA 泄漏是不同机制,未钉。

本 PR 删除了什么

无删除。本 PR 只在法定解码器的丢弃臂加一个码点、订正一处标记文档、新增一支回归钉与一个解码器单元钉:无文件删除、无公开 API 删除、无行为删除;唯一行为变化是 bad substitution 诊断不再印出内部 PUA 标记(与 GNU 逐字节一致)。

…y (Refs #3433)

bad substitution cited the pre-walk marked word, and decode_to_visible_text
had a drop arm for the sibling U+E31A but not U+E31B, so the marker leaked
into stderr for assignment-like words with ${-...} (GNU subst.c:10278
cites the word verbatim). Decoder-level fix; E400-escaped user literals
still round-trip. Pin: tests/issue3433_bad_substitution_pua_leak.rs.
UNVERIFIED-BY-CAPTAIN
#3433)

The marker-lint rework compared decode_to_visible_text(..) (String)
with &format!(..) (&String): E0277, the only compile error on head
1b05a83. Drop the borrow; test code only, product semantics untouched.
caomengxuan666 added a commit that referenced this pull request Nov 13, 2026
…or assignment-shaped words (Refs #3441) (#3442)

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}}` / 引号形归属 #3437)。
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 14, 2026 01:56
@caomengxuan666
caomengxuan666 merged commit 564c92d into master Nov 14, 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