From 35fa13559ec0ce1df810f237d7afb224a014e6dc Mon Sep 17 00:00:00 2001 From: arbelonson-source <269032023+arbelonson-source@users.noreply.github.com> Date: Mon, 31 Aug 2026 01:49:05 +0300 Subject: [PATCH] numfmt: find the suffix by byte, not by character MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `find_valid_number_with_suffix` ends the number at a byte offset and then looks for the suffix that many *characters* further in. The two agree only while every character is one byte, which the decimal separator is not in a locale like ar-SA, where it is the two-byte U+066B: $ LC_ALL=ar_SA.UTF-8 numfmt --from=si '1٫€K' thread 'main' panicked at src/uu/numfmt/src/format.rs:70:20: byte index 4 is not a char boundary; it is inside '€' (bytes 3..6) The number is `1٫`, three bytes. Skipping three characters walks past the `€` and finds the `K`, so `K` is taken as the suffix; slicing to three bytes plus one then cuts the `€` in half and the slice aborts the process. GNU rejects the input and exits 2. Take the suffix from the bytes after the number instead, so the two accountings cannot disagree. The two arms that returned the same slice are now one, since duplicating the arithmetic is what let it drift. Fixes #13937. --- src/uu/numfmt/src/format.rs | 12 +++++++----- tests/by-util/test_numfmt.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/uu/numfmt/src/format.rs b/src/uu/numfmt/src/format.rs index 9c03801ef7d..a8cbe319625 100644 --- a/src/uu/numfmt/src/format.rs +++ b/src/uu/numfmt/src/format.rs @@ -57,7 +57,11 @@ fn find_valid_number_with_suffix(s: &str, unit: Unit) -> Option<&str> { let accepts_suffix = unit != Unit::None; let accepts_i = [Unit::Auto, Unit::Iec(true)].contains(&unit); - let mut characters = s.chars().skip(numeric_part.len()); + // What follows the number begins where the number ends, which is a + // position in bytes: the decimal separator of a locale such as ar-SA is + // two bytes wide, so skipping that many *characters* instead lands past + // the suffix, and the byte index taken from it lands inside a character. + let mut characters = s[numeric_part.len()..].chars(); let potential_suffix = characters.next(); let potential_i = characters.next(); @@ -65,14 +69,12 @@ fn find_valid_number_with_suffix(s: &str, unit: Unit) -> Option<&str> { return Some(numeric_part); } + // Every suffix is one ASCII character, so the byte it ends on is known. match (potential_suffix, potential_i) { - (Some(suffix), None) if RawSuffix::try_from(&suffix).is_ok() => { - Some(&s[..=numeric_part.len()]) - } (Some(suffix), Some('i')) if accepts_i && RawSuffix::try_from(&suffix).is_ok() => { Some(&s[..numeric_part.len() + 2]) } - (Some(suffix), Some(_)) if RawSuffix::try_from(&suffix).is_ok() => { + (Some(suffix), _) if RawSuffix::try_from(&suffix).is_ok() => { Some(&s[..=numeric_part.len()]) } _ => Some(numeric_part), diff --git a/tests/by-util/test_numfmt.rs b/tests/by-util/test_numfmt.rs index 4db86944771..e5921ce0cf3 100644 --- a/tests/by-util/test_numfmt.rs +++ b/tests/by-util/test_numfmt.rs @@ -1614,6 +1614,35 @@ fn test_locale_fr_rejects_period() { .stderr_contains("invalid"); } +/// The suffix begins where the number ends, and ar-SA's decimal separator is +/// two bytes wide, so counting that end in characters walked into the middle +/// of the one after it and aborted the process. +#[test] +#[cfg_attr(wasi_runner, ignore = "WASI: locale env vars not propagated")] +fn test_locale_multibyte_separator_before_a_multibyte_char() { + for (unit, input) in [ + ("si", "1٫€K"), + ("si", "1٫€Kx"), + ("auto", "1٫€Ki"), + ("iec-i", "1٫€K"), + ("none", "1٫€K"), + ] { + new_ucmd!() + .env("LC_ALL", "ar_SA.UTF-8") + .args(&[format!("--from={unit}"), input.into()]) + .fails_with_code(2) + .stderr_contains("invalid suffix in input"); + } + + // The suffix that is there is still found: '€' is what is wrong with this + // one, not 'K'. + new_ucmd!() + .env("LC_ALL", "ar_SA.UTF-8") + .args(&["--from=si", "1٫Ki"]) + .fails_with_code(2) + .stderr_is("numfmt: invalid suffix in input '1٫Ki': 'i'\n"); +} + #[test] fn test_locale_c_uses_period() { // C locale should still use '.' as usual