Skip to content

Commit 02487d2

Browse files
committed
fix(chat): prevent panic on UTF-8 boundary when rendering response
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. Replace the direct slice with buf.get(offset..) which returns None instead of panicking when the index is out of bounds or misaligned, and break the loop gracefully in that case. Fixes #3715
1 parent e14ea18 commit 02487d2

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

  • crates/chat-cli/src/cli/chat

crates/chat-cli/src/cli/chat/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3099,7 +3099,11 @@ impl ChatSession {
30993099

31003100
// Print the response for normal cases
31013101
loop {
3102-
let input = Partial::new(&buf[offset..]);
3102+
// Use `get` to avoid panicking if `offset` lands on a non-UTF-8 boundary,
3103+
// which can happen when the response contains multi-byte characters (e.g.
3104+
// non-ASCII text adjacent to triple backticks). See: #3715
3105+
let Some(slice) = buf.get(offset..) else { break };
3106+
let input = Partial::new(slice);
31033107
if self.stdout.should_send_structured_event {
31043108
match interpret_markdown(input, &mut temp_buf, &mut state) {
31053109
Ok(parsed) => {

0 commit comments

Comments
 (0)