Skip to content

rtk find panics on a non-ASCII directory path >50 bytes, and rtk git commit panics when the first output line does not start with [ #3415

Closed

Description

@HetCreep

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_boundary as used at src/analytics/session_cmd.rs:113 and src/analytics/gain.rs:730, or count characters instead of bytes as src/core/utils.rs:29 does.
  • git.rs:1013 → check bracket_end > 1 and use a character-aware slice; or skip the leading [ with strip_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

  1. added
    bug
    good first issue
    on Aug 4, 2026
  2. pszymkowiak commented on Aug 4, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 92%

    Summary

    Two reproduced panics from unchecked byte-slicing: rtk find crashes on non-ASCII directory paths >50 bytes (find_cmd.rs:326), and parse_commit_output crashes 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.

    This looks like a simple fix that could be auto-resolved. Repo maintainers can use /wshm fix to attempt it.

    📁 Relevant files
    • src/cmds/system/find_cmd.rs
    • src/cmds/git/git.rs

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

  3. mohammedelkarsh commented on Aug 4, 2026

    @mohammedelkarsh

    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.

  4. HetCreep commented on Aug 4, 2026

    @HetCreep
    Author

    @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 rustc probes of the two functions copied verbatim:

    find_cmd.rs:326 — &dir[dir.len() - 47..] behind a dir.len() > 50 byte-length check:

    input result
    60 ASCII bytes ok
    "ก".repeat(20) (60 bytes) panic
    "日".repeat(20) (60 bytes) panic

    git.rs:1013 — &line[1..bracket_end]:

    input result
    "[main abc1234def] msg" ok
    "] done" panic — range 1..0
    "日本] x" panic — byte 1 is not a char boundary

    Two things that might save you time:

    • The second one needs both guards, not just a char-boundary fix: bracket_end can be 0, which makes 1..0 an invalid range regardless of encoding.
    • The in-tree idioms are floor_char_boundary (analytics/session_cmd.rs:113, analytics/gain.rs:730) and chars().take(n) (core/utils.rs:29). For git.rs:1013 specifically there is already a chars().take(7) three lines below at git.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_output through 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 real git commit to 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.

  5. mohammedelkarsh commented on Aug 5, 2026

    @mohammedelkarsh

    Thanks @HetCreep — helpful write-up.

    PR is up at #3435. It already covers your exact repro cases as unit regressions:

    • find path truncation: ASCII long path + ก.repeat(20) + 日.repeat(20), via floor_char_boundary
    • parse_commit_output: ] done and 日本] x, via strip_prefix('[') so both the 1..0 range and the mid-char slice are closed

    Noted on #3425 (separate, no overlap). Happy to adjust if anything looks off against your probes.

  6. HetCreep commented on Aug 5, 2026

    @HetCreep
    Author

    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 ] done and mid-multibyte parse_commit_output slice cases. Looks solid, nothing off against my probes.

  7. PathGao commented on Aug 6, 2026

    @PathGao

    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.

    $ mkdir -p "2026-08-07-rtk三端接入会话/00-方法论(活文档)/图解与机制"
    $ touch  "2026-08-07-rtk三端接入会话/00-方法论(活文档)/图解与机制/a.md"
    $ rtk find . -name '*.md'
    thread 'main' (9629803) panicked at src/cmds/system/find_cmd.rs:326:34:
    start byte index 25 is not a char boundary; it is inside '入' (bytes 23..26 of string)
    note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
    $ echo $?
    134

    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 -g installs a PreToolUse hook that rewrites bare find:

    $ echo '{"tool_name":"Bash","tool_input":{"command":"find . -name \"*.md\""}}' | rtk hook claude
    {"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecisionReason":"RTK auto-rewrite","updatedInput":{"command":"rtk find . -name \"*.md\""}}}

    So under the recommended global setup, an agent running an ordinary find in a repository with non-Latin directory names gets a hard abort and no output, and nothing in the transcript explains why find "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.

  8. HetCreep commented on Aug 30, 2026

    @HetCreep
    Author

    Re-verified at develop @ 0de9610: both halves of this issue are fixed. My earlier note said only the git commit half 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 from str::find, which only returns char boundaries, and the inverted range is ruled out explicitly.
    • find_cmd truncation — 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:

    1. The find-side fix also changed the truncation threshold from 50 bytes to 50 characters. That is a sensible change, but it means the original repro in my report no longer reaches the branch at all — anyone re-testing this from the issue text should use a longer name.
    2. find_cmd.rs has 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:clibuggood first issuepriority:high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions