Skip to content

fix(patsub): literal-backslash pattern markers convert one-for-one - #3974

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/3963-pattern-backslash-pair
Nov 20, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/3963-pattern-backslash-pair

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

UNVERIFIED-BY-CAPTAIN

Refs #3963

核实(报告坐实)

报告原文(脚本文件字节形态):x='a\\b\c'(值 = a、\、b、\、c),printf '%q\n' "${x//\\\\/@}"(模式源码形态为 4 个反斜杠)。

  • GNU bash 5.3.20 oracle:a@b\c(%q 形态)——模式匹配值中一对双反斜杠,单个反斜杠保留。
  • master(81a5bbf):a@@b@c——每个反斜杠都被单独替换。与报告(niu 1.5.0 / fb025fc)一致。

根因

模式解码器(decode_parameter_pattern_quotes)把源码中的一对 \\ 消费为一个字面反斜杠标记(PATTERN_LITERAL_BACKSLASH,U+0018),所以源码 4 个反斜杠 = 2 个标记 = 2 个字面反斜杠。但无 glob 字符模式走的字面快路径在 normalize_parameter_pattern_backslashes 里又把相邻的两个标记折叠成一个 \——等于把已经解码完的标记再当成源码转义对二次解码,模式被减半,于是纯字符串搜索按单个反斜杠逐个命中。该函数同时被 glob 路径的 pattern_literal_tail_ends 尾串计算复用,同病。

修法

normalize_parameter_pattern_backslashes(src/executor/parameter_replace.rs)改为严格 1:1:每个标记(U+0018 / U+0014)各转一个真实反斜杠。glob 匹配器本体未动(它本来就把 U+0018 当字面反斜杠原子,带 glob 字符的模式一直正确,见对照表 case9/case12/case13)。

本 PR 删除了什么

删除了 normalize 中的 pair 折叠 pass(PATTERN_LITERAL_BACKSLASH_STR.repeat(2) → \ 的替换)及其误导性注释;无其他机制被绕过或新增。

oracle 对照表(脚本文件探针,两侧同跑;结果为 %q 解码后的值)

形态 GNU 5.3.20 master 本 PR
原文 ${x//\\\\/@},x=a\b\c a@b\c a@@b@c a@b\c
单替版 ${x/\\\\/@} a@b\c a@\b\c a@b\c
模式 2 反斜杠 ${x//\\/@} a@@b@c a@@b@c a@@b@c
单反斜杠值 + 模式 4 a\b\c(不变) a@b@c a\b\c
单反斜杠值 + 模式 2 a@b@c a@b@c a@b@c
混合模式 \\x(z=a\xb\y) a@b\y a@b\y a@b\y
混合模式 \x a@b\y a@b\y a@b\y
模式经变量 p='' 展开 不变 不变 不变
引号模式 '\' a@b\c a@b\c a@b\c
两对值 aa\bb\cc + 模式 4 aa@bb@cc aa@@bb@@cc aa@bb@cc
三连值 a\\b + 模式 4 a@\b a@@@b a@\b
转义星号 * a@b@c a@b@c a@b@c
模式 \\* (glob 路径) a@ a@ a@
删除形 ${x##*\} c c c
模式 \\\b(字面 \b) a@\c a@\c a@\c

测试

  • 新增单元回归 issue_3963_literal_backslash_markers_convert_one_for_one(normalize 1:1 + pair 匹配 + 三连步进)。
  • cargo test --lib:1281 过 / 1 败,唯一失败 unix_file_unary_operators… 为 master 同名环境基线(master 1279/1,差额为新增测试)。
  • 相邻集成套件:pattern trio 3/3、extglob param 2/2、nocase anchored patsub 6/6、patsub typed glyph 8/8、case pattern expansion quotes 4/4、case array pattern 1/1、glob expand 4/4 全过;glob_expand_quote 套件 6/3,3 条失败(issue_1253 errexit 家族)与 master 逐名一致、为存量基线。
  • 改动文件 cargo fmt --check 无漂移。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 20, 2026 07:40
@caomengxuan666

Copy link
Copy Markdown
Member Author

Captain review: verified against the GNU bash 5.3.20 oracle — 16/16 adjacent probes byte-identical after the fix (the 6 previously-wrong forms were all "≥2 consecutive literal backslashes with no other glob characters"). Root cause is a double decode: the pattern decoder already collapses \\ to one literal-backslash marker, and the literal fast path folded adjacent markers a second time. The fix makes the conversion strictly 1:1 and deletes the pair-folding pass; the glob matcher itself was already correct. Lib suite 1281/1 with the single failure being the known master environment baseline; adjacent pattern/glob suites green or baseline-identical. Merging.

@caomengxuan666
caomengxuan666 merged commit fbcc5bf into master Nov 20, 2026
9 of 12 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/3963-pattern-backslash-pair branch November 20, 2026 07:40
caomengxuan666 added a commit that referenced this pull request Nov 20, 2026
The needle site for replace:PATTERN_LITERAL_BACKSLASH_STR in
src/executor/parameter_replace.rs was deleted by #3974, leaving the
allowlist entry stale; master marker-lint fails on it. Remove the
single stale line (allowlist 279 -> 278 lines).

Refs #2885
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