fix(windows): UTF-16 boundary when stripping trailing dots/spaces#22
Open
fibibot wants to merge 1 commit into
Open
fix(windows): UTF-16 boundary when stripping trailing dots/spaces#22fibibot wants to merge 1 commit into
fibibot wants to merge 1 commit into
Conversation
`normalize_path` strips trailing `.` and ` ` from each path component on
Windows. The strip range was computed in UTF-8 bytes via
`as_encoded_bytes()`, then applied as a slice index into the UTF-16
`encode_wide()` vector. For components containing multi-byte characters
(Cyrillic, CJK, emoji, etc.) the UTF-8 byte length exceeds the UTF-16
unit count, so the slice indexes out of bounds and panics:
range end index 70 out of range for slice of length 55
Both characters being trimmed (`.` and ` `) are ASCII, so each trailing
byte corresponds to exactly one UTF-16 unit. Compute `trim_count` from
the byte trimming and translate it to a wide-vec end index
(`wide.len() - trim_count`).
Fixes denoland/deno#31761.
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.
Summary
normalize_pathstrips trailing.andfrom each path component on Windows. The strip range was computed in UTF-8 bytes viaas_encoded_bytes(), then applied as a slice index into the UTF-16encode_wide()vector. For components containing multi-byte characters (Cyrillic, CJK, emoji, etc.) the UTF-8 byte length exceeds the UTF-16 unit count, so the slice indexes out of bounds and panics:Both characters being trimmed (
.and) are ASCII, so each trailing byte corresponds to exactly one UTF-16 unit. The fix computestrim_countfrom the byte-side trimming and applies it aswide.len() - trim_counton the UTF-16 side.Fixes denoland/deno#31761.
Test plan
test_normalize_path_wincovering Cyrillic, CJK, and emoji components with trailing.— these would panic with the byte index, but slice cleanly with the UTF-16 index.cargo fmt --check,cargo clippy --lib --tests -- -D warnings.