checksum: Fix EOF handling
Using `Read::read_exact` is not the correct way to read a file in chunks. When it returns `UnexpectedEof`, the buffer has data in it, but is zero-padded to the end. This causes the calculated checksum to be incorrect.master
parent
6d0bfeedaf
commit
4c7192582d
11
src/main.rs
11
src/main.rs
|
@ -303,14 +303,11 @@ fn checksum(path: impl AsRef<Path>) -> std::io::Result<Vec<u8>> {
|
|||
let mut blake = Blake2b512::new();
|
||||
loop {
|
||||
let mut buf = vec![0u8; 16384];
|
||||
match f.read_exact(&mut buf) {
|
||||
Ok(_) => blake.update(buf),
|
||||
Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
|
||||
blake.update(buf);
|
||||
break;
|
||||
}
|
||||
Err(e) => return Err(e),
|
||||
let sz = f.read(&mut buf)?;
|
||||
if sz == 0 {
|
||||
break;
|
||||
}
|
||||
blake.update(&buf[..sz]);
|
||||
}
|
||||
Ok(blake.finalize().to_vec())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue