Skip to content

fix: UTF-8 panic on multi-byte string truncation (3 locations) - #482

Merged
ajianaz merged 1 commit into
developfrom
fix/utf8-panic-commit-prompt
Aug 4, 2026
Merged

fix: UTF-8 panic on multi-byte string truncation (3 locations)#482
ajianaz merged 1 commit into
developfrom
fix/utf8-panic-commit-prompt

Conversation

@ajianaz

@ajianaz ajianaz commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

What

Three unsafe byte-slice operations panic when the slice boundary falls inside a multi-byte UTF-8 codepoint (emoji, CJK, Arabic, etc.):

Location Code Trigger
commit_cmd.rs:247build_commit_prompt() &diff[..max_chars] Large diff with non-ASCII chars at byte 8000
secrets_scanner.rs:183mask_secret() &s[..4], &s[s.len()-4..] Secret containing multi-byte UTF-8
commit_cmd.rs:349parse_commit_message() &subject[..69] LLM generates commit subject with emoji

Why

These are unrecoverable panics on valid UTF-8 input. cora commit crashes, 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-169is_char_boundary() floor loop. This PR applies the same pattern to all three locations.

Testing

  • 4 new regression tests:
    • commit_prompt_truncates_multibyte_utf8_without_panic — emoji at byte 7998–8002
    • parse_truncates_long_subject_with_multibyte — emoji at byte 67–70 in commit subject
    • mask_secret_multibyte_no_panic — long secret with emoji prefix
    • mask_secret_multibyte_short_no_panic — short secret with emoji
  • All 786 tests pass
  • cargo clippy --features tree-sitter --bin cora --tests -- -D warnings clean

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
ajianaz force-pushed the fix/utf8-panic-commit-prompt branch from 3bfbe41 to f3029c4 Compare August 4, 2026 12:11
@ajianaz ajianaz changed the title fix(commit): UTF-8 panic on multi-byte truncation in commit prompt fix: UTF-8 panic on multi-byte string truncation (3 locations) Aug 4, 2026
@ajianaz
ajianaz merged commit a2f9a0d into develop Aug 4, 2026
16 checks passed
@ajianaz
ajianaz deleted the fix/utf8-panic-commit-prompt branch August 4, 2026 15:09
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