Skip to content

fix(cond): decode raw-byte pairs at the [[ =~ ]] regex boundary (Refs #3545) - #3931

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3545-regex-operand-carrier-bytes
Nov 18, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3545-regex-operand-carrier-bytes

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

根因

载体字节(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。

改动

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

本 PR 删除了什么

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

UNVERIFIED-BY-CAPTAIN

Fail-first nails for #3545: carrier bytes (0x0c, 0x14..=0x1f)
in the regex operand — $'...' literal form and ${c} after '|'
— must match as GNU 5.3.20 does (execute_cmd.c:4068/4096).
UNVERIFIED-BY-CAPTAIN
…3545)

Carrier bytes (0x0c, 0x11, 0x13, 0x14..=0x1f) ride rubash transport
as U+E000 raw-byte pairs, but decode_command_substitution_payload
only restores the U+E319 comsub sentinel. The quoted regex path
(regex_rhs_marked_chars via shell_text_to_raw_bytes) decoded the
pattern to raw bytes while the subject stayed pair-encoded, so
$'...' literals and ${c} after '|' never matched; the unquoted
path only matched by accident of both sides staying encoded. Decode
both spellings at the regex boundary — pattern in
compile_conditional_regex_uncached, subject at the three match
entry points — so pattern and subject meet as the same raw bytes,
the normalization the #729 glob side already applies.

GNU: execute_cmd.c:4068 cond_expand_word (mode 2) + :4096
sh_regmatch receive the expanded operands as raw bytes;
subst.c:4114 QGLOB_REGEXP only literalizes quoted metacharacters.

UNVERIFIED-BY-CAPTAIN
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 18, 2026 19:43
@caomengxuan666
caomengxuan666 merged commit 36cc7d9 into master Nov 18, 2026
10 of 12 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 18, 2026
fix(exec): classify empty command word as not found 127 (Refs #3317)

## 根因

`exec ""` 应报 not found、rc 127,rubash 报 `cannot execute: Is a directory`、rc 126。

根因在 `src/builtins/exec.rs` 的 as-written 目录探针(rubash#2661 引入):探针把操作数 join 到 session cwd 后再 `stat`。`cwd.join("")` 本身就是 cwd(一个目录),于是空词被读作 cwd 目录、误分类为 EX_NOEXEC(126)。

## GNU 锚点

- `builtins/exec.def:155-168`:搜索失败(command == 0)时,`file_isdir(args[0])` 决定 126 vs `sh_notfound` / EX_NOTFOUND(127)。
- `general.c:812-817`:`file_isdir` 即 `stat(fn) == 0 && S_ISDIR`;`stat("")` 以 ENOENT 失败,所以空词不是目录,是 not found。
- 探针的其余部分不变:`exec .` / `exec somedir` 仍为 126(#2661 钉守护);slash 分支与 PATH 查找不动;普通外部命令分类器本来就对无分隔符的空词返回 None、报 127,不受影响。

## 修复

空词跳过目录探针(`stat("")` 永远失败);非空词的 #2661 探针原样保留。只改 `src/builtins/exec.rs` 一个文件。

## 回归钉

`tests/issue3317_exec_empty_command.rs`(8 钉):`exec ""`、`exec ''`、带额外参数、子 shell、函数内、`shopt -s execfail` 形态(`after rc=127`),另加真不存在路径(127)与 `exec .`(126)两个对照。

Fail-first(新服务器 [email protected],GNU bash 5.3.9 作 oracle,commit 6d9f2cb):6 红 2 绿——6 个空词钉全红(报 126),2 个对照钉绿;GNU oracle 同形全为 127 / execfail `after rc=127`。

绿腿(同机,commit db40bb5,`-j 1`,`scrubbed_cargo.py`):

- 钉 stem `issue3317_exec_empty_command`:**8/8 绿**。
- 邻茎:`issue2661` 5/5、`issue3316` 7/7、`issue3148` 8/8 与 14/14 全绿;`issue3310` 20/21,唯一红为 `issue3310_plain_spawn_env_keeps_live_level`(plain-spawn 的子进程 SHLVL 得 6、钉期望 5;GNU oracle:`$SHLVL` 读 6、子进程 env 得 5)。
- 该红与本 PR 无关:它走 plain-spawn 路径(`rubash -c /usr/bin/env`,不进 exec 内置),且在 base `db580be6` 的 worktree 上单测复现完全相同(0/1 红)——**基底已红**,请队长另行处置(疑似 #3310 的 SHLVL 导出面残差)。
- 全量 lib:1280 passed / 0 failed / 4 ignored(fix 提交 03f9076);`RUSTFLAGS="-D warnings" cargo check --locked --lib --bins` rc=0(同提交)。db40bb5f 相对 03f9076 仅为空白格式 + 钉期望行号修正,lib 全量未在该提交上重跑(当时构建槽已被 #3917 车道占用,未抢槽)——请队长复核时补跑。
- `rustfmt --check`(两改动文件):rc=0。整仓 hygiene 脚本 `scripts/check-rustfmt.py` 仍报 3/12 批有 diff,但剩余 diff 全在未动文件(`pipeline_exec.rs`、`shell_options.rs`、`issue3546`、`issue3894` 钉)——基底既有格式漂移,非本 PR 引入,不在本 PR 里顺手改。

未完成腿:Windows 面(X299 反向隧道仍断,2026-11-19 02:42 实测 2666 refused)**PENDING**;box(钱包空、休眠)与老服务器(须经 box 跳转)均不可达,本轮 Linux 腿走新服务器。

## 本 PR 删除了什么

无。未删除任何文件、函数、测试或公开面;只把目录探针的适用域收窄到非空词。`docs/api-ledger.md` 未动;未碰 `src/lexer/continuation.rs`。

UNVERIFIED-BY-CAPTAIN


## 队长复核(头 db40bb5,run 44377890814)

- census **79**,唯一新增为 issue2340 抖动族一格;自带钉 **8/8 绿**;issue2883 一格翻绿。
- goldens 仅基线二名(lib 1383/0 通过);CLI 仅 coproc 抖动格;darwin 作业 cancelled/steps=[](未运行,非产品红);其余全绿。
- 服务器腿:fail-first 6 红 2 绿、绿腿 8/8、邻茎 issue2661/3316/3148 全绿、-D warnings rc=0、rustfmt 干净;**全量 lib 在 fixup 头 db40bb5 上补跑确认:1280 passed / 0 failed / 4 ignored(LEG-RC=0)**。
- 邻茎红 `issue3310_plain_spawn_env_keeps_live_level` 已另立 **rubash #3932**(plain-spawn SHLVL 导出残差),不属本 PR。
- 与 #3931 合入点零交集(程序化核验:db580be6...36cc7d9 仅改 conditional.rs + #3545 钉),故不经 update-branch 直接合。
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