Skip to content

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

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/3785-read-bytes-c-locale
Nov 17, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/3785-read-bytes-c-locale

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

根因

在单字节 locale(LC_ALL=C)下,GNU read -n N / -N N 按字节计数(builtins/read.def:851,874,880-886:仅当 mb_cur_max > 1 才调 read_mbchar;否则每字节一个单位,nr++)。#3477(Refs #3334)只修了继承 stdin 的 StdinCharDecoder;其余三处 serve 仍按字符计数:

  • slurp 面(< file / 0<file / <<< / heredoc):trim_read_input → truncate_read_post_escape 的无 marker 走查数 chars();slurped_record_eof_no_delimiter 的 EOF 判定同法计数,导致预算满足的短输入误报 EOF(rc 1)。
  • 管道面(read_function_stdin 的 FUNCTION_STDIN 镜像):byte_unit_scan_applies 只在多字节 locale 且含 raw-byte marker 时才走字节域扫描。

修复(三处机制改动,均在 src/executor/read_helpers.rs)

  • byte_unit_scan_applies:单字节 locale 下镜像文本含非 ASCII 字符即走字节域扫描 scan_text_record_bytes;纯 ASCII 保持旧字符扫描。
  • slurped_record_eof_no_delimiter:单字节下单位代价为 ch.len_utf8(),与截断侧的字节域计数一致。
  • truncate_read_post_escape:单字节下无条件走解码字节域截断,切口可落在字符中间(悬空首字节以 raw-byte marker 承载)。

多字节 locale 行为不变。

回归钉

  • tests/issue3785_read_n_bytes_redirect_pipeline.rs(20 格):< file、-N < file、-n 1、0<file、EOF rc、<<<(-n/-N)、管道 brace/parens/function(-n 与 -N)、连续 -n 1 读、管道 EOF rc、< /dev/stdin,及对照(继承 stdin、组重定向、C.utf8 字符计数、ASCII、plain read)。
  • tests/issue714_read_limit_transport_units.rs:read_n_multibyte_char_count_unchanged 钉死 LC_ALL=C.utf8——该钉断言多字节计数,本修法后在单字节 locale 下同一 -n 2 只取 é 的两字节,钉的期望仅在 UTF-8 locale 下成立;此前它随测试进程的环境 locale 漂移。

验证(X299-2607,Windows MSVC,基底 5f32a7c)

  • 基底 fail-first:20 钉 7 通过 / 13 失败,13 红恰为 C-locale DIFF 面。
  • 修后:20 钉全绿;相关茎 issue3334(9)、issue3294(11)、issue1328(4)、issue714(8)、issue3212(14)、issue3293(4)、issue3489(4)、issue3191(8)、issue3650(7)全绿。
  • 全量 lib:基底与修后逐数对齐,均为 1368 通过 / 1 失败,唯一失败为 tip 既存 api::consumer_tests::issue2084_session_end_restores_host_process_state(基底同红,与本修法无关)。
  • RUSTFLAGS="-D warnings" cargo check --locked --lib --bins:零警告,rc 0。
  • rustfmt --check(三文件):干净,rc 0。
  • GNU oracle:vendored bash 5.3.20 上 issue 回归种子 10/10。

本 PR 删除了什么

无。仅新增测试文件与一处既有测试的 locale 钉死;产品代码为三处计数域改动,无删除、无公开面变化(未碰 docs/api-ledger.md,未碰 src/lexer/continuation.rs)。

UNVERIFIED-BY-CAPTAIN

…tdin (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).
…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).
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.
@caomengxuan666

Copy link
Copy Markdown
Member Author

【Y3-AUDIT】PR #3810 第1轮 — 建议:PASS
head: d749cf2 base(merge-base): 5f32a7c 文件: 3 个 +416/-15
关联 issue: #3785(状态:open,核于 2026-11-17 本地)

机制核查:

  • GNU 锚点核实:builtins/read.def:851-886——mb_cur_max > 1 && is_basic(c)==0 才走 read_mbchar,否则逐字节 nr++(:880-886 if (nchars > 0 && nr >= nchars) break)。单字节 locale(MB_CUR_MAX==1)下 -n/-N 预算确实按字节计,issue 描述属实。
  • byte_unit_scan_applies(read_helpers.rs:1068-1080):无预算→false;多字节→原 raw-byte-marker 门;单字节→text 含任一 >0x7F 字符走字节域。纯 ASCII 时字符域与字节域逐单位一致,门正确。
  • scan_text_record_bytes/decoded_unit_len(read_helpers.rs:930,1084):multi_byte=false 时 decoded_unit_len 恒返回 1 → 逐字节单位,与 GNU nr++/byte 一致。
  • slurped_record_eof_no_delimiter(read_helpers.rs:~1338):字面字符臂 unit_cost = is_multi_byte()?1:ch.len_utf8()——单字节 locale 下 é(2B)=2 单位;marker-pair 臂在 !is_multi_byte() 时 cursor+=2; units+=1(每 raw byte 1 单位),两臂口径一致。escaped 多字节字符按字节长计与 GNU「\ 吃下一字节、余下字节各自成单位」总数一致。
  • truncate_read_post_escape(read_helpers.rs:~1909):!is_multi_byte() || 有 marker → 字节域扫描;单字节 locale 下恒走字节域(含纯 ASCII,字节=字符无差)。截断可落在字符中间,残 byte 走 raw-byte marker——与既有 marker 形态一致。
  • issue714 改动仅把既有 multibyte 断言钉到 C.utf8(piped_read_locale),是测试卫生,非新钉。
  • diff 无无关改动;无桥接/翻译层。

腿:

  • 钉自跑:issue3785_read_n_bytes_redirect_pipeline head 20 passed / 0 failed;issue714_read_limit_transport_units head 8 passed / 0 failed
  • fail-first:merge-base 5f32a7c 上 issue3785 stem 7 passed / 13 failed——红名 = {read_n1_pipeline_sequential_reads_consume_byte_by_byte, read_n1_redirect_file_takes_first_byte_in_c_locale, read_n2_here_string_counts_bytes_in_c_locale, read_n2_pipeline_brace_counts_bytes_in_c_locale, read_n2_pipeline_function_counts_bytes_in_c_locale, read_n2_pipeline_parens_counts_bytes_in_c_locale, read_n2_pipeline_succeeds_at_budget_without_newline, read_n2_redirect_0file_counts_bytes_in_c_locale, read_n2_redirect_file_counts_bytes_in_c_locale, read_n2_redirect_file_succeeds_at_budget_without_newline, read_upper_n2_here_string_counts_bytes_in_c_locale, read_upper_n2_pipeline_brace_counts_bytes_in_c_locale, read_upper_n2_redirect_file_counts_bytes_in_c_locale},绿名 = 7 个 SAME/控制格(inherited/group-redirect/dev-stdin/utf8×2/ascii/plain-read)——与 issue [bug] read -n/-N counts characters instead of bytes in C locale on redirected / pipeline stdin (follow-up to #3334) #3785 的 DIFF/SAME 边界逐一吻合;issue714 head 版在 base 上 8/8(控制面,非 fail-first 钉)
  • lib 双边:head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state} / base 同名集 / 差集 {}(同 fix: Windows immediate wait prints the signal-death notice (Refs #3803) #3811 注:环境 flake,两侧皆红)
  • -D warnings:RUSTFLAGS="-D warnings" cargo check --locked --all-targets → Finished,0 警告
  • 卫生:rustfmt --edition 2021 --check(read_helpers.rs + 2 测试文件)rc=0;check-marker-literals.py OK;check-utf8-boundary-hygiene.sh clean
  • CI:红任务 = goldens(基线同名 issue_753::case_9 + upstream_suite_slice_snapshots)+ census(head 40 红 stem vs master 37:+issue1902(已知 flake)、+issue3255(fix: Windows immediate wait prints the signal-death notice (Refs #3803) #3811 census 同红,跨 PR 漂移)、+issue2340_wait_job_retention_regressions 仅 single_operand_wait_keeps_unwaited_job 一格红(sleep-30 时序敏感面,与本 PR 的 read_helpers 改动无语义交集,判 flake/漂移)、−issue371(已知 flake))+ Cross-target check (darwin) conclusion=cancelled(无 steps 无日志,infra 层取消,非编译失败——gh api 核实)→ 均非本 PR 账
  • GNU 对照:WSL bash 5.3.20(1)-release;脚本 target/devin-dispatch/probes/gnu-3785.sh:C locale 下 -n2/-N2 <file→c3 a9、-n1→c3、herestring/0<//dev/stdin/pipeline brace·parens·func/<<</group-redirect 全→c3 a9、两次 -n1→c3 a9、无 NL 预算满足→rc=0、plain read→整行;C.utf8 对照→-n2 取 2 字符 c3 a9 c3 a9——与钉断言逐字节一致

F 点:无

未跑/VOID 的腿:无

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 17, 2026 03:47
@caomengxuan666
caomengxuan666 merged commit 95edfaa into master Nov 17, 2026
9 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