From cdbae736f15fe527b93722bb57e489a422be3bee Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Wed, 25 Nov 2015 09:50:37 +0100 Subject: [PATCH 1/2] sum: use wrapping_add to avoid overflow panics --- src/sum/sum.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sum/sum.rs b/src/sum/sum.rs index 1febb2446..49b131e2c 100644 --- a/src/sum/sum.rs +++ b/src/sum/sum.rs @@ -32,7 +32,7 @@ fn bsd_sum(mut reader: Box) -> (usize, u16) { blocks_read += 1; for &byte in buf[..n].iter() { checksum = (checksum >> 1) + ((checksum & 1) << 15); - checksum += byte as u16; + checksum = checksum.wrapping_add(byte as u16); } }, _ => break, @@ -45,14 +45,14 @@ fn bsd_sum(mut reader: Box) -> (usize, u16) { fn sysv_sum(mut reader: Box) -> (usize, u16) { let mut buf = [0; 512]; let mut blocks_read = 0; - let mut ret = 0; + let mut ret = 0u32; loop { match reader.read(&mut buf) { Ok(n) if n != 0 => { blocks_read += 1; for &byte in buf[..n].iter() { - ret += byte as u32; + ret = ret.wrapping_add(byte as u32); } }, _ => break, From e7398b3ca72c8ae975e45021c3f4bb846a886b43 Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Wed, 25 Nov 2015 09:51:32 +0100 Subject: [PATCH 2/2] sum: fix sysv_stdin testcase --- tests/sum.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/sum.rs b/tests/sum.rs index eeb8c1f05..967f2119d 100644 --- a/tests/sum.rs +++ b/tests/sum.rs @@ -65,7 +65,7 @@ fn test_sysv_multiple_files() { fn test_sysv_stdin() { let (at, mut ucmd) = testing(UTIL_NAME); let input = at.read("lorem_ipsum.txt"); - let result = ucmd.run_piped_stdin(input); + let result = ucmd.arg("-s").run_piped_stdin(input); assert_empty_stderr!(result); assert!(result.success);