Skip to content

fix(executor): zero-length advance and literal U+E000 stay on match-unit boundaries (Refs #3921) - #3926

Merged
caomengxuan666 merged 8 commits into
masterfrom
fix/3921-bracket-zero-length-pua-boundary
Nov 18, 2026
Merged

caomengxuan666 merged 8 commits into
masterfrom
fix/3921-bracket-zero-length-pua-boundary

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

根因

#3546 修法(PR #3920,47a24b28)只把三个 replace finder 的候选边界改为匹配单元边界,留下两项边界情形(REPORT §6,即本单):

  1. 零长匹配推进切半 pair:replace_parameter_pattern_inner 的零长分支(cursor = end + ch.len_utf8())只复制一个字符。零长匹配恰落在 raw-byte marker 对(U+E000 + payload)或 literal-char escape 对(U+E400 + char)之前时,只复制对的首半、把 cursor 丢在 pair 当中;随后扫描在末尾位置又找到一个幻影零长匹配,值以 pair 收尾时多出一个替换(${w//*(x)/Q} 在单个 0x14 字节上得 Q\x14Q,GNU 为 Q\x14)。
  2. 字面 U+E000 按一单元走边界:用户数据里的真 U+E000 在 replace 面须与 removal 侧一致、按一整个匹配单元处理,不得与后随的 payload 区字符拼成一单元、也不得从 escape 半上切开。

修法

  • 零长推进步进落到匹配单元边界(与 fix(executor): bracket carrier bytes match in ${v//[...]/r} (Refs #3546) #3920 的 marker_pair_split_points 过滤同构):用已算好的 indices(单元边界表)取 end 之后第一个边界,整单元复制后推进。
  • 字面 U+E000:区分“用户数据里的真 U+E000”与“载体 marker 对的首字符”;若无法干净区分,至少把行为钉死并与 removal 侧对齐(见钉)。

GNU 锚点

  • subst.c:9184 pat_subst() → lib/glob/sm_loop.c:398 BRACKMATCH():载体字节与 U+E000 字符都是数据。
  • subst.c:9286-9304:零长匹配后复制一个字符;GNU 里载体字节(一字节)与 U+E000(一字符)各为一个字符。

回归钉

tests/issue3921_zero_length_pua_boundary_regressions.rs(5 个 test fn):*(x)/?(x) 零长推进 15 形(单载体、双载体、两类载体、单斜杠、ASCII guard)、E400 对上的零长推进、字面 U+E000 的 ?/[...]/字面 pattern/#/%/*/${#} 16 形、U+E000 + U+E001 不拼合 6 形、comsub 与源码字面两种入口。期望值 = GNU bash 5.3.20 在 LC_ALL=C.UTF-8 下的实测输出。

本 PR 删除了什么

什么也没删除。本 PR 只加回归钉(首提交),随后补修法提交;不删代码、不删测试、不改公开面。

UNVERIFIED-BY-CAPTAIN

队长复核前状态:修法提交待补;钉的 fail-first 与修绿腿待新服务器复跑后补记。

@caomengxuan666

Copy link
Copy Markdown
Member Author

队长复核(钉形态):钉已立住,待修法。

fail-first 已证:census 79,新增红格恰为两只缺陷格(zero_length_match_advance_does_not_split_carrier_pair、zero_length_match_advance_does_not_split_literal_pua_pair),其余钉格全绿;与 #3925 头比零其他新增(issue2340 wait 格未复现,佐证其为抖动格)。goldens 基线二名;CLI 仅 coproc 抖动格;macOS 本次全绿;其余全绿。钉头注写明两项残差的 GNU 锚点(subst.c:9184、:9286-9304、sm_loop.c:398)与 C.UTF-8 locale 要求,期望值均为 GNU 5.3.20 输出。

修法提交上来后队长再复核合并(基底即现尖 f983851,修法提交后无须 rebase 除非有其他 PR 先合入)。

@caomengxuan666

Copy link
Copy Markdown
Member Author

队长复核(修法头 26f1e69):待改——item-1 已修绿,但修法带出 PUA 面回归 + item-2 两格仍红。

已修绿:两只缺陷格(零长 advance 切半 carrier / literal PUA pair)转绿;自带钉 9 格中 7 绿。

回归(须修):

  1. lib 三腿同红一格:debugger::cli_tests::print_renders_decoded_carrier_values(cli_tests.rs:242)——修法改了 debugger print 的 PUA 解码渲染,transcript 缺 declare -- pua="\u{e314}"。
  2. census 新增五格,皆 PUA 面:issue2884 ×3(env import 编码:bare guard / escape pair / lone payload)、issue824(marker decode 边界 roundtrip declare -p)、issue843(patsub PUA escape declare -p glyph)。
  3. 自带钉 item-2 两格仍红:declare_p_shows_literal_pua_chars_as_data、printf_v_stores_literal_pua_chars_as_data——printf -v 与 declare -p 的矛盾未解。

其余:issue1902 一格为既判抖动族;CLI 转绿;goldens 本体测试通过(upstream_suite_slice_snapshots ok,case_9 在 lib 套内通过);darwin/linux/Windows/shell/ledger/niu-build 全绿。census 85(钉头 79)。

修法方向提示:item-2 的边界改动同时动了 env import、marker decode、patsub escape、debugger print 四条 PUA 消费面——须以 GNU 5.3.20 逐面定语义后收窄改动面,勿以单面修法压四面回归。

caomengxuan666 added a commit that referenced this pull request Nov 18, 2026
…3545)

## 根因

载体字节(0x0c、0x11、0x13、0x14..=0x1f)在 rubash 传输层以 U+E000 raw-byte pair 编码存储,但 `[[ =~ ]]` 两侧的解码并不对称:

- 带引号的 regex 路径(`regex_rhs_marked_chars` 经 `shell_text_to_raw_bytes`)把 pattern 侧的 pair 解回裸字节;
- subject 侧只过 `decode_command_substitution_payload`,它只还原 U+E319 comsub sentinel,不动 U+E000 pair;
- 于是 `$'...'` 字面形与 `|` 后的 `${c}`(走带引号路径)pattern 为裸字节、subject 为 pair 编码,永不匹配;而不带引号的 `a${c}b` 形两侧都保持 pair 编码,只是碰巧一致才匹配。

修法:在 regex 边界上把两种拼写都解开——pattern 在 `compile_conditional_regex_uncached` 入口、subject 在三个 match 入口(`conditional_regex_match` / `conditional_regex_match_status` / `conditional_quoted_regex_match_status`),共用一个 `decode_regex_operand`(sentinel 先解——它只产 U+0000..=U+00FF,不可能造出 pair introducer——再折叠 raw-byte pair)。这是 #729 glob 侧已在用的同一归一化。

## GNU 锚点

- `execute_cmd.c:4068` `arg2 = cond_expand_word (cond->right->op, mode)`(`=~` 且 compat > 31 时 mode 2)、`:4096` `sh_regmatch (arg1, arg2, ...)`:两侧展开后都以裸字节进 regcomp/regexec;
- `subst.c:4077` `cond_expand_word`、`:4114` QGLOB_REGEXP(`pathexp.c:212` `quote_string_for_globbing`)只把带引号的 regex 元字符字面化,载体字节本身是数据,原样进 regcomp。

## 改动

- `src/executor/conditional.rs`:新增 `decode_regex_operand`,4 处调用(pattern 1 + subject 3)。不碰 `src/executor/parameter_replace.rs`(在飞 PR #3926)、`src/executor/embedded_parameters.rs`(在飞 PR #3925)、`src/lexer/continuation.rs`、`docs/api-ledger.md`。
- `tests/issue3545_regex_operand_carrier_bytes.rs`:3 个钉(issue 原样复现循环、全载体集 15 字节 × var/alt/lit 三形 + 3 个非载体对照、`|` 位置对照 + `BASH_REMATCH[0]` 字节值)。

## 验证(新服务器 [email protected],Linux;本地 VM 未构建)

- fail-first(base db580be + 钉):3/3 红,输出与 issue 所述逐字一致(0c/14/1b/1f `alt=n`、`lit-n`;全集 15 字节 `y n n`;`x|${c}` 与 `${c}|x` 为 y、仅带引号的 `$'\\x01'|${c}` 为 n——正是带引号路径解码不对称的证据)。
- 绿腿(fix 树):stem 3/3 绿;15 个 guard stem 全绿(92 测:#729 carrier_byte_class、#3489、#1059/#1365 BASH_REMATCH、#1177/#1290/#1491/#1717/#3791/#322/#461 regex、#2981/#2987/#3100 quoted RHS、#1061/1242);`RUSTFLAGS="-D warnings" cargo check --locked --lib --bins` exit 0;rustfmt 两文件 OK;复现脚本用修后二进制跑出与 GNU 逐字节一致的输出。
- lib 对齐:fix 树 1280 passed / 0 failed / 4 ignored;base 树(db580be6)1280 passed / 0 failed / 4 ignored——逐数对齐。
- Windows 腿 PENDING(X299 隧道未通);CI 腿待合入前复核。

## 本 PR 删除了什么

什么也没删除。只改 `src/executor/conditional.rs`(+36/−8)并新增一个测试文件;没有删除函数、文件、公开面或行为分支。`decode_command_substitution_payload` 的其余 10 处调用不受影响。

## 队长复核(头 ef443d3,run 44379902406)

- census **77**,零新增(issue1902 与 issue2883 两格抖动族翻绿);自带钉 **3/3 绿**。
- goldens 仅基线二名(lib 1383/0 通过);CLI 全绿;darwin/linux/Windows 编译/shell/ledger/niu-build 全绿。
- 基底即现尖 db580be,无需 rebase。服务器腿齐(§验证)。
@caomengxuan666

Copy link
Copy Markdown
Member Author

队长复核(头 25bfa83,run 44388654722,已 settled):待改——有进展,未清零。

进展:自带钉 9/9 全绿。item-2 的两个旧红格(declare_p_shows_literal_pua_chars_as_data、printf_v_stores_literal_pua_chars_as_data)已转绿;item-1 仍绿。

仍红(census 85,尖 77;新增 8 格,修复 0 格):

  • PUA 面回归 6 格未清:issue2884 ×3(env import 编码)、issue824(marker decode 回绕)、issue843(patsub 转义字形),外加本头新增 issue1927 literal_typed_e000_unchanged(typed glyph 面,第四提交的 @Q/格式转义改动疑似波及)。
  • lib 三腿同红一格:debugger::cli_tests::print_renders_decoded_carrier_values(Rust tests / goldens 1382/1 / macOS 1276/1 均为此格;期望 declare -- pua="\u{e314}")。
  • issue2340 main_shell_jobs_prints_survivor_once:wait/jobs 已知抖动族,按抖动处理。
  • issue2771 posix_in_function_seconds_converts_and_ticks:SECONDS 时序敏感,疑似抖动——复跑确认,若稳定复现则按回归处理。

其余腿全绿(ledger / niu / shell / CLI / cross-target / darwin / Windows compile)。

方向:item-2 已合口,剩下的 6 格 PUA 回归 + debugger lib 格是同一边界改动的余波——按 02:46 评论的方向继续逐面收窄:env import、marker decode、patsub 转义、typed glyph 各自对 GNU 5.3.20 核一遍,不要再扩边界。REPORT.md 仍为 02:24 版,推新头时同步更新(四提交的改动面与验证腿一并记入)。

…3921)

Fail-first nail for rubash#3921 (residual of #3546):

- Zero-length `* (x)` / `?(x)` matches landing on a raw-byte
  marker pair must advance past the whole pair; today the advance
  copies one char and strands the cursor inside the pair, earning
  a phantom trailing replacement when the pair is the last unit.
- A literal U+E000 in user data must behave as one ordinary data
  character in the replace face (`?`, `[...]`, literal pattern,
  `#`/`%` removal, `${#}`); it must not fuse with a following
  payload-range char (U+E001) or split from its escape half.

GNU anchor: subst.c:9184 pat_subst() -> lib/glob/sm_loop.c:398
BRACKMATCH(); pat_subst:9286-9304 copies one character past a
zero-length match. Expectations are GNU bash 5.3.20 outputs under
LC_ALL=C.UTF-8.
…ry faces (Refs #3921)

Item 1 — zero-length-match advance: replace_parameter_pattern_inner
copied one char past a zero-length match (GNU pat_subst:9286-9304
copies one CHARACTER, and in GNU one carrier byte / one U+E000 char
is one character). A raw-byte marker pair (U+E000 + payload) or a
literal-char escape pair (U+E400 + char) is one match unit, so the
advance now lands on the next unit boundary from the #3920 finder
table instead of stranding the cursor inside the pair.

Item 2 — literal U+E000 user data, two faces, both oracle-verified
against GNU bash 5.3.20 (LC_ALL=C.UTF-8):
(a) `printf -v` stored the formatted output raw: the lone U+E000 +
payload was read as a raw-byte marker pair at the first decode
boundary, so `printf -v v '\uE000\uE001'` stored one NUL byte
(len 1, plain expansion NUL, child-env spawn failure). The output
is now value-entry encoded (entry_encode_value_text, the
bytes_to_shell_text parity every other value channel uses) at the
last step before storage, in all four assign_printf_output arms
(scalar, indexed, assoc, nameref).
(b) `declare -p` double-decoded: decode_to_visible_text folds E400
pairs to lone U+E000s, and the byte-view quoters (strtrans.c
ansic_quote port, setattr.def:528-531) re-folded a lone U+E000 +
payload-range char into a byte, so `declare -p` showed `$'\000'`
for a correctly stored U+E000 U+E001. The display quoters
(quote_declare_value, quote_array_element_value,
quote_declare_display_value, quote_assoc_display_key,
quote_listing_value) now re-spell the visible text as transport
text before quoting, so the exactly-once decode yields the visible
chars. The genuine-pair arm is unchanged (rubash#540/#1060).

Pins: the two item-1 cells were already fail-first red; ten new
item-2 cells (printf -v storage + declare -p display) are red on
the unfixed base and green with this fix.
…ld, printf flush, format escapes, @q (Refs #3921)

- Array declare -p: fold_indexed_storage_dupes verbatim path decoded
  E400 pairs blanket to visible text; the flush then folded the lone
  U+E000+payload pair to NUL. New decode_bare_sentinels_keep_raw_byte_pairs
  keeps pairs in the raw-byte marker range (U+E000..=U+E100) as transport
  and resolves the rest (U+E314 still displays as one char).
- printf direct output (D1): printf Text segments are final bytes after
  raw_bytes; the direct flush decoded them a second time. OrderedOutputLog
  now carries stdout_final_bytes (printf only); the flush skips the fd-1
  decode for it, stderr still decodes.
- printf \u/\U and %b (D2): unicode_escape_text E400-escapes a single-char
  marker-zone result so raw_bytes restores the char; marker pairs pass
  through untouched; u32cconv_utf8_text itself unchanged (IFS needs the
  bare char).
- ${var@Q} round-trip: shell_reusable_quote re-spells its visible quoted
  form as transport text, so `q="${w2@Q}"` stores the 4-char form (GNU
  len 4) instead of folding to quote+NUL+quote.

GNU anchors: subst.c:9184 pat_subst / pat_subst:9286-9304 (item-1 advance);
printf.def -v / strtrans.c ansic_quote (display faces);
subst.c string_var_assignment and parameter_brace_transform (@Q/@A renders).
…-encode it (Refs #3921)

The D2 fix made the rendered output text uniformly transport text
(format escapes arrive E400-escaped, %s args verbatim), but
assign_printf_output still ran entry_encode_value_text over it, which
E400-escapes every marker-zone char and double-escaped the pairs:
`printf -v v '\uE000\uE001'` stored 4 chars (stem len cell: left 4,
right 2), and `printf -v v '%s' "$w2"` did the same to the argument.
printf_output_to_storage round-trips through the final bytes instead:
identity on well-formed transport, and it normalizes any lone marker
char a conversion emitted (%C) exactly the way the write path folds
it — the variable holds what printf prints. Four new pin cells lock
the %s-argument faces (PUA pair + genuine 0x14 byte).
…#3921)

The #3921 quoter fix re-spells declare -p output as transport text and
lets the stdout sink decode it once. The debugger inspection snapshot
carries the same rendered line but has no sink: the E400 introducer
leaked into the 'info variables' transcript
(lib debugger::cli_tests::print_renders_decoded_carrier_values).
Decode the rendered line at the snapshot boundary so it reads exactly
as 'declare -p NAME' prints it.
@caomengxuan666
caomengxuan666 force-pushed the fix/3921-bracket-zero-length-pua-boundary branch from af5e3eb to 8ada639 Compare November 18, 2026 20:46
@caomengxuan666

Copy link
Copy Markdown
Member Author

队长复核(头 8ada639,run 44394576724,已 settled):待改。

进展:debugger lib 格已修(macOS 全绿、Linux lib 无红);自带钉 9/9 全绿;goldens 仅基线二名;CLI / cross / darwin / Windows / shell / ledger / niu 全绿。rebase 到现尖 667c629 干净(零文件交集)。

仍红(census 85,尖 77;新增 8 格,修复 0 格):

  • PUA 面回归 6 格,与上一头一字不差:issue2884 ×3(env import 编码)、issue824(marker decode 回绕,Rust tests 腿同红此格)、issue843(patsub 转义字形)、issue1927 literal_typed_e000_unchanged(typed glyph)。
  • issue2340(wait/jobs 抖动族)与 issue2883(read-prompt 抖动族)按抖动处理;issue2771 本头已翻绿。

结论:item-1/item-2 本身已绿,但边界改动在 4 个 PUA 消费面(env import、marker decode、patsub 转义、typed glyph)上仍带 6 格回归——逐面收窄的方向不变:每一面先对 GNU 5.3.20 定“字面 PUA 字符何时是数据、何时是载体”,再改实现,不要再扩边界。本轮车道已交付,返工由续作车道接手。

The stdout flush decode (decode_raw_byte_marker_bytes) runs only when
the chunk contains a bare U+E000; otherwise it returns the chunk
unchanged. The blanket escape_marker_zone_chars in the five display
quoters re-spelled visible text as transport text unconditionally, so
for values with no U+E000 the E400 introducers leaked verbatim in
front of every glyph (issue2884 x3, issue824, issue843: declare -p
showed U+E400 U+E301 for a stored U+E301). Re-spell as transport only
when the visible text contains U+E000 (then the flush decodes exactly
once); otherwise feed the visible text as-is (pre-PR behaviour).
push_existing_literal_pair excluded a following U+E000, on the
assumption that an E400+E000 pair is the raw-byte pair protocol. It
is not: raw-byte pairs are E000-FIRST (E000 + E001..E100 payload) and
can never start with E400; an E400-first pair in transport text is
always the literal-char escape of a typed glyph. The exclusion made
the mark pass re-escape the pair (E400 -> E400 E400, E000 verbatim),
so a typed ${v/b/\u{E000}x} replacement came out double-escaped
(issue1927 literal_typed_e000_unchanged printed U+E400 U+E000 x).
A bare E000 still rides push_replacement_literal's verbatim arm.
@caomengxuan666

Copy link
Copy Markdown
Member Author

队长复核(头 f46d5c8,run 44405772328,已 settled):可合。

返工车道仍在跑收尾腿/REPORT——待其交卷且头未再动,即合。

Whitespace-only: wrap the four escape_marker_zone_chars_for_display
call sites to the 100-column limit. No semantic change.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 18, 2026 22:23
@caomengxuan666
caomengxuan666 merged commit 9b4bb3b into master Nov 18, 2026
10 of 12 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