Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dd: fail on missing number in count, fix #5904 #5920

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/uu/dd/src/parseargs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ fn parse_bytes_only(s: &str) -> Result<u64, ParseError> {
fn parse_bytes_no_x(full: &str, s: &str) -> Result<u64, ParseError> {
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')) {
Expand Down
8 changes: 7 additions & 1 deletion src/uucore/src/lib/parser/parse_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
/// 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"
Expand Down Expand Up @@ -48,6 +50,10 @@
self
}

pub fn with_allow_empty_numeric(&mut self, value: bool) -> &mut Self {
self.no_empty_numeric = value;

Check warning on line 54 in src/uucore/src/lib/parser/parse_size.rs

View check run for this annotation

Codecov / codecov/patch

src/uucore/src/lib/parser/parse_size.rs#L53-L54

Added lines #L53 - L54 were not covered by tests
self
}

Check warning on line 56 in src/uucore/src/lib/parser/parse_size.rs

View check run for this annotation

Codecov / codecov/patch

src/uucore/src/lib/parser/parse_size.rs#L56

Added line #L56 was not covered by tests
/// Parse a size string into a number of bytes.
///
/// A size string comprises an integer and an optional unit. The unit
Expand Down Expand Up @@ -160,7 +166,7 @@
// 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)?
Expand Down
10 changes: 10 additions & 0 deletions tests/by-util/test_dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
Loading