Repository navigation
fix(read): decode input as UTF-8 instead of Latin-1 - #326
Merged
ferdingler merged 2 commits intoNov 21, 2026
Merged
Conversation
The read builtin pushed each input byte as a char, so every non-ASCII byte of a multi-byte UTF-8 sequence became its own Latin-1 character ("café" came back as "café"). Collect raw bytes and decode them once with String::from_utf8_lossy. On the timeout path, put the original bytes back into the channel buffer instead of re-encoding chars as u8, which would have corrupted them the same way. Fixes strands-agents#255.
Collaborator
|
@strandly-the-agent review this PR |
This was referenced Nov 20, 2026
[BUG]
echo -e prints \xHH, \e and \uHHHH literally and writes \0NNN bytes above 0177 as Latin-1
#336
Open
strandly-the-agent
approved these changes
Nov 20, 2026
strandly-the-agent
left a comment
Collaborator
There was a problem hiding this comment.
Approve. This fixes #255, and I found no defects at 0a4968f. @ferdingler
- Tests:
cargo test --workspace --all-targetspasses locally (2,680 passed, 0 failed, 0 ignored), andcargo fmt --checkandcargo clippyare clean.ci.ymlhas not run on this PR (the head commit shows only the label checks), so this local run is the only suite result for it. I did not run the Python or Node suites. - One behaviour change to accept knowingly: input that isn't valid UTF-8 now becomes U+FFFD, with no warning. Base transcoded it from Latin-1, so
readover a Latin-1 file gaveJoséand now givesJos�. This matches$(…). No test covers it; the inline comment has one. - Merge order: #307 (
read -d) edits the same lines and still hasline.push(byte[0] as char). Whichever lands second has to keep this fix.
What I verified
Head 0a4968f, base 9455e9f (merge base with main), each built into its own target directory. Rust 1.100.0, Debian 12 aarch64, debug builds.
- The issue's two repros.
echo café | { read l; echo "[$l]"; }andecho café | while read l; do echo "[$l]"; doneprint[café]on base and[café]on head. - The new tests prove the fix.
read_utf8_multibyteandread_utf8_multibyte_fieldsfail with base'sread.rsand pass on head. - Timeout path (
read.rs:58-64). A timeout that lands inside a multi-byte character keeps the bytes exactly, and the nextreaddecodes the completed character (caf\xC3, timeout,\xA9\n, thenreadgivescafé). This was run through the Rust API (set_outside_reader), the only surface where a later read can resume. Base kept the bytes exactly too. - Lines after the decode.
\rstripping is unchanged. Field splitting is better:IFS=éandIFS=│now split, andIFS=©no longer splits insideé. The slice at&trimmed[pos..]is safe becauseposcomes fromfindon a validstr. - Hostile input on head. Lone
0x80/0xFF, a truncated sequence at end of input, overlong and surrogate encodings, NUL and a BOM: no panic, one U+FFFD per invalid byte. A 12,000-case random comparison against "lossy UTF-8 decode plus the unchanged split code" had no mismatch. - Other input forms. Here-doc,
< file, pipeline,-r,-p,REPLY, CRLF and end of input without a newline behave as on base apart from the decoding.
Not run: Python and Node suites, wasm32, macOS, Windows, release builds.
Appendix — non-blocking (3)
- PR description. It says the timeout path "had the same problem". It did not: on base every
charwas at most U+00FF, soline.chars().map(|c| c as u8)put the original bytes back. The change atread.rs:61is still needed, becauselineno longer exists there. Worth correcting if the description becomes the commit message. - Timeout put-back. Only an ASCII test covers it (
tests/shell_integration.rs:9679). It works with multi-byte input, as above. - Pre-existing, filed: #336 (
echo -eescapes), #337 (a group or subshell can't be piped into a loop or group), #338 (while read … < fileis quadratic in the file size).
Collaborator
|
@strandly-the-agent Please review this PR |
strandly-the-agent
approved these changes
Nov 21, 2026
strandly-the-agent
left a comment
Collaborator
There was a problem hiding this comment.
Approve at 2406f69. @ferdingler
- Previous finding: fixed. The new commit adds the two suggested tests and changes nothing else.
read_invalid_utf8_is_replacedfails whenread.rs:74is made strict (String::from_utf8(bytes).expect(…)), and all fourreadUTF-8 tests fail with base'sread.rs. - Tests:
cargo test --workspace --all-targetspasses on this head (2,682 passed, 0 failed, 0 ignored), and CI is now green on every Rust, Python and Node job. A trial merge intomainat2664aa7is clean and passes as well (2,709 passed). - Correction to my last review: I called
cargo clippyclean. It exits 0 but prints 14 warnings, the same ones on0a4968fand2406f69. None are inread.rsor in the tests this PR adds, and CI does not gate on clippy.
Two notes from the last review still stand, neither blocking: the PR description says the timeout path "had the same problem" (it did not), and #307 edits the same lines and still has line.push(byte[0] as char).
ferdingler
approved these changes
Nov 21, 2026
10 tasks done
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.
Description
readpushed every input byte as achar(line.push(byte[0] as char)), so each byte of a multi-byte UTF-8 sequence became its own Latin-1 character:echo café | { read l; echo "$l"; }printedcafé. The timeout path had the same problem when it put the partial line back into the channel buffer (line.chars().map(|c| c as u8)).This collects the raw bytes in a
Vec<u8>and decodes them once withString::from_utf8_lossyafter the line is complete. On timeout the original bytes are restored as-is. Onlysrc/builtins/read.rschanges; the field splitting, IFS handling and backslash continuation below it are untouched.I did not comment on the issue before starting, since this is a small, self-contained fix. Happy to adjust if you would rather handle it differently.
Related Issues
Fixes #255
Documentation PR
N/A
Type of Change
Bug fix
Testing
Added two tests to
tests/shell_integration.rs:read_utf8_multibyte(café 日本語 😀into one variable) andread_utf8_multibyte_fields(multi-byte text split across two variables).I could not build or run the suites locally, so I verified with the CI workflow on my fork (same
ci.yml, triggered by a pull request inside the fork). The Rust jobs on ubuntu, macOS and Windows, the Python jobs (3.10 to 3.14) and the Node.js jobs (20, 22, 24) all passed. I have not runcargo fmtorcargo clippylocally.cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.