UTF-8 Character Boundary Panic on Chinese Text Processing
Summary
Claude Code CLI crashes with a Rust panic when processing Chinese text, due to incorrect UTF-8 character boundary handling during string slicing.
Environment
- OS: macOS (Darwin 24.6.0)
- Claude Code version: 2.1.3
- Command:
claude --dangerously-skip-permissions
Error Message
thread '<unnamed>' (8441975) panicked at /rustc/ed61e7d7e242494fb7057f2657300d9e77bb4fcb/library/core/src/str/mod.rs:833:21:
byte index 86 is not a char boundary; it is inside '。' (bytes 84..87) of `关键人物——他的研究让计算机能像人脑一样层层提取图像特征。深度学习是AI的核心技术:让机器通过多层数据处理,自动学会识别模式,无需人类手把手教每一条规则。`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fatal runtime error: failed to initiate panic, error 5, aborting
[1] 55108 abort claude --dangerously-skip-permissions
Steps to Reproduce
- Run Claude Code CLI
- Process text containing Chinese characters (specifically text with Chinese punctuation like "。")
- The crash occurs during text processing/rendering
Expected Behavior
Claude Code should handle UTF-8 multi-byte characters (including Chinese text and punctuation) without crashing.
Actual Behavior
The CLI panics when attempting to slice a string at a byte index that falls inside a multi-byte UTF-8 character (Chinese full-width period "。" occupies bytes 84-87, but code attempts to slice at byte 86).
Root Cause Analysis
The error suggests the code is using byte-based indexing instead of character-based indexing when slicing strings. In Rust, string slicing with &s[start..end] requires both indices to be valid UTF-8 character boundaries. Chinese characters typically occupy 3 bytes in UTF-8, and Chinese punctuation like "。" is no exception.
Suggested Fix
Use character-aware string operations:
s.chars().take(n)instead of&s[..n]- Or use
s.char_indices()to find valid boundaries - Or use a crate like
unicode-segmentationfor proper grapheme handling
This issue has 7 comments on GitHub. Read the full discussion on GitHub ↗