launcher: avoid UTF-8 char-boundary panic in replace_ignore_case #35

Merged
ahmetbagci8 merged 2 commits from upstream-pr/35 into dev 2026-11-29 20:55:24 +00:00
ahmetbagci8 commented 2026-11-29 12:35:32 +00:00 (Migrated from github.com)

Imported from edsuuu/VI-BR-PORT. Originally posted by ahmetbagci8 on 2026-11-29T12:35:32Z.


Problem

In pps/vi-launcher/src/ui.rs,
eplace_ignore_case checked lower.len() != text.len() and assumed equal total byte lengths meant byte offsets in ext.to_lowercase() aligned 1:1 with UTF-8 char boundaries in ext.

When ext contains Unicode characters whose lowercase byte-length changes cancel out (for example two 'İ' U+0130 2 -> 3 bytes and one 'K' U+212A 3 -> 1 bytes), lower.len() == text.len() still holds, but lower[at..].find(&needle) returns a byte offset that lands inside a multi-byte character of ext, panicking at &text[at..at + i]. It also fell back to case-sensitive ext.replace whenever ext contained any length-changing character like 'İ'.

Fix

Build lower char-by-char alongside a byte-offset map back to original UTF-8 char boundaries in ext, and slice ext only at mapped boundaries. Added unit test
eplace_ignore_case_handles_unicode_case_length_changes.

Verification

  • cargo test -p vi-launcher mask_tests (2 passed, 0 failed)
  • cargo clippy -p vi-launcher --tests (0 warnings)
<!-- greyfinch-sync:edsuuu/VI-BR-PORT:issue:5550758861 --> Imported from [edsuuu/VI-BR-PORT](https://github.com/edsuuu/VI-BR-PORT/pull/35). Originally posted by [ahmetbagci8](https://github.com/ahmetbagci8) on 2026-11-29T12:35:32Z. --- ## Problem In pps/vi-launcher/src/ui.rs, eplace_ignore_case checked lower.len() != text.len() and assumed equal total byte lengths meant byte offsets in ext.to_lowercase() aligned 1:1 with UTF-8 char boundaries in ext. When ext contains Unicode characters whose lowercase byte-length changes cancel out (for example two 'İ' U+0130 2 -> 3 bytes and one 'K' U+212A 3 -> 1 bytes), lower.len() == text.len() still holds, but lower[at..].find(&needle) returns a byte offset that lands inside a multi-byte character of ext, panicking at &text[at..at + i]. It also fell back to case-sensitive ext.replace whenever ext contained any length-changing character like 'İ'. ## Fix Build lower char-by-char alongside a byte-offset map back to original UTF-8 char boundaries in ext, and slice ext only at mapped boundaries. Added unit test eplace_ignore_case_handles_unicode_case_length_changes. ## Verification - cargo test -p vi-launcher mask_tests (2 passed, 0 failed) - cargo clippy -p vi-launcher --tests (0 warnings)
edsuuu commented 2026-11-29 13:07:09 +00:00 (Migrated from github.com)

Imported from edsuuu/VI-BR-PORT. Originally posted by edsuuu on 2026-11-29T13:07:09Z.

Code review location: apps/vi-launcher/src/ui.rs; line 2021

@@ -1996,4 +2015,13 @@ mod mask_tests {
         assert!(!masked.to_lowercase().contains(&home.to_lowercase()), "{masked}");
         assert!(masked.starts_with("%USERPROFILE%\\Desktop"));
     }
+
+    #[test]
+    fn replace_ignore_case_handles_unicode_case_length_changes() {
+        // 'İ' (2B -> 3B) x2 (+2B) + 'K' (3B -> 1B) (-2B): total byte length is

The comment can name them by code point, e.g. U+0130 (2B -> 3B) x2 (+2B) + U+212A Kelvin sign (3B -> 1B).

<!-- greyfinch-sync:edsuuu/VI-BR-PORT:review-comment:4731501752 --> Imported from [edsuuu/VI-BR-PORT](https://github.com/edsuuu/VI-BR-PORT/pull/35#discussion_r4731501752). Originally posted by [edsuuu](https://github.com/edsuuu) on 2026-11-29T13:07:09Z. **Code review location:** `apps/vi-launcher/src/ui.rs`; line 2021 ~~~~diff @@ -1996,4 +2015,13 @@ mod mask_tests { assert!(!masked.to_lowercase().contains(&home.to_lowercase()), "{masked}"); assert!(masked.starts_with("%USERPROFILE%\\Desktop")); } + + #[test] + fn replace_ignore_case_handles_unicode_case_length_changes() { + // 'İ' (2B -> 3B) x2 (+2B) + 'K' (3B -> 1B) (-2B): total byte length is ~~~~ --- The comment can name them by code point, e.g. `U+0130 (2B -> 3B) x2 (+2B) + U+212A Kelvin sign (3B -> 1B)`.
edsuuu commented 2026-11-29 13:07:09 +00:00 (Migrated from github.com)

Imported from edsuuu/VI-BR-PORT. Originally posted by edsuuu on 2026-11-29T13:07:09Z.

Code review location: apps/vi-launcher/src/ui.rs; line 2023

@@ -1996,4 +2015,13 @@ mod mask_tests {
         assert!(!masked.to_lowercase().contains(&home.to_lowercase()), "{masked}");
         assert!(masked.starts_with("%USERPROFILE%\\Desktop"));
     }
+
+    #[test]
+    fn replace_ignore_case_handles_unicode_case_length_changes() {
+        // 'İ' (2B -> 3B) x2 (+2B) + 'K' (3B -> 1B) (-2B): total byte length is
+        // unchanged, but byte offsets in `text.to_lowercase()` diverge from `text`.
+        assert_eq!(super::replace_ignore_case("İİaK", "a", "X"), "İİXK");

"\u{130}\u{130}a\u{212A}" and "\u{130}\u{130}X\u{212A}" keep the same bytes.

<!-- greyfinch-sync:edsuuu/VI-BR-PORT:review-comment:4731501756 --> Imported from [edsuuu/VI-BR-PORT](https://github.com/edsuuu/VI-BR-PORT/pull/35#discussion_r4731501756). Originally posted by [edsuuu](https://github.com/edsuuu) on 2026-11-29T13:07:09Z. **Code review location:** `apps/vi-launcher/src/ui.rs`; line 2023 ~~~~diff @@ -1996,4 +2015,13 @@ mod mask_tests { assert!(!masked.to_lowercase().contains(&home.to_lowercase()), "{masked}"); assert!(masked.starts_with("%USERPROFILE%\\Desktop")); } + + #[test] + fn replace_ignore_case_handles_unicode_case_length_changes() { + // 'İ' (2B -> 3B) x2 (+2B) + 'K' (3B -> 1B) (-2B): total byte length is + // unchanged, but byte offsets in `text.to_lowercase()` diverge from `text`. + assert_eq!(super::replace_ignore_case("İİaK", "a", "X"), "İİXK"); ~~~~ --- `"\u{130}\u{130}a\u{212A}"` and `"\u{130}\u{130}X\u{212A}"` keep the same bytes.
edsuuu commented 2026-11-29 13:07:09 +00:00 (Migrated from github.com)

Imported from edsuuu/VI-BR-PORT. Originally posted by edsuuu on 2026-11-29T13:07:09Z.

Code review location: apps/vi-launcher/src/ui.rs; line 2024

@@ -1996,4 +2015,13 @@ mod mask_tests {
         assert!(!masked.to_lowercase().contains(&home.to_lowercase()), "{masked}");
         assert!(masked.starts_with("%USERPROFILE%\\Desktop"));
     }
+
+    #[test]
+    fn replace_ignore_case_handles_unicode_case_length_changes() {
+        // 'İ' (2B -> 3B) x2 (+2B) + 'K' (3B -> 1B) (-2B): total byte length is
+        // unchanged, but byte offsets in `text.to_lowercase()` diverge from `text`.
+        assert_eq!(super::replace_ignore_case("İİaK", "a", "X"), "İİXK");
+        assert_eq!(super::replace_ignore_case("C:\\Users\\İsmail\\Log", "c:\\users\\i\u{307}smail", "%USERPROFILE%"), "%USERPROFILE%\\Log");

"C:\\Users\\\u{130}smail\\Log" likewise.

<!-- greyfinch-sync:edsuuu/VI-BR-PORT:review-comment:4731501761 --> Imported from [edsuuu/VI-BR-PORT](https://github.com/edsuuu/VI-BR-PORT/pull/35#discussion_r4731501761). Originally posted by [edsuuu](https://github.com/edsuuu) on 2026-11-29T13:07:09Z. **Code review location:** `apps/vi-launcher/src/ui.rs`; line 2024 ~~~~diff @@ -1996,4 +2015,13 @@ mod mask_tests { assert!(!masked.to_lowercase().contains(&home.to_lowercase()), "{masked}"); assert!(masked.starts_with("%USERPROFILE%\\Desktop")); } + + #[test] + fn replace_ignore_case_handles_unicode_case_length_changes() { + // 'İ' (2B -> 3B) x2 (+2B) + 'K' (3B -> 1B) (-2B): total byte length is + // unchanged, but byte offsets in `text.to_lowercase()` diverge from `text`. + assert_eq!(super::replace_ignore_case("İİaK", "a", "X"), "İİXK"); + assert_eq!(super::replace_ignore_case("C:\\Users\\İsmail\\Log", "c:\\users\\i\u{307}smail", "%USERPROFILE%"), "%USERPROFILE%\\Log"); ~~~~ --- `"C:\\Users\\\u{130}smail\\Log"` likewise.
edsuuu commented 2026-11-29 13:07:09 +00:00 (Migrated from github.com)

Imported from edsuuu/VI-BR-PORT. Originally posted by edsuuu on 2026-11-29T13:07:09Z.

Review: CHANGES_REQUESTED


Thanks, the fix is right and the test covers the case well. One thing stops the CI: the repository rule cargo test -p xtask (xtask/src/english.rs) allows only ASCII letters in comments and literals. Could you write the non-ASCII characters as escapes? The bytes stay the same.

<!-- greyfinch-sync:edsuuu/VI-BR-PORT:review:6103002567 --> Imported from [edsuuu/VI-BR-PORT](https://github.com/edsuuu/VI-BR-PORT/pull/35#pullrequestreview-6103002567). Originally posted by [edsuuu](https://github.com/edsuuu) on 2026-11-29T13:07:09Z. **Review:** CHANGES_REQUESTED --- Thanks, the fix is right and the test covers the case well. One thing stops the CI: the repository rule `cargo test -p xtask` (`xtask/src/english.rs`) allows only ASCII letters in comments and literals. Could you write the non-ASCII characters as escapes? The bytes stay the same.
ahmetbagci8 commented 2026-11-29 13:25:08 +00:00 (Migrated from github.com)

Imported from edsuuu/VI-BR-PORT. Originally posted by ahmetbagci8 on 2026-11-29T13:25:08Z.


Updated the test comments and literals to use \\u{0130}\ / \\u{212A}\ escapes and verified \cargo test -p xtask\ (34 passed, 0 failed) along with \cargo test -p vi-launcher mask_tests\ and \cargo clippy -p vi-launcher --tests.

<!-- greyfinch-sync:edsuuu/VI-BR-PORT:comment:7256522960 --> Imported from [edsuuu/VI-BR-PORT](https://github.com/edsuuu/VI-BR-PORT/pull/35#issuecomment-7256522960). Originally posted by [ahmetbagci8](https://github.com/ahmetbagci8) on 2026-11-29T13:25:08Z. --- Updated the test comments and literals to use \\\u{0130}\ / \\\u{212A}\ escapes and verified \cargo test -p xtask\ (34 passed, 0 failed) along with \cargo test -p vi-launcher mask_tests\ and \cargo clippy -p vi-launcher --tests\.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
edsuuu/VI-BR-PORT!35
No description provided.