sort: fix panic when collator not available in worker threads (#10915)

This commit is contained in:
Sylvestre Ledru
2026-02-14 12:33:22 +01:00
committed by GitHub
parent eea061db56
commit 54566dd9da
3 changed files with 23 additions and 6 deletions
+2 -2
View File
@@ -80,9 +80,9 @@ pub fn locale_cmp(left: &[u8], right: &[u8]) -> Ordering {
if get_collating_locale().0 == DEFAULT_LOCALE {
left.cmp(right)
} else {
// Fall back to byte comparison if collator is not available
COLLATOR
.get()
.expect("Collator was not initialized")
.compare_utf8(left, right)
.map_or_else(|| left.cmp(right), |c| c.compare_utf8(left, right))
}
}
+4 -4
View File
@@ -59,10 +59,10 @@ pub fn get_locale_from_env(locale_name: &str) -> (Locale, UEncoding) {
// locale. Treat the special case of the given locale being "C"
// which becomes the default locale.
let encoding = if (locale != DEFAULT_LOCALE || bcp47 == "C")
&& split
.next()
.is_some_and(|enc| enc.to_lowercase() == "utf-8")
{
&& split.next().is_some_and(|enc| {
let lower = enc.to_lowercase();
lower == "utf-8" || lower == "utf8"
}) {
UEncoding::Utf8
} else {
UEncoding::Ascii
+17
View File
@@ -2713,4 +2713,21 @@ fn test_locale_complex_utf8_sorting() {
.stdout_is("apple\nApple\nbanana\nBanana\nzebra\nZebra\n");
}
#[test]
fn test_locale_utf8_with_key_field() {
// Regression test for issue #10909
// Sort should not panic when using -k flag with UTF-8 locale
// The bug occurred when rayon worker threads tried to access an uninitialized collator
let input = "a b 5433 down data path1 path2 path3 path4 path5
c d 5435 down data path1 path2 path3 path4 path5
e f 5436 down data path1 path2 path3 path4 path5\n";
new_ucmd!()
.env("LANG", "en_US.utf8")
.arg("-k3")
.pipe_in(input)
.succeeds()
.stdout_is(input);
}
/* spell-checker: enable */