Repository navigation
fix: keep captured output that contains invalid UTF-8 - #231
Conversation
33c681d to
d3b706c
Compare
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve. The fix works: $(...) and run() output with invalid UTF-8 is kept, with U+FFFD where the bad bytes were, and pipes and files still carry the raw bytes. @ferdingler, one 🟡 below: the max_output test passes on main too, so the new behaviour at the limit has no test.
- 🟡
invalid_utf8_capture_still_enforces_max_outputdoesn't tell main and this PR apart (suggestion inline). - Question (non-blocking):
src/builtins/lua.rs:94is the lastread_to_string_limited(..).unwrap_or_default().cat /g/bin | lua -e 'io.write(io.read("a"))'silently loses all of its stdin. Do you want to swap it here as well, or leave it for a follow-up? - I found an unrelated bug while probing and filed it as #255:
readdecodes input as Latin-1, socafécomes back ascafé.
✅ What I verified
- Head
d3b706c, base5a0fa2b.cargo test --workspace --all-targetspassed 2204 tests with 0 failures, andcargo fmt --checkis clean. Running clippy locally with Rust 1.100 fails, but only on lints in files this PR doesn't touch (find.rs,lua.rs,ls.rs). The stable Rust/Python jobs in CI are green. - Main vs PR: I ran the new tests against main's
src/.subst_*andrun_output_*fail there, andinvalid_utf8_capture_still_enforces_max_outputpasses. - A file containing
ok\xffbad\nwithcat /g/bingives: max=6 →"", rc 0. max=7 →"ok\u{FFFD}ba"plus a limit error, rc 1. max=8 →"ok\u{FFFD}bad", rc 1. max=9 → the full output, rc 0. On main, every one of these returns""with rc 0. - 8191 ×
afollowed byé, run through$(cat | cat), comes back intact with no false U+FFFD at the 8 KiB read boundary. The bytes are only decoded after the whole stream has been read (src/os.rs:819). - Probe log:
s3://…/strands-agents/shell/pr/231/pr231-limit-probe.txt. Test log:pr231-test.log.
Appendix: non-blocking (2)
- PR body: the limit claim is right, but the reason is different.
read_to_end_limited(src/os.rs:854) counts raw bytes, so U+FFFD never trips it. The error comes from the check on the converted string inexecute_pipeline(src/exec.rs:3037). That check covers stdout only:cat /g/bin >&2at max=7 returns all 9 bytes of stderr with rc 0. One more oddity: an overflow that is over the limit in raw bytes is still dropped silently (max=6 gives""), while a smaller one is reported (max=7). The silent drop already happens on main, through the sameunwrap_or_default, so it's a follow-up and not part of this PR. - The adversarial pass I spawned timed out. I ran its chunk-boundary and limit-edge probes myself (results above).
|
I reproduced the Lua case: invalid UTF-8 makes its stdin pre-read empty, while the ASCII control works. That input conversion is unchanged from main and happens before shell output capture, so I’m leaving it for a separate Lua fix; replacing it here would also change the bytes that Lua code receives. I corrected the PR description to distinguish the raw-byte cap from the later stdout check. Replacement expansion can trigger that stdout check, while stderr has no equivalent check after conversion; raw-byte overflow still drops silently. Those existing limit behaviors stay outside this PR. Thanks for filing the separate The requested limit regression is now in d727544, with a failing run against the old capture reads and a passing run with the fix. The full local Rust checks and seven CLI probes passed; CI is running again for the new commit. |
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Still approve. The 🟡 is fixed. With main's capture reads, the new max=7 case in invalid_utf8_capture_still_enforces_max_output fails (left: "", right: "ok\u{FFFD}ba"), and with this PR it passes. @ferdingler, nothing else needed from me.
- Leaving Lua stdin for a follow-up makes sense to me, since changing it would also change the bytes Lua code receives.
- The corrected PR description now matches the code.
✅ Verified at d727544
- The only change since my last review is +7 lines in
tests/shell_integration.rs. cargo test --workspace --all-targets: 2204 passed, 0 failed.cargo fmt --checkis clean.- I swapped main's
src/exec.rsandsrc/os.rsinto the PR checkout, ran the new assertion and saw it fail, then restored the PR's versions.
Description
When the shell captures output for command substitution or returns it to the caller, one invalid UTF-8 byte made that command’s whole output disappear while leaving its status successful. For example, a file containing
ok\377bad\nproduced an empty substitution and lost the validokandbadtext too.Captured text now keeps its valid parts and replaces invalid bytes with U+FFFD; pipes and files continue carrying raw bytes. The capture reader still limits raw bytes, while the later stdout check can truncate and report an error when replacement characters expand the text past that limit; stderr has no corresponding check after conversion. Silent drops for raw-byte overflow and Lua’s existing stdin-decoding behavior remain follow-ups. This pairs with #215, which lets printf emit raw bytes.
I checked substitution, pipelines, captured stdout and stderr, raw pipes and files, and the output-limit boundaries; the added replacement-expansion test fails with the old capture reads and passes with this change. The full Rust suite, doc tests, warning-denying docs build, formatting, clippy, and seven CLI probes passed locally, with existing clippy warnings; Python and Node are covered by CI and their local suites were not run.
Related Issues
Part of #207
Documentation PR
N/A
Type of Change
Bug fix
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.
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.