Skip to content

Commit

Permalink
Merge pull request #3085 from jfinkels/dd-zero-multiplier
Browse files Browse the repository at this point in the history
dd: show warning when using 0x size multiplier
  • Loading branch information
sylvestre authored Feb 7, 2022
2 parents 1f7c08d + fec662a commit 4d07083
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/uu/dd/src/parseargs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -356,6 +357,13 @@ fn parse_bytes_only(s: &str) -> Result<usize, ParseError> {
/// assert_eq!(parse_bytes_no_x("2k").unwrap(), 2 * 1024);
/// ```
fn parse_bytes_no_x(s: &str) -> Result<usize, ParseError> {
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),
Expand Down
33 changes: 33 additions & 0 deletions tests/by-util/test_dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!()
Expand Down

0 comments on commit 4d07083

Please sign in to comment.