From d2eb4936b167becd5746a4139ba6bdc79a1a81a3 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 20 Mar 2018 16:43:12 +0100 Subject: [PATCH] Fix: Allocating of buffer does not work as expected with Vec::with_capacity() --- lib/entry/libimagentryref/src/generators/mod.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/entry/libimagentryref/src/generators/mod.rs b/lib/entry/libimagentryref/src/generators/mod.rs index 21b31d73..427ebaaa 100644 --- a/lib/entry/libimagentryref/src/generators/mod.rs +++ b/lib/entry/libimagentryref/src/generators/mod.rs @@ -205,8 +205,19 @@ macro_rules! make_sha_mod { .open(path) .map_err(RE::from) .and_then(|mut file| { - let mut buffer = Vec::with_capacity(n); - let _ = file.read_exact(&mut buffer)?; + let mut buffer = vec![0; n]; + debug!("Allocated {} bytes", buffer.capacity()); + + match file.read_exact(&mut buffer) { + Ok(_) => { /* yay */ Ok(()) }, + Err(e) => if e.kind() == ::std::io::ErrorKind::UnexpectedEof { + debug!("Ignoring unexpected EOF before {} bytes were read", n); + Ok(()) + } else { + Err(e) + } + }?; + let buffer = String::from_utf8(buffer)?; $hashingimpl(buffer) })