From 0f27389e7207fb286ac47cd81f7b6a5e0622cf29 Mon Sep 17 00:00:00 2001 From: ousado Date: Tue, 26 Jun 2018 07:09:12 +0200 Subject: [PATCH] set length of vector to max_bytes (closes #345) (#346) --- src/fs.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/fs.rs b/src/fs.rs index 574f2cff8..7ac0effae 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -440,11 +440,14 @@ impl Stream for ChunkedReadFile { let max_bytes: usize; max_bytes = cmp::min(size.saturating_sub(counter), 65_536) as usize; let mut buf = Vec::with_capacity(max_bytes); + // safe because memory is initialized/overwritten immediately + unsafe { buf.set_len(max_bytes); } file.seek(io::SeekFrom::Start(offset))?; let nbytes = file.read(buf.as_mut_slice())?; if nbytes == 0 { return Err(io::ErrorKind::UnexpectedEof.into()); } + unsafe { buf.set_len(nbytes); } Ok((file, Bytes::from(buf))) })); self.poll()