mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
dd: fail on missing number in count
This commit is contained in:
parent
ab5a3656bf
commit
bd336ebbf1
3 changed files with 18 additions and 1 deletions
|
@ -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')) {
|
||||
|
|
|
@ -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)?
|
||||
|
|
|
@ -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")]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue