Skip to content

Refs #3088 - #3113

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/3088-alternate-backslash-protection
Nov 10, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/3088-alternate-backslash-protection

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #3088

UNVERIFIED-BY-CAPTAIN — lane self-verification only; captain review pending.

基底 1c132f68(#3080 合入点);master 现已前移,合入前 update-branch。

根因

${var:-word} alternate 展开后,展开结果的反斜杠在后续 unescape pass 里被当作源码转义折叠(GNU 绝不重扫展开结果)。同一机制,四处落点:

  • Site A — expand_operator_rhs_for_context 的 DoubleQuoted/Heredoc 分支:mut walk 先保护、后在 dq-unescape 前恢复 U+E30D,\$ / \` / \" / \} 被折叠。
  • Site B — expand_embedded_parameters_alternate_mut 末尾的 unescape_remaining_shell_escapes(protect_rhs_data=false、裸拼接),含 $( 的 alternate 触发。
  • Site C — 非 mut expand_parameter_word 末尾的 unescape_unquoted_rhs_escapes,无 comsub 的非引 alternate 触发(每嵌套一层折一次)。
  • &self 链 expand_quoted_parameter_word 的 :-/:+/-/+ 臂(for-list "${x:-$y}" 经此链)。

修法:展开结果的反斜杠以 U+E30D 载体穿越全部后处理 unescape pass,只在最外层边界恢复;源码转义(fall-through 臂)保持不保护,仍在 pass 内正常消解。修法面:embedded_mutations.rs / embedded_parameters.rs / parameter_words.rs / command_prepare.rs(后两者为折叠落点本身,均不在停手清单内;未碰 parameter_core.rs / expand_word.rs / command_substitution.rs / compound_exec.rs / quotes.rs / continuation.rs / api-ledger.md)。

GNU 锚点

  • subst.c:4462 expand_string_for_rhs → subst.c:11229 expand_word_internal:alternate RHS 走与普通词同一单遍词展开。
  • subst.c:4773 quote_string:展开结果以 CTLESC 保护;subst.c:4807 dequote_string:只剥 CTLESC。
  • subst.c:7966 parameter_brace_expand_rhs。

另:单内嵌套形原文(双引、2 反斜杠)经 GNU 实测为 ['a\'],与单内所称 [a\\$b] 不符——钉按 REPORT §6 形(非引、1 反斜杠,GNU [a\$b])断言,见 REPORT。

验证(车道自验)

本 PR 删除了什么

无删除:纯加法修复。新增 tests/issue3088_alternate_backslash.rs(499 行);4 个源码文件 +258/−136 行,新增保护入口与恢复辅助函数,无公开面变动,无函数删除(仅 expand_embedded_parameters_protect_ifs_and_data 入口由直调 inner 改为经 with_context_inner 路由,行为不变)。

…otection (Refs #3088)

43 cells: flat + nested ${var:-word} forms whose expansion-result
backslashes must survive the post-expansion unescape passes, plus
no-backslash controls and source-escape regression guards.
Expectations are GNU bash 5.3.20 byte output. Fail-first on base
1c132f6: 15 passed / 28 failed; fixed tree: 43/0.
…nsion unescape passes (Refs #3088)

${var:-word} alternate results lost their backslashes: after the
alternate walker assembled the result, a trailing unescape pass
re-scanned expansion data as source escape syntax and folded \$ /
\` / \" / \\} pairs. GNU never re-scans expansion results
(subst.c:4773 quote_string CTLESC-protects them; subst.c:4807
dequote_string strips only CTLESC).

One mechanism, four sites: the mut walker's double-quoted/heredoc
branch and assignment alternate (deferred restores + restore after
the operator-result unescape), the alternate walker's trailing pass
(splice protection via protect_expansion_results), the non-mut
expand_parameter_word tail (protect_ifs_and_data walk + restore),
and the &self quoted chain's :-/:+/-/ + arms (protect_data walk +
restore). Source escapes still resolve in every pass — only
expansion-result and single-quoted-span backslashes ride the U+E30D
carrier.

Probe matrix 96 shapes: 94 SAME / 2 DIFF (e6/e8, pre-existing
source-escape recognition residuals, different mechanism).
Pin tests/issue3088_alternate_backslash.rs: 43/0 (base fail-first
15 passed / 28 failed). lib 1206/0/4 both sides.
Resolve one content conflict in src/executor/embedded_parameters.rs
(expand_embedded_parameters_double_quoted): keep master's
with_extquote_name_quoted_context wrapper (rubash#2650) around the
PR's 7-arg expand_embedded_parameters_inner call (rubash#3088
protect_data=false). All other files auto-merged.
@caomengxuan666
caomengxuan666 merged commit c52d316 into master Nov 10, 2026
11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 10, 2026
E3 波修法落地:三单按 GNU `verify_substring_values` / `evalerror` / 下标解码语义修毕(钉先行,本 PR 前段四提交为四单回归钉;#3086 本 PR 仅钉、修法不在内)。

逐单:

- Refs #2713 — 钉 `tests/issue2713_substring_arith_diagnostic_raw_text.rs`(11 格);修法 `fd6c0d2f`:负长度诊断改报原始 length 切片(原文逐字、含前导空格,求值数仅兜底),算术求值只去首空白(`.trim()` → `.trim_start()`),单引号操作数按整操作数/span 两形分判回显。GNU 锚 subst.c:8395/8487/8502、expr.c:1526-1537。
- Refs #2752 — 钉 `tests/issue2752_substring_length_lazy_evaluation.rs`(13 格);修法 `cf93f442`:offset 求值后、length 求值前按值类界做越界门控(位置参数 nargs+1、关联元素数、索引最大下标、标量字符长),越界直接空展开——length 的副作用/comsub/坏表达式全不触发;B4/B12 为非判别回归锁。GNU 锚 subst.c:8428-8464。
- Refs #3085 — 钉 `tests/issue3085_dollar_single_quote_subscript_token.rs`(10 格);修法 `5f8d47f5`:写路径下标 `$'...'` 经既有助手 ANSI-C 解码 + sh_single_quote 重引后进算术 pass,读路径解码为裸内容骑 span-data 载体,`$` 与载体不再泄漏进算术错误 token。GNU 锚 subst.c:4133。
- Refs #3086 — 钉 `tests/issue3086_dollar_locale_quote_walker.rs`(12 格)仅钉:共享 walker 的 `$"` 臂修法不在本 PR(原门控 PR #3113 已合入,修法另行派工);本单合入后保持 OPEN。

验证(队长复核,记录 `~/workspace/review-3164/CAPTAIN-VERIFY.md` 修法头复核节):等价性三证逐字一致(三 fix 提交合计产品 diff 与批跑 B18 所测未提交工作树 diff 字节级相同,md5 `de0c4fecfcdf5ff4021bde84aa8bab09`);B18 实测头钉 11/11、13/13、10/10,邻茎六茎全绿,lib 头 1206/0/4(名集 ⊆ 基底),`-D warnings` rc=0,fmt/marker PASS;PR 自身 CI 10 success + 1 skipped、零红(含三条跨目标 check --tests 腿全绿)。

## 本 PR 删除了什么

无文件删除、无功能移除。产品代码净 +196/−13:删除的 13 行均为被替换的实现行——parameter_core.rs 8 行(两处 `.trim()` 全修剪实现、span-only 回显调用行等)与 parameter_errors.rs 5 行(以已求值数字组装负长度诊断的旧构造),分别由原文回显/只去首空白/原文切片构造取代;其余为纯新增(界定价与门控、下标 `$'` 解码臂、钉文件 +779 行)。
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