Skip to content

fix(chat): prevent panic on UTF-8 boundary when rendering response#3716

Open
sandikodev wants to merge 1 commit intoaws:mainfrom
sandikodev:fix/response-render-utf8-panic
Open

fix(chat): prevent panic on UTF-8 boundary when rendering response#3716
sandikodev wants to merge 1 commit intoaws:mainfrom
sandikodev:fix/response-render-utf8-panic

Conversation

@sandikodev
Copy link
Copy Markdown

@sandikodev sandikodev commented Apr 4, 2026

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 offset accumulated via parsed.offset_from() can land in the middle of a character boundary. The subsequent &buf[offset..] slice then panics at runtime:

The application panicked (crashed).
Message:  byte index 536 is out of bounds of `...`[...]
Location: crates/chat-cli/src/cli/chat/mod.rs:3849

The same pattern existed in two places:

  • crates/chat-cli/src/cli/chat/mod.rs — streaming response rendering loop
  • crates/chat-cli/src/cli/chat/parse.rsvalidate! test macro loop (line 675)

Fix

Replace the direct slice with .get(offset..) which returns None instead of panicking when the index is out of bounds or misaligned, and break the loop gracefully.

// Before (both locations)
let input = Partial::new(&buf[offset..]);

// After
let Some(slice) = buf.get(offset..) else { break };
let input = Partial::new(slice);

Testing

# Run the regression test
cargo test -p chat_cli multibyte_utf8

# Run the full test suite to check for regressions
cargo test -p chat_cli

The regression test covers three real-world inputs that triggered the panic:

  • Indonesian text adjacent to triple backticks
  • Chinese characters adjacent to triple backticks
  • Emoji adjacent to triple backticks

Before this fix: the test panics with byte index N is out of bounds
After 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.

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
@sandikodev sandikodev force-pushed the fix/response-render-utf8-panic branch from 02487d2 to b2230cf Compare April 4, 2026 16:08
@sandikodev
Copy link
Copy Markdown
Author

Update (amended commit): The fix was originally applied only to mod.rs. On closer inspection, the identical &buf[offset..] pattern existed in parse.rs as well (inside the validate! test macro loop at line 675). Both locations are now fixed.

A regression test was also added in parse.rs with real-world inputs that triggered the panic in the wild — Indonesian text, Chinese characters, and emoji adjacent to triple backticks — to confirm the fix prevents the panic and catch any future regression.

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.

panic: byte index out of bounds in chat response rendering when response contains triple backticks

1 participant