Skip to content

read: byte-domain record scan for joined -d delimiters (Refs #3212) - #3290

Merged
caomengxuan666 merged 4 commits into
masterfrom
fix/3212-read-d-joined
Nov 10, 2026
Merged

caomengxuan666 merged 4 commits into
masterfrom
fix/3212-read-d-joined

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #3212

问题与机制

read -d 的分隔符字节与后续数据字节构成合法 UTF-8 时,该字节在传输文本中不可见:规范编码器 bytes_to_shell_text 把合法 UTF-8 跨度存为单个字面字符(流字节 C3 A9 存为单个字符 é),分隔符字节 C3 在 needle、single-byte marker、literal 三种可测形态中皆不存在,管道镜像与 whole-text slurp(heredoc、here-string、文件、procsub)一律读到 EOF 并丢失记录。

GNU 口径(builtins/read.def):分隔符是 -d 参数的第一个字节(read.def:385),主循环在 read_mbchar 消费多字节字符余部之前先逐字节比对(read.def:834);-n 预算数转义后的单位(read.def:874-886)。

修法

read_helpers.rs 新增字节域记录扫描 scan_text_record_bytes:对 decode_raw_byte_markers 的输出复演 GNU 循环(字节比对、pass_next 转义单位、被转义换行不耗预算、转义后单位计预算、单位步进按 locale 为单字节或一个 UTF-8 字符跨度),记录与余部均以 bytes_to_shell_text 重编码。窄门 joined_delimiter_scan_applies 仅在 joined 分歧可能处接合,未接合的读保持原 walker 逐字不变:

  • 镜像 serve(read_function_stdin):门触发时以扫描供记录,余部重编码回写镜像(切分可令下一记录始于一个字符的内部);
  • 全部 12 处 whole-text slurp 站点(read_io.rs 6 处、read_redirected_fd.rs 6 处)改走共享漏斗 slurped_record,门触发走扫描,否则复演原 verdict 与 trim 调用对。

多字节 locale:operative 字节取分隔符字符 UTF-8 编码的首字节——续字节永不在字符中间截断(read_mbchar 消费),首字节仍可切开任何携带它的字符。

本 PR 删除了什么

  • read_io.rs 与 read_redirected_fd.rs 中 12 处重复的 slurped_record_eof_no_delimiter 加 trim_read_input 调用对,由 slurped_record 漏斗取代(两文件净删约 120 行)。slurped_record_eof_no_delimiter 本体保留,漏斗的 legacy 支仍调用它。
  • 无其他删除:未删任何函数、测试或行为路径;legacy 路径对未接合输入逐字保留。

验证(Simular box,基底 d955590,头 617c287)

  • 钉 tests/issue3212_read_d_joined_delimiter.rs 共 14 格(C locale 10 格加 C.UTF-8 4 格):基底 fail-first 为 9 红 5 绿,红格逐名恰为缺陷面(镜像 5、slurp 2、多字节 locale 2),绿格为文件循环(字节面本对)、-n 字节预算、缺席对照与多字节两对照;头 14/14 全绿。
  • 读族邻茎 14 茎:13 茎首跑全绿;issue2662 一格首跑单格红(退出码断言),头侧连跑 3 次全绿、头与基底二进制直复现同形均正确、基底该茎亦绿,按 flake 定谳(2 vCPU 盒并行负载)。
  • lib 双边:头 1233 过 0 红 4 忽略、基底同数,失败名集差为零。
  • 五腿全跑:RUSTFLAGS="-D warnings" rc=0、cargo check --tests rc=0、fmt rc=0、marker-lint rc=0、UTF-8 边界卫生全树 rc=0。

残余(本 PR 未收口,故仅 Refs)

  1. C.UTF-8 文件循环(fd_table 字节面的多字节 serve):流 a C3 A9 b C3 A9、分隔字为 é 的文件重定向循环读,GNU 给出两条记录 <a> 与 <$'\251b'>,头树给出 <a> 与 <b>;基底同形,属既存分歧而非本 PR 引入。该面是流式字节读取器,与读族后续专项(issue 3191 一带)相邻,建议随读族专项处理;探针证据见施工报告 fix-3212/REPORT.md。
  2. 多字节 locale 下字节派生高字节 -d 词(如 -d $'\351')与同值 widened 字符的混同:read_delimiter_char 既有契约由全部 serve 共担,非本 PR 引入。
  3. 镜像 byte-served 记录跳过 -t timed judge(镜像改写后长度已变)。
  4. 非法 UTF-8 序列的单位长度是对 glibc mbrtowc 歩进的近似,仅影响多字节 locale 下 -n 预算落在已非法数据内部时的切点(至多差一字节)。

Four multibyte-locale cells measured against GNU bash 5.3.20:
pipe loop (delimiter byte splits the delimiter character itself,
tail byte leads the next record), other-char lead byte (stream
holds è, delimiter word names é — the shared lead byte splits),
plus two controls (continuation-byte delimiter never terminates
in a multibyte locale; delimiter character herestring).
GNU builtins/read.def:385 takes the delimiter as the FIRST BYTE of
the -d word and read.def:834 compares it against raw stream bytes
before read_mbchar consumes the rest of a multibyte character. The
text serves could not see that byte whenever the canonical storage
encoder had joined it into a literal character (0xC3 0xA9 stored as
the single char é), so pipeline mirror reads and whole-text slurps
(heredoc, here-string, file, procsub) read to EOF and lost the
record (rubash#3212).

Add a byte-domain record scanner in read_helpers.rs that replays
the GNU loop over decode_raw_byte_markers output: byte-wise
delimiter comparison, backslash pass_next units (escaped newline
costs no budget), post-escape unit counting for -n/-N (characters
in a multibyte locale, bytes in a byte locale), and re-encoding of
record and remainder with bytes_to_shell_text. A narrow gate
(joined_delimiter_scan_applies) engages it only where a joined
divergence is possible, so unaffected reads keep the historical
walkers untouched:

- read_function_stdin (the pipeline mirror) serves the record from
  the scan and writes the unconsumed tail back re-encoded, since
  the split can leave the next record starting inside a character;
- every whole-text slurp site in read_io.rs and
  read_redirected_fd.rs now derives (record, eof_no_delimiter) from
  the shared slurped_record funnel, which byte-serves gated inputs
  and otherwise replays the historical verdict+trim pair.

Multibyte locale: the operative byte is the first byte of the
delimiter character's UTF-8 encoding, so a continuation byte never
terminates a record mid-character (read_mbchar consumption), while
the lead byte still splits any character that carries it.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 10, 2026 20:46
@caomengxuan666
caomengxuan666 merged commit a7d125b into master Nov 10, 2026
11 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