Repository navigation
fix(read): count decoded units for read -n/-N carrier-pair limits (#714) - #775
Merged
Merged
Conversation
`read -n N` / `-N N` bounded the record by transport characters, so a raw-byte carrier pair (U+E000 + U+E001..=U+E100 — the internal encoding of one input byte) counted as two. A limit that landed inside the pair emitted the bare introducer (`41 42 ee 80 80` for `AB\033C`) and a limit one past the pair dropped the byte after it; GNU yields `41 42 1b`. GNU counts one per decoded character and never cuts a character: builtins/read.def:884 `nr++`, builtins/read.def:886 `if (nchars > 0 && nr >= nchars) break;`, with builtins/read.def:874 `i += read_mbchar(...)` consuming a multibyte character whole. Apply that same unit discipline — the one the buffered splitter already uses (`read_split::logical_units`) and `tag_read_carrier_text` normalizes to: - `src/executor/read_io.rs` `read_function_stdin`: walk the FUNCTION_STDIN mirror in decoded units; a carrier pair is emitted whole and consumes one unit of the budget, so the cursor can never land mid-pair. - `src/executor/read_helpers.rs` `trim_read_input_literal`: replace the multibyte `input.chars().take(limit)` with `truncate_read_units`, which counts a pair once and never splits it (this second truncation was the split that survived the reader loop). Scope: read-builtin limit accounting only. The redirection engine (#726) and `src/invocation.rs` (#705) are untouched. Reference: WSL GNU bash 5.3.0, scripts run from a file. Regression cover in tests/issue714_read_limit_transport_units.rs: -n cut at/inside/after a pair, -N exact limit, several consecutive carriers, plain-ASCII and multibyte guards, and the issue's outer-pipe repro. The pre-existing `-N` trailing-newline drop on the mirror path is unrelated and unchanged.
This was referenced Oct 17, 2026
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.
Summary
read -n N/-N Nbounded the record by transport characters, so a raw-byte carrier pair (U+E000+U+E0xx, the internal encoding of one input byte) counted as two. A limit that landed inside the pair emitted the bare introducer, and a limit one past the pair dropped the following byte.Root cause
The limit was applied twice, both times on transport text:
read_function_stdin(src/executor/read_io.rs) walked theFUNCTION_STDINcarrier mirror withslice.char_indices()and countedoutput.chars().count(), soE000+E01C(ESC) consumed two units and the loop could stop between them.trim_read_input_literal(src/executor/read_helpers.rs) then re-truncated the record withinput.chars().take(limit), which split the surviving pair.GNU counts one per decoded character and never cuts a character:
builtins/read.def:884 nr++,builtins/read.def:886 if (nchars > 0 && nr >= nchars) break;, withbuiltins/read.def:874 i += read_mbchar(...)consuming a multibyte character whole.Fix
Apply the same decoded-unit discipline the buffered splitter already uses (
read_split::logical_units) andtag_read_carrier_textnormalizes to:read_function_stdin: iterate decoded units; a carrier pair is emitted whole and consumes exactly one unit of the budget.trim_read_input_literal:truncate_read_unitsreplaces the multibytechars().take(limit); a pair counts once and is never split.Scope is the read builtin's limit accounting only. The redirection engine (#726) and
src/invocation.rs(#705) are untouched. (The issue text namessrc/builtins/read_io.rs; in this tree the read builtin lives atsrc/executor/read_io.rs+src/executor/read_helpers.rs.)Verification
Byte-for-byte against genuine WSL GNU bash 5.3.0, scripts run from a file:
-n3onAB\033C41 42 1b41 42 ee 80 8041 42 1b-n2onA\033B(pair at boundary)41 1b41 ee 80 8041 1b-n2onAB\033C(pair excluded)41 4241 4241 42-n4onAB\033C41 42 1b 4341 42 1b41 42 1b 43-N3onAB\033C41 42 1b41 42 ee 80 8041 42 1b-n3on\033\014\021X1b 0c 111b ee 80 801b 0c 11-n3onabcdef61 62 6361 62 6361 62 63-n2on\303\251Z(multibyte)c3 a9 5ac3 a9 5ac3 a9 5atests/issue714_read_limit_transport_units.rs(8 tests): fails 5/8 on unpatchedorigin/master, passes 8/8 on the fix. Covers-nat/inside/after a pair,-N, consecutive carriers, plain-ASCII and multibyte guards, and the outer-pipe repro. Child CWD is sandboxed (per chore(tests): drop accidentally committed f27x probe files and sandbox their producers #763/test(issue540): sandbox the probe script CWD (root 'f' producer) #765).issue165,issue275,issue276,issue277,issue279,issue280,issue353,issue325,issue700,issue701,stdin_multibyte,issue282,issue419,issue431,issue528.cargo fmt --checkclean.Out of scope (pre-existing, unchanged)
-Ndrops an embedded newline on the mirror path (printf 'AB\nC' | { read -N3 c; ... }→41 42vs GNU41 42 0a). Reproduces identically on unpatchedorigin/master; it is delimiter handling, not carrier-unit accounting.Closes #714.