fix: replace byte-index string slicing with char-safe truncation#2903
Open
amitksingh1490 wants to merge 5 commits intomainfrom
Open
fix: replace byte-index string slicing with char-safe truncation#2903amitksingh1490 wants to merge 5 commits intomainfrom
amitksingh1490 wants to merge 5 commits intomainfrom
Conversation
Replace unsafe `&str[..N]` byte-index slicing with character-aware truncation to prevent panics on multi-byte UTF-8 strings (e.g. →, 🔑). Fixes: - tools_display.rs: `&error[..77]` → `error.chars().take(77).collect()` - new_types.rs: `&key[..=12]` / `&key[len-4..]` → `chars().take()/skip()` - provider_repo.rs: `&token[..min(20,len)]` → `chars().take(20).collect()` Added 11 tests covering multi-byte characters and emoji edge cases. Co-Authored-By: ForgeCode <noreply@forgecode.dev>
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
Fix
byte index N is not a char boundarypanics by replacing unsafe byte-index string slicing with character-aware truncation.Context
The codebase had multiple locations using byte-index string slicing (
&str[..N]) which panics when strings contain multi-byte UTF-8 characters (e.g.,→= 3 bytes, 🔑 = 4 bytes). This caused runtime crashes when processing tool arguments, error messages, or keys containing non-ASCII characters.Changes
truncate_key()to usechars().take()andchars().skip()instead of byte indicesfrom_search_matchwith multi-byte charactersKey Implementation Details
key.chars().count()for length checks instead ofkey.len()(byte length)chars().take(N)for safe prefix truncationchars().skip(N)for safe suffix extractionTesting
All 5 new tests pass, including:
Links
refactor/dotforge-home-directory