Skip to content

Commit b8aa0b0

Browse files
committed
sort: fix panic when collator not available in worker threads
1 parent a04ca6b commit b8aa0b0

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/uucore/src/lib/features/i18n/collator.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ pub fn locale_cmp(left: &[u8], right: &[u8]) -> Ordering {
8080
if get_collating_locale().0 == DEFAULT_LOCALE {
8181
left.cmp(right)
8282
} else {
83+
// Fall back to byte comparison if collator is not available
8384
COLLATOR
8485
.get()
85-
.expect("Collator was not initialized")
86-
.compare_utf8(left, right)
86+
.map(|c| c.compare_utf8(left, right))
87+
.unwrap_or_else(|| left.cmp(right))
8788
}
8889
}

tests/by-util/test_sort.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2713,4 +2713,21 @@ fn test_locale_complex_utf8_sorting() {
27132713
.stdout_is("apple\nApple\nbanana\nBanana\nzebra\nZebra\n");
27142714
}
27152715

2716+
#[test]
2717+
fn test_locale_utf8_with_key_field() {
2718+
// Regression test for issue #10909
2719+
// Sort should not panic when using -k flag with UTF-8 locale
2720+
// The bug occurred when rayon worker threads tried to access an uninitialized collator
2721+
let input = "a b 5433 down data path1 path2 path3 path4 path5
2722+
c d 5435 down data path1 path2 path3 path4 path5
2723+
e f 5436 down data path1 path2 path3 path4 path5\n";
2724+
2725+
new_ucmd!()
2726+
.env("LANG", "en_US.utf8")
2727+
.arg("-k3")
2728+
.pipe_in(input)
2729+
.succeeds()
2730+
.stdout_is(input);
2731+
}
2732+
27162733
/* spell-checker: enable */

0 commit comments

Comments
 (0)