From 6f3ce6763d32b3b8a06917f2d2b1627293fbaa65 Mon Sep 17 00:00:00 2001 From: Denis Andreyev Date: Tue, 3 Nov 2026 03:24:12 +0500 Subject: [PATCH] uucore: break locale_cmp ties by bytes The collator returns Equal for strings that only differ in characters it gives no weight, such as punctuation or zero-width characters. expr, comm and join read Equal as "same string", so in a non-C locale `expr /srv/uploads/../.. = /srv/uploads` printed 1, comm listed the two as common lines and join paired keys like ad-min and admin. sort -u with a key dropped one of two keys that only differ in such characters. Fall back to comparing the reversed bytes when the collator says Equal. This keeps the order total and consistent with equality, and it is the tie order sort already uses for whole lines, including with -m. comm and join therefore accept the output of sort, and ls and test list such ties in the same order as GNU. sort's own reversed-bytes fallback after locale_cmp is now dead code and is removed. Closes #15752 --- src/uu/sort/src/sort.rs | 7 +-- src/uucore/src/lib/features/i18n/collator.rs | 10 ++-- tests/by-util/test_comm.rs | 38 ++++++++++++++ tests/by-util/test_expr.rs | 22 ++++++++- tests/by-util/test_join.rs | 20 +++++++- tests/by-util/test_sort.rs | 52 +++++++++++++++++++- 6 files changed, 138 insertions(+), 11 deletions(-) diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index cec38b32a24..9f80ce35986 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -2793,11 +2793,8 @@ pub fn merge_compare<'a>( #[cfg(feature = "i18n-collator")] #[inline(never)] fn compare_lines_with_collator(a: &Line<'_>, b: &Line<'_>, settings: &GlobalSettings) -> Ordering { - let mut cmp = locale_cmp(a.line, b.line); - if cmp == Ordering::Equal { - // Equal keys for inputs like `01` and `0_1`; fall back to (reversed) byte order. - cmp = b.line.cmp(a.line); - } + // `locale_cmp` already breaks collator ties by reversed bytes. + let cmp = locale_cmp(a.line, b.line); if settings.reverse { cmp.reverse() } else { cmp } } diff --git a/src/uucore/src/lib/features/i18n/collator.rs b/src/uucore/src/lib/features/i18n/collator.rs index 7acec13cfbd..11851f80a91 100644 --- a/src/uucore/src/lib/features/i18n/collator.rs +++ b/src/uucore/src/lib/features/i18n/collator.rs @@ -88,14 +88,18 @@ pub fn compute_sort_key_utf8(input: &[u8], buf: &mut Vec) { } /// Compare both strings with regard to the current locale. +/// +/// Collator ties are broken by reversed bytes, as `sort` does for whole lines, +/// so `Equal` means identical bytes. pub fn locale_cmp(left: &[u8], right: &[u8]) -> Ordering { // If the detected locale is 'C', just do byte-wise comparison if get_collating_locale().0 == DEFAULT_LOCALE { left.cmp(right) } else { // Fall back to byte comparison if collator is not available - COLLATOR - .get() - .map_or_else(|| left.cmp(right), |c| c.compare_utf8(left, right)) + COLLATOR.get().map_or_else( + || left.cmp(right), + |c| c.compare_utf8(left, right).then_with(|| right.cmp(left)), + ) } } diff --git a/tests/by-util/test_comm.rs b/tests/by-util/test_comm.rs index 0064205398e..9215200ac73 100644 --- a/tests/by-util/test_comm.rs +++ b/tests/by-util/test_comm.rs @@ -833,6 +833,44 @@ fn test_locale_collation() { .stdout_only("a1\n"); } +#[uutests::wasi_ignore(NoLocaleData)] +#[test] +#[cfg(unix)] +fn test_locale_collation_accepts_the_tie_order_of_sort() { + // The collator gives U+200B no weight, so `ab` and `a\u{200b}b` tie there. + // `sort` puts the longer one first, and `comm` must read that as sorted. + let ts = TestScenario::new(util_name!()); + ts.fixtures.write("one", "a\u{200b}b\nab\n"); + ts.fixtures.write("two", "ab\n"); + + ts.ucmd() + .env("LC_ALL", "en_US.UTF-8") + .args(&["--check-order", "one", "two"]) + .succeeds() + .stdout_only("a\u{200b}b\n\t\tab\n"); +} + +#[uutests::wasi_ignore(NoLocaleData)] +#[test] +#[cfg(unix)] +fn test_locale_collation_does_not_match_punctuation_variants() { + // The collator gives no weight to `/` and `.`, but these lines differ. + let locale = "en_US.UTF-8"; + if !is_locale_available(locale) { + return; + } + let scene = TestScenario::new(util_name!()); + scene.fixtures.write("allowed", "/srv/uploads\n"); + scene.fixtures.write("requested", "/srv/uploads/../..\n"); + + scene + .ucmd() + .env("LC_ALL", locale) + .args(&["-12", "allowed", "requested"]) + .succeeds() + .no_stdout(); +} + #[test] #[cfg(unix)] fn test_c_locale_still_orders_by_bytes() { diff --git a/tests/by-util/test_expr.rs b/tests/by-util/test_expr.rs index 44c690e0fb1..55ff4daafe1 100644 --- a/tests/by-util/test_expr.rs +++ b/tests/by-util/test_expr.rs @@ -6,7 +6,7 @@ // spell-checker:ignore αbcdef ; (people) kkos // spell-checker:ignore aabcccd aabcd aabd abbb abbbd abbcabc abbcac abbcbbbd abbcbd // spell-checker:ignore abbccd abcabc abcac acabc andand bigcmp bignum emptysub -// spell-checker:ignore orempty oror bcdef fedcb GHSA xjfw pqfw +// spell-checker:ignore orempty oror bcdef fedcb GHSA xjfw pqfw zabc use rstest::rstest; use uutests::new_ucmd; @@ -1701,6 +1701,26 @@ mod locale_aware { .stdout_only(output); } } + + #[uutests::wasi_ignore(NoLocaleData)] + #[test] + fn test_expr_equality_is_exact_in_locale() { + // The collator gives no weight to punctuation, but `=` must still + // tell these strings apart. + for (left, right) in [("/srv/uploads/../..", "/srv/uploads"), ("r.o.o.t", "root")] { + new_ucmd!() + .args(&[left, "=", right]) + .env("LC_ALL", "en_US.UTF-8") + .run() + .code_is(1) + .stdout_only("0\n"); + new_ucmd!() + .args(&[left, "!=", right]) + .env("LC_ALL", "en_US.UTF-8") + .succeeds() + .stdout_only("1\n"); + } + } } /// Tests for multibyte character arithmetic in expr diff --git a/tests/by-util/test_join.rs b/tests/by-util/test_join.rs index 2506478c4d6..345b3d450ea 100644 --- a/tests/by-util/test_join.rs +++ b/tests/by-util/test_join.rs @@ -3,7 +3,7 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -// spell-checker:ignore (words) autoformat nocheck FILENUM +// spell-checker:ignore (words) autoformat nocheck FILENUM svcabc use rstest::rstest; #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))] @@ -922,6 +922,24 @@ fn test_locale_collation() { .stdout_contains("ab:d 1 x"); } +#[uutests::wasi_ignore(NoLocaleData)] +#[cfg(unix)] +#[test] +fn test_locale_collation_does_not_pair_punctuation_variants() { + // The collator gives no weight to `-`, but the keys differ. + let ts = TestScenario::new(util_name!()); + let at = &ts.fixtures; + + at.write("accounts", "admin:1\n"); + at.write("requests", "ad-min:2\n"); + + ts.ucmd() + .env("LC_ALL", "en_US.UTF-8") + .args(&["-t:", "accounts", "requests"]) + .succeeds() + .no_stdout(); +} + #[uutests::wasi_ignore(Usize32BitOverflow)] #[test] fn test_incompatible_fields_reports_exact_field_number() { diff --git a/tests/by-util/test_sort.rs b/tests/by-util/test_sort.rs index 7f63dcec076..694d7c0ace6 100644 --- a/tests/by-util/test_sort.rs +++ b/tests/by-util/test_sort.rs @@ -3,7 +3,7 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -// spell-checker:ignore (words) ints (linux) NOFILE dfgi abmon avril naab +// spell-checker:ignore (words) ints (linux) NOFILE dfgi abmon avril naab Xena #![allow(clippy::cast_possible_wrap)] @@ -4136,6 +4136,56 @@ fn test_consistent_sorting_with_i18n_collate() { .stdout_is(expected_output); } +#[test] +#[cfg(unix)] +fn test_unique_key_keeps_keys_the_collator_cannot_tell_apart() { + // The collator gives U+200B no weight, so these keys compare `Equal` + // there, but they are different strings and `-u` must keep both. + let locale = "en_US.UTF-8"; + if !is_locale_available(locale) { + return; + } + let out = new_ucmd!() + .env("LC_ALL", locale) + .args(&["-u", "-k1,1"]) + .pipe_in("ab x\na\u{200b}b y\n") + .succeeds() + .stdout_move_str(); + // GNU orders the tie like a full sort does: by reversed bytes. + assert_eq!(out, "a\u{200b}b y\nab x\n"); +} + +#[test] +#[cfg(unix)] +fn test_merge_breaks_collator_ties_like_a_full_sort() { + // The collator gives U+200B and `_` no weight, so these pairs tie there. A + // full sort orders such a tie by reversed bytes, and `-m` must do the same + // rather than fall back to the file order or to forward bytes. + let locale = "en_US.UTF-8"; + if !is_locale_available(locale) { + return; + } + for (first, second, expected) in [ + ("ab", "a\u{200b}b", "a\u{200b}b\nab\n"), + ("01", "0_1", "0_1\n01\n"), + ] { + let ts = TestScenario::new("sort"); + ts.fixtures.write("a.txt", &format!("{first}\n")); + ts.fixtures.write("b.txt", &format!("{second}\n")); + + ts.ucmd() + .env("LC_ALL", locale) + .args(&["a.txt", "b.txt"]) + .succeeds() + .stdout_is(expected); + ts.ucmd() + .env("LC_ALL", locale) + .args(&["-m", "a.txt", "b.txt"]) + .succeeds() + .stdout_is(expected); + } +} + #[test] fn test_merge_locale_collation() { // `sort -m` compares lines lazily with the collator instead of precomputed