Skip to content

fix(shell): keep valid UTF-8 prefix on fallback - #34

Open
asto18089 wants to merge 1 commit into
Pinvou:pinvou3-cleanfrom
asto18089:fix/shell-utf8-prefix-fallback
Open

fix(shell): keep valid UTF-8 prefix on fallback#34
asto18089 wants to merge 1 commit into
Pinvou:pinvou3-cleanfrom
asto18089:fix/shell-utf8-prefix-fallback

Conversation

@asto18089

Copy link
Copy Markdown

Background

During the r11 review of the Windows shell decoder, one MAJOR finding was verified fixed on candidate 654490026 (the prefix-preserving split reviewed in #24): when a chunk contains valid UTF-8 followed by genuinely invalid bytes, only the invalid remainder may go to the system code-page decoder. That fix was lost when the work was re-landed in #29 from upstream follow-up 0a85b13ba, which predates it. The published pinvou-v0.9.5-r11 head therefore still re-mojibakes the entire valid prefix of a chunk once one stray byte appears — worst on the synchronous path, where a fresh decoder sees the complete output of a foreground command (decode_shell_bytes with last = true), so a single stray byte corrupts all of it on Windows.

Changes

  • crates/tui/src/tools/shell/output.rs: the final Err(error) arm now splits on Utf8Error::valid_up_to(), emits the certified UTF-8 prefix unchanged, and passes only pending_utf8[valid_up_to..] to the legacy decoder (or from_utf8_lossy when no legacy encoding is configured). The other two match arms and the flattened control flow from 132f8567c are unchanged.
  • Reinstates the regression coverage lost with the fix: forkguard_shell_valid_utf8_prefix_survives_legacy_fallback ("中文" + 0x92 through WINDOWS_1252 must yield "中文’") and the deterministic CP1252 finalization assertion in running_full_decoder_keeps_incomplete_utf8_private_until_final.

Verification

  • cargo test -p codewhale-tui --lib output::tests: 15 passed (was 14; the new prefix test included). Without the fix, the new test fails by reproducing the mojibake.
  • cargo test -p codewhale-tui --lib shell::tests::: 96 passed.
  • cargo fmt --check -p codewhale-tui: clean.
  • cargo clippy -p codewhale-tui --all-features --locked with the CI lint set: no diagnostics in output.rs; remaining local diagnostics are pre-existing on a newer toolchain (28 before the change, 27 after) and do not reference the touched file.

Notes for the next parent sync (r12)

The legacy-fallback arm of ShellStreamDecoder handed the whole
pending_utf8 buffer, including its certified UTF-8 prefix, to the
system code-page decoder. One stray byte in an otherwise valid chunk
therefore re-mojibaked all preceding output on Windows, worst on the
synchronous decode path where a fresh decoder sees the complete
output.

Split on Utf8Error::valid_up_to first: emit the valid prefix as-is
and pass only the invalid remainder to the legacy decoder (or
from_utf8_lossy when no legacy encoding is configured). This restores
the prefix-preserving behavior reviewed in Hmbown#24 (candidate 6544900)
that was dropped when the fix was re-landed in Hmbown#29 from upstream
follow-up 0a85b13, and reinstates the regression tests lost with
it.

Note for the next parent sync: the restored test carries the
forkguard_ prefix, so the register's forkguard test inventory count
moves from 56 to 57 when this is synced.

Signed-off-by: asto <asto18089@126.com>
@github-actions

Copy link
Copy Markdown

Thanks @asto18089 for taking the time to contribute.

This repository is observing a maintainer-managed PR intake gate in dry-run mode, so this pull request is staying open. This note helps maintainers prepare the allowlist before any enforcement is considered.

Please read CONTRIBUTING.md for the expected contribution shape. A maintainer can grant recurring PR access by commenting /lgtm on a pull request.

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.

1 participant