From 184c4af76de23361f2e434b9e7d2845ea3363773 Mon Sep 17 00:00:00 2001 From: Wim Hueskes Date: Tue, 23 Aug 2016 22:55:40 +0200 Subject: [PATCH] od: fix zero width user input (-w0) --- src/od/od.rs | 4 ++-- tests/test_od.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/od/od.rs b/src/od/od.rs index a43a3fc1b..a20856ba0 100644 --- a/src/od/od.rs +++ b/src/od/od.rs @@ -205,12 +205,12 @@ impl OdOptions { Some(s) => { match s.parse::() { 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; } diff --git a/tests/test_od.rs b/tests/test_od.rs index adddacbe9..92828012f 100644 --- a/tests/test_od.rs +++ b/tests/test_od.rs @@ -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(){