From 1a811492ea38e59afb39a10a4a7c4271332cc464 Mon Sep 17 00:00:00 2001 From: Jake Wallin Date: Mon, 23 Nov 2026 11:55:49 +0000 Subject: [PATCH] truncate: interpret zero-padded sizes as decimal --- src/uu/truncate/src/truncate.rs | 4 +-- .../src/lib/features/parser/parse_size.rs | 32 ++++++++++++++++--- tests/by-util/test_truncate.rs | 15 +++++++++ 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/uu/truncate/src/truncate.rs b/src/uu/truncate/src/truncate.rs index e68c11b64cd..9f6ab0a1f1f 100644 --- a/src/uu/truncate/src/truncate.rs +++ b/src/uu/truncate/src/truncate.rs @@ -454,7 +454,7 @@ fn is_modifier(c: char) -> bool { /// Parse a size string with optional modifier symbol as its first character. /// -/// A size string is as described in [`Parser::parse_u64`]. The first character +/// Leading zeros in the size are decimal padding. The first character /// of `size_string` might be a modifier symbol, like `'+'` or /// `'<'`. The first element of the pair returned by this function /// indicates which modifier symbol was present, or @@ -488,7 +488,7 @@ fn parse_mode_and_size(size_string: &str) -> Result>(); Parser::default() .with_allow_list(&allow_list_ref) - .parse_u64(size_string) + .parse_u64_with_decimal_leading_zeros(size_string) .map(match c { '+' => TruncateMode::Extend, '-' => TruncateMode::Reduce, diff --git a/src/uucore/src/lib/features/parser/parse_size.rs b/src/uucore/src/lib/features/parser/parse_size.rs index 39e52ad6d99..64608b033b2 100644 --- a/src/uucore/src/lib/features/parser/parse_size.rs +++ b/src/uucore/src/lib/features/parser/parse_size.rs @@ -259,12 +259,18 @@ impl<'parser> Parser<'parser> { /// assert_eq!(Ok(44251 * 1024 * 1024), parser.parse("0b1010110011011011")); // 0b1010110011011011 is 44251 in decimal, default M /// ``` pub fn parse(&self, size: &str) -> Result { + self.parse_with_number_system(size, Self::determine_number_system(size)) + } + + fn parse_with_number_system( + &self, + size: &str, + number_system: NumberSystem, + ) -> Result { if size.is_empty() { return Err(ParseSizeError::parse_failure(size)); } - let number_system = Self::determine_number_system(size); - // Split the size argument into numeric and unit parts // For example, if the argument is "123K", the numeric part is "123", and // the unit is "K" @@ -391,9 +397,25 @@ impl<'parser> Parser<'parser> { /// Same as `parse()` but tries to return u64 pub fn parse_u64(&self, size: &str) -> Result { - self.parse(size).and_then(|num_u128| { - u64::try_from(num_u128).map_err(|_| ParseSizeError::size_too_big(size)) - }) + self.parse_u64_with_number_system(size, Self::determine_number_system(size)) + } + + /// Like [`Self::parse_u64`], but a leading zero is decimal padding. + pub fn parse_u64_with_decimal_leading_zeros(&self, size: &str) -> Result { + let system = match Self::determine_number_system(size) { + NumberSystem::Octal => NumberSystem::Decimal, + system => system, + }; + self.parse_u64_with_number_system(size, system) + } + + fn parse_u64_with_number_system( + &self, + size: &str, + number_system: NumberSystem, + ) -> Result { + let value = self.parse_with_number_system(size, number_system)?; + u64::try_from(value).map_err(|_| ParseSizeError::size_too_big(size)) } /// Same as `parse_u64()`, except returns `u64::MAX` on overflow diff --git a/tests/by-util/test_truncate.rs b/tests/by-util/test_truncate.rs index aec3ab0ec9a..7cb6eb13864 100644 --- a/tests/by-util/test_truncate.rs +++ b/tests/by-util/test_truncate.rs @@ -38,6 +38,21 @@ fn test_increase_file_size_kb() { assert_eq!(expected, actual, "expected '{expected}' got '{actual}'"); } +#[test] +fn test_zero_padded_decimal_size() { + for (size, original, expected) in [ + ("010", "ABCDEFGHIJKL", "ABCDEFGHIJ"), + ("<010", "ABCDEFGHIJ", "ABCDEFGHIJ"), + ("009", "ABCDEFGHIJ", "ABCDEFGHI"), + ("00K", "ABCDEFGHIJ", ""), + ] { + let (at, mut ucmd) = at_and_ucmd!(); + at.write(FILE1, original); + ucmd.args(&["-s", size, FILE1]).succeeds().no_output(); + assert_eq!(at.read(FILE1), expected, "size {size}"); + } +} + #[test] fn test_size_above_i64_max_is_rejected_without_creating_file() { // A size above i64::MAX is rejected up front and the file is not created.