Repository navigation
Description
I swept the repository for the byte-slice class discussed in #2954 (&str sliced at a computed index behind a byte-length guard). Two live sites remain, both reproduced.
Provenance: AI-assisted source audit, human-directed, at v0.44.2 (700bdde). Both reproduced by compiling the two functions verbatim with rustc 1.97.1 and running them.
1. src/cmds/system/find_cmd.rs:326 — non-ASCII directory paths
let dir_display = if dir.len() > 50 {
format!("...{}", &dir[dir.len() - 47..])
} else {
dir.clone()
};dir.len() is bytes; dir.len() - 47 is a byte index. For any path longer than 50 bytes whose byte at that offset is inside a multibyte character, the slice panics.
ascii 60-byte dir : ok
"ก" x20 (60 bytes) : PANIC
"日" x20 (60 bytes) : PANIC
Twenty CJK or Thai characters is a perfectly ordinary directory name, and 50 bytes is short — any non-ASCII path a little way down a tree reaches it. This is a display-formatting line, so the whole rtk find invocation dies for a cosmetic truncation.
2. src/cmds/git/git.rs:1013 — first line of git commit output
/// … localized variants, and multibyte branch names.
fn parse_commit_output(line: &str) -> String {
if let Some(bracket_end) = line.find(']') {
let bracket_content = &line[1..bracket_end];Two unchecked assumptions: that byte 1 is a character boundary, and that bracket_end >= 1.
"[main abc1234def] msg" : ok
"] done" : PANIC (range 1..0)
"日本] x" : PANIC (byte 1 not a char boundary)
The doc comment directly above says the function handles "multibyte branch names", but &line[1..] is exactly what cannot. The four unit tests all feed lines beginning with [.
Reachability: this runs on the first stdout line of a successful git commit (via classify_commit_outcome, git.rs:1078). Anything a pre-commit hook prints to stdout lands there first. I did not manage to trigger it through the CLI on this machine — git routed my test hook's output to stderr — so treat the reachability as plausible rather than demonstrated; the panic itself is demonstrated.
Note the correct idiom is already two lines below, at git.rs:1016: hash.chars().take(7).
Suggested fixes (untested — no programmer has reviewed these)
Both have an in-tree model:
find_cmd.rs:326→floor_char_boundaryas used atsrc/analytics/session_cmd.rs:113andsrc/analytics/gain.rs:730, or count characters instead of bytes assrc/core/utils.rs:29does.git.rs:1013→ checkbracket_end > 1and use a character-aware slice; or skip the leading[withstrip_prefix('[')rather than a byte index.
On the class as a whole
I checked the other ~30 &str slice sites in src/ and they are safe by construction — floor_char_boundary (gain.rs:730, session_cmd.rs:113, json_cmd.rs:112, pipe_cmd.rs:171), explicit boundary walks (tee.rs:135-140, curl_cmd.rs:141-143), regex Match::start()/end() (ls.rs:157-159), ASCII-only offsets (wc_cmd.rs:252, pnpm_cmd.rs:161), or input pre-mapped to ASCII (search.rs:116-120). So this is not a widespread pattern; it is two remaining sites.
Two further sites are byte-length-guarded but reachable only through data rtk wrote itself (analytics/gain.rs:464, core/display_helpers.rs:205,210 — all fed by %Y-%m-%d SQLite columns). They panic on non-ASCII input in isolation but I could not construct a real path to them, so I am listing rather than filing them.
As mentioned in #3394, clippy::indexing_slicing / clippy::string_slice are not configured anywhere, which is why new instances are not caught at review time.
Activity
pszymkowiak commentedon Aug 4, 2026
🔍 Automated Triage
bughighSummary
Two reproduced panics from unchecked byte-slicing:
rtk findcrashes on non-ASCII directory paths >50 bytes (find_cmd.rs:326), andparse_commit_outputcrashes when the first git-commit output line lacks a leading[or has non-ASCII content (git.rs:1013); fix with char-boundary-safe slicing per in-tree examples.📁 Relevant files
src/cmds/system/find_cmd.rssrc/cmds/git/git.rsTriaged automatically by wshm · This is an automated analysis, not a human review.
mohammedelkarsh commentedon Aug 4, 2026
I'd like to work on this. Fixing both remaining byte-slice panic sites (find_cmd path truncation + parse_commit_output) with char-boundary-safe slicing, plus regression tests. Opening a PR from a fresh branch off develop.
HetCreep commentedon Aug 4, 2026
@mohammedelkarsh — all yours, please go ahead. I only report; I do not send patches, so there is nothing of mine for you to work around.
Here are the exact cases I used, so you can drop them straight into regression tests. Both were run as standalone
rustcprobes of the two functions copied verbatim:find_cmd.rs:326—&dir[dir.len() - 47..]behind adir.len() > 50byte-length check:"ก".repeat(20)(60 bytes)"日".repeat(20)(60 bytes)git.rs:1013—&line[1..bracket_end]:"[main abc1234def] msg""] done"1..0"日本] x"Two things that might save you time:
bracket_endcan be0, which makes1..0an invalid range regardless of encoding.floor_char_boundary(analytics/session_cmd.rs:113,analytics/gain.rs:730) andchars().take(n)(core/utils.rs:29). Forgit.rs:1013specifically there is already achars().take(7)three lines below atgit.rs:1016, so that file's own style is right there.One caveat on reachability, since it affects how you might frame the test for the second: I could not trigger
parse_commit_outputthrough the CLI on my machine — git routed my test hook's output to stderr rather than stdout. The panic itself is demonstrated; the path from a realgit committo a first stdout line that does not start with[is plausible but I did not prove it. A unit test on the function is solid either way.There is a related item I filed separately in #3425 (a
/*inside a string literal opening a phantom block comment) — different function, no overlap with your branch, just flagging so nothing surprises you.Happy to answer anything about the repros. I have no stake in how it gets fixed.
mohammedelkarsh commentedon Aug 5, 2026
Thanks @HetCreep — helpful write-up.
PR is up at #3435. It already covers your exact repro cases as unit regressions:
ก.repeat(20) +日.repeat(20), viafloor_char_boundary] doneand日本] x, viastrip_prefix('[')so both the1..0range and the mid-char slice are closedNoted on #3425 (separate, no overlap). Happy to adjust if anything looks off against your probes.
HetCreep commentedon Aug 5, 2026
Thanks @mohammedelkarsh — confirmed, the regressions in #3435 match both repro paths from the original report: the ASCII+
ก×20+日×20 boundary case for find path truncation, and both the] doneand mid-multibyteparse_commit_outputslice cases. Looks solid, nothing off against my probes.PathGao commentedon Aug 6, 2026
Confirming site 1 on the released binary. The original report notes both panics were demonstrated by compiling the two functions verbatim under
rustc, so this closes the end-to-end gap on the shipped artifact.Environment: rtk 0.44.2, Homebrew bottle (
rtk--0.44.2.arm64_tahoe.bottle.tar.gz), macOS 15 / arm64.Process aborts, no results printed. Worth noting the path here is 74 bytes and is not repetition padding — it is a dated session directory plus two ordinary nested Chinese names, taken from a real tree. 50 bytes is about 16 CJK characters, so any nested non-Latin path clears the threshold without looking long to the person who named it.
Severity note that may not be obvious from the repro: this is reachable without the user ever typing
rtk.rtk init -ginstalls aPreToolUsehook that rewrites barefind:So under the recommended global setup, an agent running an ordinary
findin a repository with non-Latin directory names gets a hard abort and no output, and nothing in the transcript explains whyfind"failed" — the agent did not choose to invoke rtk. That is the shape most users will meet this bug in, and it argues for the fix landing rather than for anything different in #3435.Nothing needed from my side on the patch — the regressions there cover this site. Adding only because the hook path changes how often it fires.
HetCreep commentedon Aug 30, 2026
Re-verified at
develop@0de9610: both halves of this issue are fixed. My earlier note said only thegit commithalf had landed; that was accurate when written and is no longer true.Provenance: AI-assisted source audit, human-directed, at
develop@0de9610. Both halves were executed, not just read.parse_commit_output—cargo test --bin rtk parse_commit_output= 11 passed / 0 failed, and those tests feed the exact inputs from the report ("] stray [main abc1234def]"for the inverted-range case, and the multibyte-prefix cases). Both indices now come fromstr::find, which only returns char boundaries, and the inverted range is ruled out explicitly.find_cmdtruncation — built the binary and forced the truncation branch with a 187-byte non-ASCII directory name; it renders and exits 0. Under the old code that slice landed mid-character.Two notes offered as observations, not complaints:
find_cmd.rshas no unit test covering the char-based truncation (the git side has 11, including the report's verbatim inputs). The fix is structurally sound rather than test-pinned, so a future regression there would land silently. Entirely your call whether that is worth a test.Thanks for fixing both. Happy for this to be closed.