From 985b4848e69a4354a3c449f8bd08df0243c89a93 Mon Sep 17 00:00:00 2001 From: Devel Date: Mon, 27 Jul 2026 02:38:28 +0300 Subject: [PATCH 1/4] truncate: check if there's a number after the positive/negative sign in -s flag --- src/uu/truncate/src/truncate.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/uu/truncate/src/truncate.rs b/src/uu/truncate/src/truncate.rs index bdb2af011ab..5951dba7d8e 100644 --- a/src/uu/truncate/src/truncate.rs +++ b/src/uu/truncate/src/truncate.rs @@ -360,6 +360,13 @@ fn parse_mode_and_size(size_string: &str) -> Result Result TruncateMode::Absolute, }) } else { + if size_string.is_empty() { + return Err(ParseSizeError::ParseFailure("''".to_string())); + } Err(ParseSizeError::ParseFailure(size_string.to_string())) } } From a15703aa5fafcaff904bd5445f0eaf01cf89bb93 Mon Sep 17 00:00:00 2001 From: Devel Date: Mon, 27 Jul 2026 02:47:34 +0300 Subject: [PATCH 2/4] test_truncate: add test_empty_size and test_sign_as_a_size --- tests/by-util/test_truncate.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/by-util/test_truncate.rs b/tests/by-util/test_truncate.rs index d44723c36b6..743c6271823 100644 --- a/tests/by-util/test_truncate.rs +++ b/tests/by-util/test_truncate.rs @@ -491,3 +491,19 @@ fn test_truncate_non_utf8_paths() { // Test that truncate can handle non-UTF-8 filenames ts.ucmd().arg("-s").arg("10").arg(file_name).succeeds(); } + +#[test] +fn test_empty_size() { + new_ucmd!() + .args(&["-s", "", "asd"]) + .fails() + .stderr_is("truncate: Invalid number: ''\n"); +} + +#[test] +fn test_sign_as_a_size() { + new_ucmd!() + .args(&["-s", "+", "asd"]) + .fails() + .stderr_is("truncate: Invalid number: '+'\n"); +} From b79dfdbed53ee2b612b5c52c2730bcc6ff032f66 Mon Sep 17 00:00:00 2001 From: Devel Date: Mon, 27 Jul 2026 16:19:48 +0300 Subject: [PATCH 3/4] truncate: simplify empty argument and negative/postive sign checks --- src/uu/truncate/src/truncate.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/uu/truncate/src/truncate.rs b/src/uu/truncate/src/truncate.rs index 5951dba7d8e..7db36a887d3 100644 --- a/src/uu/truncate/src/truncate.rs +++ b/src/uu/truncate/src/truncate.rs @@ -361,9 +361,7 @@ fn parse_mode_and_size(size_string: &str) -> Result Result TruncateMode::Absolute, }) } else { - if size_string.is_empty() { - return Err(ParseSizeError::ParseFailure("''".to_string())); - } - Err(ParseSizeError::ParseFailure(size_string.to_string())) + Err(ParseSizeError::ParseFailure("''".to_string())); } } From e6ce4e64f6e7037479589d8388ac0fa6ab025f6a Mon Sep 17 00:00:00 2001 From: Devel Date: Mon, 27 Jul 2026 16:21:50 +0300 Subject: [PATCH 4/4] typo --- src/uu/truncate/src/truncate.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uu/truncate/src/truncate.rs b/src/uu/truncate/src/truncate.rs index 7db36a887d3..6e28c0108f8 100644 --- a/src/uu/truncate/src/truncate.rs +++ b/src/uu/truncate/src/truncate.rs @@ -383,7 +383,7 @@ fn parse_mode_and_size(size_string: &str) -> Result TruncateMode::Absolute, }) } else { - Err(ParseSizeError::ParseFailure("''".to_string())); + Err(ParseSizeError::ParseFailure("''".to_string())) } }