From 9e44acf30702dc0ba716ec096904b4197b271629 Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Tue, 12 Jul 2022 15:05:17 +0200 Subject: [PATCH] numfmt: reject suffix if unit is "none" --- src/uu/numfmt/src/format.rs | 6 ++++++ tests/by-util/test_numfmt.rs | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/uu/numfmt/src/format.rs b/src/uu/numfmt/src/format.rs index 6f0a6cbf3..7c4ad723b 100644 --- a/src/uu/numfmt/src/format.rs +++ b/src/uu/numfmt/src/format.rs @@ -130,6 +130,12 @@ fn remove_suffix(i: f64, s: Option, u: &Unit) -> Result { "missing 'i' suffix in input: '{}{:?}' (e.g Ki/Mi/Gi)", i, raw_suffix )), + (Some((raw_suffix, with_i)), &Unit::None) => Err(format!( + "rejecting suffix in input: '{}{:?}{}' (consider using --from)", + i, + raw_suffix, + if with_i { "i" } else { "" } + )), (None, _) => Ok(i), (_, _) => Err("This suffix is unsupported for specified unit".to_owned()), } diff --git a/tests/by-util/test_numfmt.rs b/tests/by-util/test_numfmt.rs index dce2b0941..be46d7891 100644 --- a/tests/by-util/test_numfmt.rs +++ b/tests/by-util/test_numfmt.rs @@ -646,3 +646,19 @@ fn test_invalid_unit_size() { } } } + +#[test] +fn test_valid_but_forbidden_suffix() { + let numbers = vec!["12K", "12Ki"]; + + for number in numbers { + new_ucmd!() + .arg(number) + .fails() + .code_is(2) + .stderr_contains(format!( + "rejecting suffix in input: '{}' (consider using --from)", + number + )); + } +}