1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 11:07:44 +00:00

od: fix zero width user input (-w0)

This commit is contained in:
Wim Hueskes 2016-08-23 22:55:40 +02:00
parent 92fc286b0e
commit 184c4af76d
2 changed files with 19 additions and 2 deletions

View file

@ -205,12 +205,12 @@ impl OdOptions {
Some(s) => {
match s.parse::<usize>() {
Ok(i) => { i }
Err(_) => { 2 }
Err(_) => { 0 }
}
}
};
let min_bytes = formats.iter().fold(1, |max, next| cmp::max(max, next.formatter_item_info.byte_size));
if line_bytes % min_bytes != 0 {
if line_bytes == 0 || line_bytes % min_bytes != 0 {
show_warning!("invalid width {}; using {} instead", line_bytes, min_bytes);
line_bytes = min_bytes;
}

View file

@ -292,6 +292,23 @@ fn test_invalid_width(){
assert_eq!(result.stdout, expected_output);
}
#[test]
fn test_zero_width(){
let input : [u8; 4] = [0x00, 0x00, 0x00, 0x00];
let expected_output = unindent("
0000000 000000
0000002 000000
0000004
");
let result = new_ucmd!().arg("-w0").arg("-v").run_piped_stdin(&input[..]);
assert_eq!(result.stderr, "od: warning: invalid width 0; using 2 instead\n");
assert!(result.success);
assert_eq!(result.stdout, expected_output);
}
#[test]
fn test_width_without_value(){