From 4c7192582dccd6fa29e6c4191920d536a9155f54 Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Fri, 12 Jan 2024 09:42:31 -0600 Subject: [PATCH] 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. --- src/main.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 209bff4..cf49f57 100644 --- a/src/main.rs +++ b/src/main.rs @@ -303,14 +303,11 @@ fn checksum(path: impl AsRef) -> std::io::Result> { 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()) }