From fec662a6237c243a5d54d2ab3c3b63729d8aefff Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Tue, 1 Feb 2022 23:22:01 -0500 Subject: [PATCH] dd: show warning when using 0x size multiplier Show a warning when a block size includes "0x" since this is ambiguous: the user may have meant "multiply the next number by zero" or they may have meant "the following characters should be interpreted as a hexadecimal number". --- src/uu/dd/src/parseargs.rs | 8 ++++++++ tests/by-util/test_dd.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/uu/dd/src/parseargs.rs b/src/uu/dd/src/parseargs.rs index 915a99344..6bc7bcfd9 100644 --- a/src/uu/dd/src/parseargs.rs +++ b/src/uu/dd/src/parseargs.rs @@ -13,6 +13,7 @@ use super::*; use std::error::Error; use uucore::error::UError; use uucore::parse_size::ParseSizeError; +use uucore::show_warning; pub type Matches = ArgMatches; @@ -356,6 +357,13 @@ fn parse_bytes_only(s: &str) -> Result { /// assert_eq!(parse_bytes_no_x("2k").unwrap(), 2 * 1024); /// ``` fn parse_bytes_no_x(s: &str) -> Result { + if s == "0" { + show_warning!( + "{} is a zero multiplier; use {} if that is intended", + "0x".quote(), + "00x".quote() + ); + } let (num, multiplier) = match (s.find('c'), s.rfind('w'), s.rfind('b')) { (None, None, None) => match uucore::parse_size::parse_size(s) { Ok(n) => (n, 1), diff --git a/tests/by-util/test_dd.rs b/tests/by-util/test_dd.rs index e9a1f9468..d27122a75 100644 --- a/tests/by-util/test_dd.rs +++ b/tests/by-util/test_dd.rs @@ -198,6 +198,39 @@ fn test_x_multiplier() { .stdout_is("abcdef"); } +#[test] +fn test_zero_multiplier_warning() { + for arg in ["count", "seek", "skip"] { + new_ucmd!() + .args(&[format!("{}=00x1", arg).as_str(), "status=none"]) + .pipe_in("") + .succeeds() + .no_stdout() + .no_stderr(); + + new_ucmd!() + .args(&[format!("{}=0x1", arg).as_str(), "status=none"]) + .pipe_in("") + .succeeds() + .no_stdout() + .stderr_contains("warning: '0x' is a zero multiplier; use '00x' if that is intended"); + + new_ucmd!() + .args(&[format!("{}=0x0x1", arg).as_str(), "status=none"]) + .pipe_in("") + .succeeds() + .no_stdout() + .stderr_is("dd: warning: '0x' is a zero multiplier; use '00x' if that is intended\ndd: warning: '0x' is a zero multiplier; use '00x' if that is intended\n"); + + new_ucmd!() + .args(&[format!("{}=1x0x1", arg).as_str(), "status=none"]) + .pipe_in("") + .succeeds() + .no_stdout() + .stderr_contains("warning: '0x' is a zero multiplier; use '00x' if that is intended"); + } +} + #[test] fn test_final_stats_noxfer() { new_ucmd!()