Skip to content

fix(read): decode input as UTF-8 instead of Latin-1 - #326

Merged
ferdingler merged 2 commits into
strands-agents:mainfrom
momolly1024:fix/read-utf8-decoding
Nov 21, 2026
Merged

ferdingler merged 2 commits into
strands-agents:mainfrom
momolly1024:fix/read-utf8-decoding

Conversation

@momolly1024

@momolly1024 momolly1024 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Description

read pushed every input byte as a char (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"; } printed café. 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 with String::from_utf8_lossy after the line is complete. On timeout the original bytes are restored as-is. Only src/builtins/read.rs changes; 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) and read_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 run cargo fmt or cargo clippy locally.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

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.
@github-actions github-actions Bot added the bug Something isn't working label Nov 19, 2026
@ferdingler

Copy link
Copy Markdown
Collaborator

@strandly-the-agent review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve. This fixes #255, and I found no defects at 0a4968f. @ferdingler

  • Tests: cargo test --workspace --all-targets passes locally (2,680 passed, 0 failed, 0 ignored), and cargo fmt --check and cargo clippy are clean. ci.yml has 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 read over a Latin-1 file gave José and now gives Jos�. This matches $(…). No test covers it; the inline comment has one.
  • Merge order: #307 (read -d) edits the same lines and still has line.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]"; } and echo café | while read l; do echo "[$l]"; done print [café] on base and [café] on head.
  • The new tests prove the fix. read_utf8_multibyte and read_utf8_multibyte_fields fail with base's read.rs and pass on head.
  • Timeout path (read.rs:58-64). A timeout that lands inside a multi-byte character keeps the bytes exactly, and the next read decodes the completed character (caf\xC3, timeout, \xA9\n, then read gives café). 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. \r stripping is unchanged. Field splitting is better: IFS=é and IFS=│ now split, and IFS=© no longer splits inside é. The slice at &trimmed[pos..] is safe because pos comes from find on a valid str.
  • 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 char was at most U+00FF, so line.chars().map(|c| c as u8) put the original bytes back. The change at read.rs:61 is still needed, because line no 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 -e escapes), #337 (a group or subshell can't be piped into a loop or group), #338 (while read … < file is quadratic in the file size).

Comment thread tests/shell_integration.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve at 2406f69. @ferdingler

  • Previous finding: fixed. The new commit adds the two suggested tests and changes nothing else. read_invalid_utf8_is_replaced fails when read.rs:74 is made strict (String::from_utf8(bytes).expect(…)), and all four read UTF-8 tests fail with base's read.rs.
  • Tests: cargo test --workspace --all-targets passes 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 into main at 2664aa7 is clean and passes as well (2,709 passed).
  • Correction to my last review: I called cargo clippy clean. It exits 0 but prints 14 warnings, the same ones on 0a4968f and 2406f69. None are in read.rs or 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
ferdingler merged commit 3a35aa1 into strands-agents:main Nov 21, 2026
33 of 35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] read decodes input as Latin-1, so non-ASCII UTF-8 lines come back garbled

3 participants