Repository navigation
read: byte-domain record scan for joined -d delimiters (Refs #3212) - #3290
Merged
Merged
Conversation
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.
This was referenced Nov 10, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 逐字不变:read_function_stdin):门触发时以扫描供记录,余部重编码回写镜像(切分可令下一记录始于一个字符的内部);slurped_record,门触发走扫描,否则复演原 verdict 与 trim 调用对。多字节 locale:operative 字节取分隔符字符 UTF-8 编码的首字节——续字节永不在字符中间截断(read_mbchar 消费),首字节仍可切开任何携带它的字符。
本 PR 删除了什么
slurped_record_eof_no_delimiter加trim_read_input调用对,由slurped_record漏斗取代(两文件净删约 120 行)。slurped_record_eof_no_delimiter本体保留,漏斗的 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 全绿。RUSTFLAGS="-D warnings"rc=0、cargo check --testsrc=0、fmt rc=0、marker-lint rc=0、UTF-8 边界卫生全树 rc=0。残余(本 PR 未收口,故仅 Refs)
a C3 A9 b C3 A9、分隔字为 é 的文件重定向循环读,GNU 给出两条记录<a>与<$'\251b'>,头树给出<a>与<b>;基底同形,属既存分歧而非本 PR 引入。该面是流式字节读取器,与读族后续专项(issue 3191 一带)相邻,建议随读族专项处理;探针证据见施工报告fix-3212/REPORT.md。-d词(如-d $'\351')与同值 widened 字符的混同:read_delimiter_char既有契约由全部 serve 共担,非本 PR 引入。-ttimed judge(镜像改写后长度已变)。-n预算落在已非法数据内部时的切点(至多差一字节)。