fix(chat): prevent panic on UTF-8 boundary when rendering response#3716
Open
sandikodev wants to merge 1 commit intoaws:mainfrom
Open
fix(chat): prevent panic on UTF-8 boundary when rendering response#3716sandikodev wants to merge 1 commit intoaws:mainfrom
sandikodev wants to merge 1 commit intoaws:mainfrom
Conversation
When the AI response contains multi-byte characters (e.g. non-ASCII text adjacent to triple backticks), the byte offset accumulated via parsed.offset_from() can land in the middle of a UTF-8 character boundary. The subsequent &buf[offset..] slice then panics at runtime. The same pattern existed in two places: - crates/chat-cli/src/cli/chat/mod.rs (streaming response loop) - crates/chat-cli/src/cli/chat/parse.rs (validate! test macro loop) Replace the direct slice with .get(offset..) which returns None instead of panicking, and break the loop gracefully. Add a regression test with Indonesian, Chinese, and emoji inputs adjacent to triple backticks to verify no panic occurs. Fixes aws#3715
02487d2 to
b2230cf
Compare
Author
|
Update (amended commit): The fix was originally applied only to A regression test was also added in |
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.
Issue
Closes #3715
Problem
When the AI response contains multi-byte UTF-8 characters (e.g. non-ASCII text adjacent to triple backticks), the byte
offsetaccumulated viaparsed.offset_from()can land in the middle of a character boundary. The subsequent&buf[offset..]slice then panics at runtime:The same pattern existed in two places:
crates/chat-cli/src/cli/chat/mod.rs— streaming response rendering loopcrates/chat-cli/src/cli/chat/parse.rs—validate!test macro loop (line 675)Fix
Replace the direct slice with
.get(offset..)which returnsNoneinstead of panicking when the index is out of bounds or misaligned, and break the loop gracefully.Testing
The regression test covers three real-world inputs that triggered the panic:
Before this fix: the test panics with
byte index N is out of boundsAfter this fix: all inputs parse without panic
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.