Skip to content

fix(git): fix panic on multibyte chars in commit output - #1266

Merged
aeppling merged 2 commits into
rtk-ai:developfrom
shalk:fix/commit-multibyte-panic-rebased
Jun 3, 2026
Merged

aeppling merged 2 commits into
rtk-ai:developfrom
shalk:fix/commit-multibyte-panic-rebased

Conversation

@shalk

@shalk shalk commented Apr 13, 2026 •

Copy link
Copy Markdown
Contributor

git commit on an initial commit under a Chinese locale outputs:
[master(根提交) fb597ec] Initial commit

The old code found the first space and sliced line[1..hash_start], yielding "master(根提交)" (21 bytes). hash.len() >= 7 was true, so it tried &hash[..7] — but byte 7 falls inside the 3-byte '(' character, causing a panic.

Two bugs were present:

  1. Logic: the code grabbed the token before the first space (branch name), not the commit hash. The hash is always the last whitespace-separated token inside the [...] brackets.
  2. Safety: raw byte slicing on a potentially non-ASCII string.

Fix: find ']', extract the bracket content, split_whitespace() and take next_back() to get the hash. Use chars().take(7) for the short-hash slice so it is always char-boundary-safe.

Summary

Test plan

  • cargo fmt --all && cargo clippy --all-targets && cargo test
  • Manual testing: rtk <command> output inspected

Important: All PRs must target the develop branch (not master).
See CONTRIBUTING.md for details.

git commit on an initial commit under a Chinese locale outputs:
  [master(根提交) fb597ec] Initial commit

The old code found the first space and sliced line[1..hash_start],
yielding "master(根提交)" (21 bytes).  hash.len() >= 7 was true,
so it tried &hash[..7] — but byte 7 falls inside the 3-byte '('
character, causing a panic.

Two bugs were present:
1. Logic: the code grabbed the token *before* the first space
   (branch name), not the commit hash.  The hash is always the last
   whitespace-separated token inside the [...] brackets.
2. Safety: raw byte slicing on a potentially non-ASCII string.

Fix: find ']', extract the bracket content, split_whitespace() and
take next_back() to get the hash.  Use chars().take(7) for the
short-hash slice so it is always char-boundary-safe.
@pszymkowiak pszymkowiak added bug Something isn't working effort-small Quelques heures, 1 fichier filter-quality Filter produces incorrect/truncated signal labels Apr 13, 2026
@pszymkowiak

Copy link
Copy Markdown
Collaborator

[w] wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟢 Risk low

Summary

Fixes a panic caused by slicing into multibyte UTF-8 characters when parsing git commit output under non-ASCII locales (e.g., Chinese). The fix correctly extracts the commit hash as the last token within brackets and uses char-safe slicing instead of raw byte indexing.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Analyzed automatically by wshm · This is an automated analysis, not a human review.

@CLAassistant

CLAassistant commented Apr 13, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

…yte branch names

Extracts parse_commit_output into a testable helper function and adds 7 comprehensive unit tests covering:
- Normal branch names
- Root-commit annotations
- Multibyte branch names (Chinese, Thai) — regression test for the panic fix
- Edge cases (no bracket, short hash, empty input)

Tests verify the fix handles UTF-8 safely by anchoring on ']' and using char-safe slicing.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
@aeppling

aeppling commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Hey @shalk , thanks for the fix and sorry for the delay

This look good to me

@aeppling aeppling mentioned this pull request Jun 3, 2026
@aeppling
aeppling merged commit 4f4a6a0 into rtk-ai:develop Jun 3, 2026
1 check passed
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
rtk 0.42.3

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## [0.42.3](rtk-ai/rtk@v0.42.2...v0.42.3) (2026-06-05)

### Feats

- feat(copilot): CLI native integration #2101

### Fix

- fix(docs): replace remaining MIT license references with Apache 2.0 [#2084](rtk-ai/rtk#2084)
- fix: prevent crashes when output is piped rtk-ai/rtk#1048 close rtk-ai/rtk#1004
- fix(grep): parse single-file output containing colons rtk-ai/rtk#1554; close rtk-ai/rtk#1436.
- fix(ls): preserve permission info as octal when -l/-la is passed rtk-ai/rtk#1675
- fix(cicd): MIT to Apache 2.0 [#2092](rtk-ai/rtk#2092)
- fix(pkg): rtk is Apache 2.0 and no MIT [#2082](rtk-ai/rtk#2082)
- fix(hook): collapse bash line continuations before matching #1572 close Hook matcher: leading backslash-newline
  defeats command rewrite #1564
- fix(gh,glab): don't pre-reject view/checks/run subcommands missing the id rtk-ai/rtk#2123
- fix(git): fix panic on multibyte chars in commit output rtk-ai/rtk#1266

### Other

- docs(readme): add Portuguese translation #2128
- doc(init): fix documentation inconsistencies arount rtk init rtk-ai/rtk#2173
- fix(provider): sanatize more chars when encoding claude code project pathes https://github.com/rtk-ai/rtk/pull/2172</pre>
  <p>View the full release notes at <a href="https://github.com/rtk-ai/rtk/releases/tag/v0.42.3">https://github.com/rtk-ai/rtk/releases/tag/v0.42.3</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!10665
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working effort-small Quelques heures, 1 fichier filter-quality Filter produces incorrect/truncated signal

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants