Skip to content

fix(read): count decoded units for read -n/-N carrier-pair limits (#714) - #775

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/714-read-limit-decoded-units
Oct 17, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/714-read-limit-decoded-units

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

read -n N / -N N bounded 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.

printf 'AB\033C' | { read -n3 c; printf %s "$c" | od -An -tx1; }
# before: 41 42 ee 80 80      GNU bash 5.3: 41 42 1b

Root cause

The limit was applied twice, both times on transport text:

  1. read_function_stdin (src/executor/read_io.rs) walked the FUNCTION_STDIN carrier mirror with slice.char_indices() and counted output.chars().count(), so E000+E01C (ESC) consumed two units and the loop could stop between them.
  2. trim_read_input_literal (src/executor/read_helpers.rs) then re-truncated the record with input.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;, with builtins/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) and tag_read_carrier_text normalizes 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_units replaces the multibyte chars().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 names src/builtins/read_io.rs; in this tree the read builtin lives at src/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:

case GNU before after
-n3 on AB\033C 41 42 1b 41 42 ee 80 80 41 42 1b
-n2 on A\033B (pair at boundary) 41 1b 41 ee 80 80 41 1b
-n2 on AB\033C (pair excluded) 41 42 41 42 41 42
-n4 on AB\033C 41 42 1b 43 41 42 1b 41 42 1b 43
-N3 on AB\033C 41 42 1b 41 42 ee 80 80 41 42 1b
-n3 on \033\014\021X 1b 0c 11 1b ee 80 80 1b 0c 11
-n3 on abcdef 61 62 63 61 62 63 61 62 63
-n2 on \303\251Z (multibyte) c3 a9 5a c3 a9 5a c3 a9 5a

Out of scope (pre-existing, unchanged)

-N drops an embedded newline on the mirror path (printf 'AB\nC' | { read -N3 c; ... } → 41 42 vs GNU 41 42 0a). Reproduces identically on unpatched origin/master; it is delimiter handling, not carrier-unit accounting.

Closes #714.

`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.
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.

read -n/-N: limit counts transport units, splitting the last kept carrier pair

1 participant