Skip to content

Commit 5aff6ec

Browse files
authored
Merge pull request #5920 from sudhackar/fix-dd-empty-num
dd: fail on missing number in count, fix #5904
2 parents 9bd531b + bd336eb commit 5aff6ec

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/uu/dd/src/parseargs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ fn parse_bytes_only(s: &str) -> Result<u64, ParseError> {
509509
fn parse_bytes_no_x(full: &str, s: &str) -> Result<u64, ParseError> {
510510
let parser = SizeParser {
511511
capital_b_bytes: true,
512+
no_empty_numeric: true,
512513
..Default::default()
513514
};
514515
let (num, multiplier) = match (s.find('c'), s.rfind('w'), s.rfind('b')) {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use crate::display::Quotable;
1616
/// The [`Parser::parse`] function performs the parse.
1717
#[derive(Default)]
1818
pub struct Parser<'parser> {
19+
/// Whether to allow empty numeric strings.
20+
pub no_empty_numeric: bool,
1921
/// Whether to treat the suffix "B" as meaning "bytes".
2022
pub capital_b_bytes: bool,
2123
/// Whether to treat "b" as a "byte count" instead of "block"
@@ -48,6 +50,10 @@ impl<'parser> Parser<'parser> {
4850
self
4951
}
5052

53+
pub fn with_allow_empty_numeric(&mut self, value: bool) -> &mut Self {
54+
self.no_empty_numeric = value;
55+
self
56+
}
5157
/// Parse a size string into a number of bytes.
5258
///
5359
/// A size string comprises an integer and an optional unit. The unit
@@ -160,7 +166,7 @@ impl<'parser> Parser<'parser> {
160166
// parse string into u128
161167
let number: u128 = match number_system {
162168
NumberSystem::Decimal => {
163-
if numeric_string.is_empty() {
169+
if numeric_string.is_empty() && !self.no_empty_numeric {
164170
1
165171
} else {
166172
Self::parse_number(&numeric_string, 10, size)?

tests/by-util/test_dd.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,16 @@ fn test_nocache_stdin_error() {
15371537
.stderr_only(format!("dd: failed to discard cache for: 'standard input': {detail}\n0+0 records in\n0+0 records out\n"));
15381538
}
15391539

1540+
/// Test that dd fails when no number in count.
1541+
#[test]
1542+
fn test_empty_count_number() {
1543+
new_ucmd!()
1544+
.args(&["count=B"])
1545+
.fails()
1546+
.code_is(1)
1547+
.stderr_only("dd: invalid number: ‘B’\n");
1548+
}
1549+
15401550
/// Test for discarding system file cache.
15411551
#[test]
15421552
#[cfg(target_os = "linux")]

0 commit comments

Comments
 (0)