Skip to content

fix(executor): read -n/-N counts bytes in a single-byte locale (Refs #3334) - #3477

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/3334-read-n-bytes-c-locale
Nov 15, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/3334-read-n-bytes-c-locale

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

根因

StdinCharDecoder::next_unit(src/executor/read_helpers.rs)的单字节分支在 !is_multi_byte() 时仍把合法 UTF-8 序列聚成一个 Char 单位,于是 LC_ALL=C 下 read -n 1 读到 é(c3 a9)时一次取走两个字节,-n 2 取走 é + x。

GNU 锚点

  • builtins/read.def:874:仅当 mb_cur_max > 1 时调用 read_mbchar 分组多字节字符;MB_CUR_MAX == 1 时不分组。
  • builtins/read.def:880-886:nr++ 按单位计,if (nchars > 0 && nr >= nchars) break; —— 单字节 locale 下每字节一个单位。
  • 另见 read.def:734 zreadn (fd, &c, nchars - nr)(-N 面同预算)。

改动

  • StdinCharDecoder::next_unit 单字节分支:一字节一单位(ASCII 仍为 Char,高位字节走 RawByte 单位,其 marker 文本原样重发该字节;stdin_unit_is_delimiter 对两种单位都按首字节比较,结果一致)。多字节分支未动。两个字节循环调用方(read_inherited_process_stdin、read_stdin_until)对两种单位对称处理,计数/回显/转义逻辑不变。
  • 回归钉:tests/issue3334_read_n_bytes_c_locale.rs(9 例:-n 1 / -n 2 / -N 1 / -N 2 + 连续三次读取定流位置 + ASCII / plain-read / C.utf8 对照),期望值取 issue 正文的 GNU 5.3.20 oracle。
  • scripts/check-utf8-boundary-hygiene.sh 增加 allowlist 条目:byte as char 仅出现在 if byte < 0x80 分支(else 分支把 >= 0x80 交给 raw_byte_unit),与既有 read_split.rs 条目同理。

本 PR 删除了什么

无。仅替换单字节分支的单位划分逻辑;无公开面、无文件、无功能删除。

未验项

UNVERIFIED-BY-CAPTAIN:本轮无任何构建/测试腿可用(本地构建禁令、Simular box 盘满、老服务器 ssh 未恢复、X299 腿留复核轮),未跑 cargo test、未跑 RUSTFLAGS="-D warnings" cargo check、未跑全量 lib 对比。仅做过:rustfmt --check(两处改动文件通过)、check-utf8-boundary-hygiene.sh(clean)、check-marker-literals.py(OK)。新钉为 fail-first 设计(在修前 master 上 6 例 C-locale 断言应失败,待复核轮跑钉验证)。

队长注意(同文件面残差,未动)

…3334)

StdinCharDecoder::next_unit grouped a valid UTF-8 sequence into one
Char unit even when !is_multi_byte(), so `read -n 1` over `é` (c3 a9)
under LC_ALL=C took both bytes. GNU builtins/read.def:874 only calls
read_mbchar when mb_cur_max > 1; with MB_CUR_MAX == 1 every byte is one
unit (nr++ per byte, read.def:880-886). The single-byte branch now
returns one byte per unit (ASCII stays Char, high bytes ride as
raw-byte units); the multibyte branch is untouched. The guarded
`byte as char` widening is allowlisted in
check-utf8-boundary-hygiene.sh (`if byte < 0x80` arm; the else arm
routes >= 0x80 to raw_byte_unit).

Regression nails: tests/issue3334_read_n_bytes_c_locale.rs (9 cases:
-n 1/-n 2/-N 1/-N 2 + sequential reads + ASCII/plain/C.utf8 controls),
seeded from the issue's GNU 5.3.20 oracle expectations.

UNVERIFIED-BY-CAPTAIN
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3477 第1轮 — 建议:PASS
head: 8b8770a base(merge-base): 3745712 文件: 3 个 +157/-46
关联 issue: #3334(状态:open,我核过的时间:2026-11-15 本地)

机制核查:

  • 单字节 locale 下一字节一单位,与 GNU 一致:read.def:851 mb_cur_max > 1 && is_basic (c) == 0 才走 read_mbchar(:873-874),单字节 locale 短路;:878 nr++ 每字节一次,:880 nr >= nchars break;-N 面 zreadn 同样逐字节返回(lib/sh/zread.c:255-281)。diff 把 !is_multi_byte() 分支改成 ASCII→Char、>=0x80→raw_byte_unit,两个消费方(read_stdin_until、read_inherited_process_stdin)对两种单位对称处理,转义/分隔符比较(stdin_unit_is_delimiter 按首字节)语义不变。机制正确。
  • 可疑/无关改动:check-utf8-boundary-hygiene.sh 只加了一条 allowlist 注释+正则条目,与代码改动配套,无顺手改无关面。

腿(逐项):

  • 钉自跑:issue3334_read_n_bytes_c_locale 9 passed / 0 failed
  • fail-first:同钉原样放 merge-base 跑 → 5 failed / 4 passed,红名 = {read_n1_takes_first_byte_in_c_locale, read_n2_takes_two_bytes_in_c_locale, read_upper_n1_takes_first_byte_in_c_locale, read_upper_n2_takes_two_bytes_in_c_locale, read_n_sequential_reads_consume_byte_by_byte_in_c_locale};对照全绿。差异注记:PR 正文称「6 例 C-locale 断言应失败」,实测红 5 例——正文计数与实测差 1,仅文档误差。
  • lib 双边:head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state} = base 失败名集(1349 passed / 1 failed / 4 ignored 两侧一致)→ 差集 ∅
  • -D warnings:lib+bins cargo check 0 警告;--all-targets 撞到 tests/issue2304_numbered_devnull_spawn_regressions.rs 的 dead_code(该文件不在本 PR diff 内,base 同构,基线既有)
  • 卫生:rustfmt --check 两个 .rs 文件 clean;marker-lint OK(117 consts/268 sites);utf8-boundary-hygiene clean(含本 PR 新增 allowlist 条目生效)
  • CI:3 红任务逐个归因——GNU regression goldens: issue2875_input_procsub_child_runs_as_rubash_shell 红=基线(1349/1);Rust tests (macOS): issue2875 红=基线(1242/1);Rust tests (Linux): issue2859_jobs_face_stopped_job_counts 红('Unrecognized option: internal-bg-wrapper')——与本 diff 无语义关联(read_helpers stdin 解码 vs jobs/wrapper flag),同 merge-base 其余 5 个 PR 与 master 同任务均只红 issue2875,疑 flaky/infra,建议队长复核但不计入本 PR
  • GNU 对照:wsl GNU bash 5.3.20(1)-release 脚本探针(target/devin-w8-audit/probe3334.sh):LC_ALL=C 下 -n1→c3 / -n2→c3a9 / -N1→c3 / -N2→c3a9 / 三连读→c3|a9|x;ascii -n1→61;plain C→c3a978;C.UTF-8 -n1→c3a9 / -n2→c3a978——与钉期望逐字节一致。补充残差探针(probe3334-residual.sh,Windows head 二进制):${#x} 后 read -n1=1、-n2=2、plain=3(GNU 同值);-d $'\xa9' 高字节分隔符命中(x=a+c3);反斜杠转义高字节计数正确——均一致。

F 点:无。

续作记录:无(未动代码)。

未跑/VOID 的腿:无。全部 9 腿已跑完并留证(探针脚本在 target/devin-w8-audit/)。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-BATCH-SUMMARY】
Wave 3 尾段钉批 6 个已全审完(worktree rubash-devin-w8;oracle = wsl /usr/local/bin/bash 5.3.20(1)-release,全部脚本文件探针):

PR 建议判词 钉质量核心结论
#3477 建议 PASS C-locale 逐字节语义与 read.def:851-880/zreadn 一致;fail-first 5 红全 fix-pin;lib 差集 ∅;CI 另有一例 issue2859(Linux)疑 flaky 已标注
#3478 建议 PASS validated_logical_pwd 作解析基与 pushd.def:296-302+cd.def:522ff+common.c:592 一致;WSL fail-first 4 红;GNU 6 形状逐字节一致
#3479 建议 PASS CDPATH tilde 展开与 cd.def:360 MP_DOTILDE→makepath.c:98 一致;WSL fail-first 6 红;GNU 8 形状全对(含 -P 逻辑公告+物理 PWD)
#3481 建议 PASS times fork-face baseline 栈设计正确(kind+depth 匹配 pop、lastpipe 豁免与 execute_cmd.c:2758 一致);fail-first 1 红精确;附 fmt 红 3 处(机械可修)
#3482 建议 FAIL 钉 16 格中 2 格在 head 仍红(Win+Linux 双侧一致):pipeline 元素 ambient 取不到止行(GNU=3,head=1);[[ ]] stdin procsub 被解析成 < 文件重定向(根本没 spawn)。钉期望本身经 GNU 核实无误
#3483 建议 PASS 6 处 trap-action 重解析改 source_text 原文通道,与 trap.c:496/1071/1109+ 一致;fail-first 7 红;GNU 六面+spacing 逐字节一致

阻塞/共用事实:

  • 全部 6 PR 的 CI 3 红均归因 api::consumer_tests::issue2875_input_procsub_child_runs_as_rubash_shell(已知基线);fix(executor): read -n/-N counts bytes in a single-byte locale (Refs #3334) #3477 Linux 任务多红一例 issue2859(疑 infra)。
  • 本地 lib 基线红 issue2084_session_end_restores_host_process_state 在 base 与全部 head 上一致,均未计入 PR 账。
  • 各 PR 评论含逐腿数字、fail-first 红名集、GNU 源码锚点与探针脚本路径(target/devin-w8-audit/)。
  • 本轮未 push 任何分支、未动代码。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 15, 2026 02:10
@caomengxuan666
caomengxuan666 merged commit e4ae08b into master Nov 15, 2026
8 of 11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 17, 2026
…3785) (#3810)

* test: pin read -n/-N byte counting in C locale on redirect/pipeline stdin (Refs #3785)

Regression nails for rubash#3785: read -n/-N must count BYTES in a
single-byte locale (GNU builtins/read.def:851,874,880-886) on every
stdin shape, not only inherited stdin (#3477 fixed that face only).
20 nails: < file, -N < file, -n 1, 0<file, EOF rc, <<< (-n/-N),
pipeline brace/parens/function (-n and -N), sequential -n 1 reads,
pipeline EOF rc, < /dev/stdin, plus controls (inherited stdin,
group redirect, C.utf8 char counting, ASCII, plain read).

* fix(executor): count bytes for read -n/-N in single-byte locale (Refs #3785)

GNU builtins/read.def:851,874 calls read_mbchar only when
mb_cur_max > 1; with MB_CUR_MAX == 1 every byte is one unit
(nr++ per byte, read.def:880-886). Three serves still counted
characters in LC_ALL=C:
- byte_unit_scan_applies: the FUNCTION_STDIN mirror scan now
  applies in a single-byte locale whenever the text holds a
  non-ASCII character (decoded_unit_len is 1 byte/unit there);
  pure ASCII keeps the historical character scan.
- slurped_record_eof_no_delimiter: the EOF verdict walk now
  charges ch.len_utf8() per unit in a single-byte locale, so a
  budget-satisfied read over a short input reports rc 0, not EOF.
- truncate_read_post_escape: the slurp cut now always runs in
  the decoded byte domain in a single-byte locale, so a cut may
  land mid-character (dangling lead byte rides a raw-byte marker).
No change in a multibyte locale. Regression nails:
tests/issue3785_read_n_bytes_redirect_pipeline.rs (20 tests).

* test: pin UTF-8 locale in #714 multibyte nail (Refs #3785)

The #714 nail read_n_multibyte_char_count_unchanged asserts
multibyte counting but never pinned the locale, so it ran under
whatever locale the test process carried. rubash#3785 made -n
count bytes in a single-byte locale (GNU read.def:851,874), so
under LC_ALL=C the same -n 2 over e-acute + Z takes just the
two bytes of e-acute and the nail fails. Pin LC_ALL=C.utf8 so
the nail asserts what its name claims on every machine.

---------

Co-authored-by: 曹梦轩 <[email protected]>
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