fix: prevent panics on multi-byte UTF-8 string slicing#57
Closed
wunitb wants to merge 2 commits intoRightNow-AI:mainfrom
Closed
fix: prevent panics on multi-byte UTF-8 string slicing#57wunitb wants to merge 2 commits intoRightNow-AI:mainfrom
wunitb wants to merge 2 commits intoRightNow-AI:mainfrom
Conversation
…strings Direct byte-offset slicing like `&s[..500]` panics when the offset falls inside a multi-byte character (e.g. Thai, Chinese, emoji — each 3-4 bytes). Replace all 12 instances across 8 files with `floor_char_boundary()` (stable since Rust 1.80) which finds the nearest valid char boundary <= the given byte index. Affected crates: - openfang-kernel (kernel.rs) - openfang-runtime (prompt_builder, compactor, loop_guard, llm_errors, workspace_context, tts, image_gen) - openfang-memory (session.rs) - openfang-api (channel_bridge.rs) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1. split_message() in types.rs panics when max_len (4096 for Telegram) falls inside a multi-byte character. Use floor_char_boundary() to find a safe split point before searching for newlines. 2. /agent command in bridge.rs only takes args[0], so "/agent Data Analyst" becomes "/agent Data" — join all args to support names with spaces. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
&s[..N]) withfloor_char_boundary(N)across 8 filesfloor_char_boundary()is stable since Rust 1.80 — finds the nearest valid char boundary ≤ the given byte indexProblem
Slicing a
&strat an arbitrary byte offset (e.g.&s[..500]) panics withbyte index N is not a char boundarywhen the offset lands inside a multi-byte UTF-8 character. This affects all non-ASCII languages:Files Changed
openfang-kernelkernel.rsopenfang-runtimeprompt_builder.rsopenfang-runtimecompactor.rsopenfang-runtimeloop_guard.rsopenfang-runtimellm_errors.rsopenfang-runtimeworkspace_context.rsopenfang-runtimetts.rsopenfang-runtimeimage_gen.rsopenfang-memorysession.rsopenfang-apichannel_bridge.rsTest plan
cargo build --workspace --libcompiles successfullycargo test --workspaceall tests pass🤖 Generated with Claude Code