Skip to content

Commit 54566dd

Browse files
authored
sort: fix panic when collator not available in worker threads (#10915)
1 parent eea061d commit 54566dd

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ 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_or_else(|| left.cmp(right), |c| c.compare_utf8(left, right))
8787
}
8888
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ pub fn get_locale_from_env(locale_name: &str) -> (Locale, UEncoding) {
5959
// locale. Treat the special case of the given locale being "C"
6060
// which becomes the default locale.
6161
let encoding = if (locale != DEFAULT_LOCALE || bcp47 == "C")
62-
&& split
63-
.next()
64-
.is_some_and(|enc| enc.to_lowercase() == "utf-8")
65-
{
62+
&& split.next().is_some_and(|enc| {
63+
let lower = enc.to_lowercase();
64+
lower == "utf-8" || lower == "utf8"
65+
}) {
6666
UEncoding::Utf8
6767
} else {
6868
UEncoding::Ascii

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)