fix: UTF-8 panic on multi-byte string truncation (3 locations) - #482
Merged
Conversation
Fix three unsafe byte-slice operations that panic when the slice boundary falls inside a multi-byte UTF-8 codepoint: 1. build_commit_prompt() — &diff[..max_chars] in commit_cmd.rs 2. mask_secret() — &s[..4] and &s[s.len()-4..] in secrets_scanner.rs 3. parse_commit_message() — &subject[..69] in commit_cmd.rs All three now use is_char_boundary() to floor the index, matching the existing safe pattern in static_analysis.rs. Added 4 regression tests covering emoji and multi-byte chars at slice boundaries. All 786 tests pass.
ajianaz
force-pushed
the
fix/utf8-panic-commit-prompt
branch
from
August 4, 2026 12:11
3bfbe41 to
f3029c4
Compare
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.
What
Three unsafe byte-slice operations panic when the slice boundary falls inside a multi-byte UTF-8 codepoint (emoji, CJK, Arabic, etc.):
commit_cmd.rs:247—build_commit_prompt()&diff[..max_chars]secrets_scanner.rs:183—mask_secret()&s[..4],&s[s.len()-4..]commit_cmd.rs:349—parse_commit_message()&subject[..69]Why
These are unrecoverable panics on valid UTF-8 input.
cora commitcrashes, and the user cannot proceed. Especially likely in codebases with non-ASCII comments, strings, or variable names.The existing codebase already has the correct pattern in
static_analysis.rs:165-169—is_char_boundary()floor loop. This PR applies the same pattern to all three locations.Testing
commit_prompt_truncates_multibyte_utf8_without_panic— emoji at byte 7998–8002parse_truncates_long_subject_with_multibyte— emoji at byte 67–70 in commit subjectmask_secret_multibyte_no_panic— long secret with emoji prefixmask_secret_multibyte_short_no_panic— short secret with emojicargo clippy --features tree-sitter --bin cora --tests -- -D warningsclean