Skip to content

fix: char-aware truncation to prevent UTF-8 boundary panics#573

Open
sungdark wants to merge 1 commit intoCortexLM:masterfrom
sungdark:fix/utf8-char-boundary-panic
Open

fix: char-aware truncation to prevent UTF-8 boundary panics#573
sungdark wants to merge 1 commit intoCortexLM:masterfrom
sungdark:fix/utf8-char-boundary-panic

Conversation

@sungdark
Copy link
Copy Markdown

Summary

Fixes 3 byte-boundary panics in agent_cmd handlers when agent descriptions or prompts contain multi-byte UTF-8 characters (emojis, international text) near truncation boundaries.

Changes

Root Cause

Byte-based slicing &s[..n] panics when the byte index n lands inside a multi-byte UTF-8 character.

Fix

Replaced byte-based slicing with chars().take(n).collect::<String>() for safe character-aware truncation.

File Before After
list.rs:95 &d[..35] + d.len() > 38 chars().take(35) + chars().count() > 38
show.rs:142 &prompt[..500] + prompt.len() > 500 chars().take(500) + chars().count() > 500
generate.rs:123 &generated.system_prompt[..500] chars().take(500) + chars().count() > 500

Testing

Verified with cargo check (builds successfully).

Related

Addresses Issue #3920 in PlatformNetwork/bounty-challenge.

Fixes 3 byte-boundary panics in agent_cmd handlers when agent
descriptions/prompts contain multi-byte UTF-8 characters (emojis,
international text) near truncation boundaries.

- list.rs: use chars().take(35) instead of byte slice [..35]
- show.rs: use chars().take(500) instead of byte slice [..500]
- generate.rs: use chars().take(500) instead of byte slice [..500]

These panics crashed the entire CLI when users had emojis or
non-ASCII characters in their agent descriptions/prompts.
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