From bd336ebbf1d316c192192d1c452ad315a57e2a83 Mon Sep 17 00:00:00 2001 From: Sudhakar Verma Date: Tue, 30 Jan 2024 20:21:25 +0530 Subject: [PATCH] dd: fail on missing number in count --- src/uu/dd/src/parseargs.rs | 1 + src/uucore/src/lib/parser/parse_size.rs | 8 +++++++- tests/by-util/test_dd.rs | 10 ++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/uu/dd/src/parseargs.rs b/src/uu/dd/src/parseargs.rs index be139b764..9b3e3911e 100644 --- a/src/uu/dd/src/parseargs.rs +++ b/src/uu/dd/src/parseargs.rs @@ -509,6 +509,7 @@ fn parse_bytes_only(s: &str) -> Result { fn parse_bytes_no_x(full: &str, s: &str) -> Result { let parser = SizeParser { capital_b_bytes: true, + no_empty_numeric: true, ..Default::default() }; let (num, multiplier) = match (s.find('c'), s.rfind('w'), s.rfind('b')) { diff --git a/src/uucore/src/lib/parser/parse_size.rs b/src/uucore/src/lib/parser/parse_size.rs index da1fca534..b963a7131 100644 --- a/src/uucore/src/lib/parser/parse_size.rs +++ b/src/uucore/src/lib/parser/parse_size.rs @@ -16,6 +16,8 @@ use crate::display::Quotable; /// The [`Parser::parse`] function performs the parse. #[derive(Default)] pub struct Parser<'parser> { + /// Whether to allow empty numeric strings. + pub no_empty_numeric: bool, /// Whether to treat the suffix "B" as meaning "bytes". pub capital_b_bytes: bool, /// Whether to treat "b" as a "byte count" instead of "block" @@ -48,6 +50,10 @@ impl<'parser> Parser<'parser> { self } + pub fn with_allow_empty_numeric(&mut self, value: bool) -> &mut Self { + self.no_empty_numeric = value; + self + } /// Parse a size string into a number of bytes. /// /// A size string comprises an integer and an optional unit. The unit @@ -160,7 +166,7 @@ impl<'parser> Parser<'parser> { // parse string into u128 let number: u128 = match number_system { NumberSystem::Decimal => { - if numeric_string.is_empty() { + if numeric_string.is_empty() && !self.no_empty_numeric { 1 } else { Self::parse_number(&numeric_string, 10, size)? diff --git a/tests/by-util/test_dd.rs b/tests/by-util/test_dd.rs index 93cfee067..b440d0f7e 100644 --- a/tests/by-util/test_dd.rs +++ b/tests/by-util/test_dd.rs @@ -1537,6 +1537,16 @@ fn test_nocache_stdin_error() { .stderr_only(format!("dd: failed to discard cache for: 'standard input': {detail}\n0+0 records in\n0+0 records out\n")); } +/// Test that dd fails when no number in count. +#[test] +fn test_empty_count_number() { + new_ucmd!() + .args(&["count=B"]) + .fails() + .code_is(1) + .stderr_only("dd: invalid number: ‘B’\n"); +} + /// Test for discarding system file cache. #[test] #[cfg(target_os = "linux")]