Skip to content

fix: use char-safe string truncation to prevent UTF-8 panic#76

Open
pluginmd wants to merge 1 commit intoRightNow-AI:mainfrom
pluginmd:fix/utf8-string-truncation-panic
Open

fix: use char-safe string truncation to prevent UTF-8 panic#76
pluginmd wants to merge 1 commit intoRightNow-AI:mainfrom
pluginmd:fix/utf8-string-truncation-panic

Conversation

@pluginmd
Copy link

@pluginmd pluginmd commented Feb 28, 2026

Summary

  • append_daily_memory_log used &trimmed[..500] which panics when byte 500 falls inside a multi-byte UTF-8 character
  • Session summary truncation used &t[..200] with the same issue
  • Replaced both with chars().take(N).collect() for safe character-boundary truncation

Root cause

Vietnamese/CJK text contains multi-byte UTF-8 characters (e.g. = 3 bytes). Rust panics on byte-level string slicing at non-char boundaries:

thread 'tokio-runtime-worker' panicked at crates/openfang-kernel/src/kernel.rs:418:17:
byte index 500 is not a char boundary; it is inside 'ặ' (bytes 498..501)

Impact

The panic kills a tokio worker thread. If the Telegram polling task was running on that thread, the polling loop dies silently — the bot stops responding to messages while the container remains healthy.

Test plan

  • cargo build --workspace --lib compiles
  • Send a >500 char Vietnamese/CJK message through Telegram and verify daily memory log is written without panic

From https://openfang.vn team with love!

🤖 Generated with Claude Code

`append_daily_memory_log` and session summary truncation used byte-level
slicing (`&s[..500]`, `&t[..200]`) which panics on multi-byte UTF-8
characters (e.g. Vietnamese, CJK). Replace with `chars().take(N)` for
safe truncation at character boundaries.

Panic observed in production with Vietnamese text:
  byte index 500 is not a char boundary; it is inside 'ặ' (bytes 498..501)

This crashed a tokio worker thread, silently killing the Telegram
polling loop and making the bot unresponsive.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

1 participant