1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 08:22:59 +01:00

set length of vector to max_bytes (closes #345) (#346)

This commit is contained in:
ousado 2018-06-26 07:09:12 +02:00 committed by Douman
parent a9425a866b
commit 0f27389e72

View File

@ -440,11 +440,14 @@ impl Stream for ChunkedReadFile {
let max_bytes: usize; let max_bytes: usize;
max_bytes = cmp::min(size.saturating_sub(counter), 65_536) as usize; max_bytes = cmp::min(size.saturating_sub(counter), 65_536) as usize;
let mut buf = Vec::with_capacity(max_bytes); 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))?; file.seek(io::SeekFrom::Start(offset))?;
let nbytes = file.read(buf.as_mut_slice())?; let nbytes = file.read(buf.as_mut_slice())?;
if nbytes == 0 { if nbytes == 0 {
return Err(io::ErrorKind::UnexpectedEof.into()); return Err(io::ErrorKind::UnexpectedEof.into());
} }
unsafe { buf.set_len(nbytes); }
Ok((file, Bytes::from(buf))) Ok((file, Bytes::from(buf)))
})); }));
self.poll() self.poll()