Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/uucore/src/lib/features/i18n/collator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
8 changes: 4 additions & 4 deletions src/uucore/src/lib/features/i18n/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions tests/by-util/test_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Loading