fix(find): stop panicking on multi-byte UTF-8 directory names#2888
Open
AZERDSQ131 wants to merge 1 commit into
Open
fix(find): stop panicking on multi-byte UTF-8 directory names#2888AZERDSQ131 wants to merge 1 commit into
AZERDSQ131 wants to merge 1 commit into
Conversation
The directory-display truncation in find_cmd's output formatter sliced
a String at a raw byte offset (dir.len() - 47) with no check that the
offset lands on a UTF-8 character boundary. A directory path longer
than 50 bytes whose 47-bytes-from-the-end cut point falls inside a
multi-byte character (e.g. an accented letter like 'é') panics with
"byte index is not a char boundary" instead of printing a truncated
path.
Extracted the truncation into a small `truncate_dir_display` helper
and walk forward from the target byte offset to the next valid char
boundary before slicing, instead of slicing blindly. The loop always
terminates at dir.len(), which is always a boundary, so this cannot
infinite-loop or panic.
Adds three unit tests, including an exact reproduction of the
directory name from the reported crash
("Les Plongées de Juliette/5-Pipeline/Versions-imprimables") verified
to place the naive cut point mid-character before this fix.
Note: the original report mentioned the panic only appeared alongside
an unknown flag like -print. That didn't reproduce as a separate
condition here -- any directory name long enough to hit this
truncation path panics regardless of flags, which this fix covers.
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2851
rtk find's output formatter truncates long directory names for display with a raw byte-offset slice:If
dir.len() - 47lands inside a multi-byte UTF-8 character (e.g. an accented letter likeé), this panics withbyte index is not a char boundaryinstead of printing a truncated path.Repro (from the issue, reproduced in a unit test):
I confirmed byte 10 in that exact directory string is a UTF-8 continuation byte (0xA9), matching the panic message precisely.
Fix: extracted the truncation into a
truncate_dir_displayhelper and walk forward from the target byte offset to the next validchar_boundarybefore slicing, instead of slicing blindly. The loop always terminates atdir.len()(always a boundary), so it can't infinite-loop or panic.Adds three unit tests:
truncate_dir_display_does_not_panic_on_multibyte_boundary— the exact directory name from the reporttruncate_dir_display_passes_through_short_pathstruncate_dir_display_truncates_long_ascii_pathsNote on the "unknown flag" condition: the report mentions the panic only appeared alongside an unknown flag like
-print. That didn't reproduce as a separate condition on my end — any directory name long enough to hit this truncation path panics regardless of flags, and this fix covers that unconditionally. If there's a second, flag-specific bug still lurking, happy to dig further with a maintainer pointer.Ran the mandatory gate locally:
cargo fmt --all— no changes neededcargo clippy --all-targets— cleancargo test --all— 2396 passed (+ 6/11/11/14/5/11 in the other suites), 0 failed