mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
sum: use wrapping_add to avoid overflow panics
This commit is contained in:
parent
6e4392779f
commit
cdbae736f1
1 changed files with 3 additions and 3 deletions
|
@ -32,7 +32,7 @@ fn bsd_sum(mut reader: Box<Read>) -> (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<Read>) -> (usize, u16) {
|
|||
fn sysv_sum(mut reader: Box<Read>) -> (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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue