claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

Panic when processing Chinese text: byte index not a char boundary in UTF-8 string

Open kiki830621 opened this issue 11 months ago • 1 comments

Description

Claude Code crashes with a Rust panic when processing text containing Chinese characters. The error occurs during string slicing where the byte index falls inside a multi-byte UTF-8 character.

Error Message

thread '<unnamed>' (19968778) panicked at /rustc/ed61e7d7e242494fb7057f2657300d9e77bb4fcb/library/core/src/str/mod.rs:833:21:
byte index 95 is not a char boundary; it is inside '。' (bytes 93..96) of `易產生內在歸因(「我需要加強」)而非外在歸因(「老師針對我」)。`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fatal runtime error: failed to initiate panic, error 5, aborting
[1]    91024 abort      claude --dangerously-skip-permissions

Environment

  • OS: macOS (Darwin 25.2.0)
  • Claude Code version: Latest (as of 2024-12-30)
  • Command: claude --dangerously-skip-permissions

Steps to Reproduce

  1. Open Claude Code in a project containing Chinese text files
  2. The CLI attempts to process/display Chinese content
  3. Crash occurs when the internal string slicing hits a multi-byte character boundary

Root Cause Analysis

The error indicates that the code is using byte indexing instead of character indexing when slicing a UTF-8 string containing CJK characters:

  • Chinese punctuation 。 (U+3002) is encoded as 3 bytes in UTF-8
  • The code attempted to slice at byte index 95
  • This index falls inside the character 。 (bytes 93..96)
  • Rust's string safety correctly panics to prevent invalid UTF-8

Suggested Fix

The string slicing logic should use character-aware methods:

// Instead of:
&s[0..byte_index]

// Use:
s.char_indices()
    .take_while(|(i, _)| *i < target_char_count)
    .map(|(_, c)| c)
    .collect::<String>()

// Or use the `unicode-segmentation` crate for proper grapheme handling

Impact

This bug affects all users working with:

  • Chinese (Traditional/Simplified)
  • Japanese
  • Korean
  • Any other multi-byte UTF-8 text

Additional Context

The affected text was from an educational psychology document discussing attribution theory in a Taiwanese high school context. The project contains extensive Chinese documentation.

kiki830621 avatar Dec 30 '25 04:12 kiki830621

same here(korean)

thread '<unnamed>' (1321268) panicked at /rustc/ed61e7d7e242494fb7057f2657300d9e77bb4fcb/library/core/src/str/mod.rs:833:21:
byte index 5 is not a char boundary; it is inside '기' (bytes 3..6) of `찾기`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fatal runtime error: failed to initiate panic, error 5, aborting
[1]    13295 abort      claude

94wogus-quantit avatar Dec 30 '25 05:12 94wogus-quantit

FYI: This issue appears to be fixed in v2.1.6!

Changelog: https://github.com/anthropics/claude-code/blob/main/CHANGELOG.md#216 Reference: https://github.com/anthropics/claude-code/issues/14133#issuecomment-2739536291

Hope this helps!

takatea avatar Jan 13 '26 04:01 takatea

Closing for now — inactive for too long. Please open a new issue if this is still relevant.

github-actions[bot] avatar Feb 26 '26 22:02 github-actions[bot]

This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.

github-actions[bot] avatar Mar 07 '26 14:03 github-actions[bot]