Skip to content

fix(find): UTF-8-safe directory path abbreviation (char-boundary panic) - #10

Merged
ryanmurf merged 2 commits into
masterfrom
fix/find-cmd-utf8-char-boundary-panic
May 30, 2026
Merged

ryanmurf merged 2 commits into
masterfrom
fix/find-cmd-utf8-char-boundary-panic

Conversation

@ryanmurf

Copy link
Copy Markdown

Problem

src/cmds/system/find_cmd.rs abbreviated long directory paths for display with a raw byte slice &dir[dir.len() - 47..]. When the byte offset dir.len() - 47 lands inside a multibyte UTF-8 character (accented, CJK, emoji, …), Rust panics:

byte index 4 is not a char boundary; it is inside '🎉' (bytes 2..6 of string)

This crashes rtk find on any legitimate path containing non-ASCII characters near the 50-char abbreviation boundary — flagged by the #9 filter-integrity sweep.

Fix

New abbreviate_dir(&str) -> String helper that keeps the last 47 characters (chars().rev().take(47)) instead of 47 bytes:

  • ASCII unchanged: 1 char == 1 byte, so output is byte-for-byte identical to the old slice.
  • Multibyte safe: abbreviates by visible character, never slices mid-codepoint.

The threshold also moves from dir.len() > 50 (bytes) to chars().count() > 50 (characters), which is the semantically-correct width measure and is identical for ASCII.

Regression tests (fail pre-fix, pass post-fix)

  • abbreviate_dir_emoji_at_boundary_no_panic — a path where the cut point straddles a 4-byte emoji (panicked pre-fix).
  • abbreviate_dir_multibyte_variants_no_panic — 2-byte (accented), 3-byte (CJK), 4-byte (emoji) paths.
  • abbreviate_dir_ascii_unchanged — asserts byte-for-byte parity with the original &dir[dir.len()-47..] for ASCII.
  • abbreviate_dir_short_multibyte_verbatim — short multibyte paths returned as-is.
  • run_multibyte_path_no_panic — end-to-end run() over a real multibyte directory tree.

Proof: the first two tests FAIL with the char-boundary panic against the pre-fix code; all pass after.

Scope / second-axis check

This is the only fixed-offset byte-index slice in find_cmd.rs. No output-mangling (entry drop/reorder): results are deterministically sorted and grouped; dir iteration order is stable via a sorted key list. glob_match operates on byte slices only for matching, never for display slicing.

Gates

cargo fmt --check ✓ · clippy --all-targets -D unsafe_code exit 0 ✓ · cargo test --all (1746+12 pass, 0 fail) ✓ · cargo build --release ✓ · binary smoke test on emoji path exit 0 ✓

The find filter abbreviated long directory paths with a raw byte slice
`&dir[dir.len() - 47..]`. When the byte offset `dir.len() - 47` lands
inside a multibyte UTF-8 character (accented, CJK, emoji, etc.), Rust
panics with "byte index N is not a char boundary", crashing rtk find on
any legitimate path containing non-ASCII near the abbreviation boundary.

Replace the byte slice with a char-boundary-safe `abbreviate_dir` helper
that keeps the last 47 *characters* (via chars().rev().take(47)). For
pure-ASCII paths a char is one byte, so output is byte-for-byte identical
to the old behavior; multibyte paths now abbreviate by visible character
instead of panicking.

Adds regression tests: an emoji straddling the cut point that panicked
pre-fix, 2/3/4-byte multibyte variants, ASCII-parity vs the original
slice, short-multibyte verbatim, and an end-to-end run() over a real
multibyte directory tree.
@github-actions

Copy link
Copy Markdown

👋 Thanks for the PR! It looks like this targets master, but all PRs should target the develop branch.

Please update the base branch:

  1. Click Edit at the top right of this PR
  2. Change the base branch from master to develop

See CONTRIBUTING.md for details.

@ryanmurf
ryanmurf merged commit eb70ed9 into master May 30, 2026
15 of 16 checks passed
@ryanmurf
ryanmurf deleted the fix/find-cmd-utf8-char-boundary-panic branch May 30, 2026 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant