Skip to content

Commit 9246ad6

Browse files
committed
Switch read_buf() from using read() to read_to_end()
Since read_to_end() takes a &mut Vec buf rather than a &mut slice, it can increase the size (i.e., length, not capacity) of the buf during operation. This removes the soundness requirement of pre-initializing the full capacity that occurred in allocate_read_buf(), thereby improving efficiency. As this was allocate_read_buf()'s only caller, remove it as well.
1 parent ba1b7c9 commit 9246ad6

File tree

1 file changed

+6
-20
lines changed

1 file changed

+6
-20
lines changed

mp4parse/src/lib.rs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,6 @@ pub fn vec_push<T>(vec: &mut Vec<T>, val: T) -> std::result::Result<(), ()> {
125125
Ok(())
126126
}
127127

128-
#[allow(unreachable_code)]
129-
fn allocate_read_buf(size: usize) -> std::result::Result<Vec<u8>, ()> {
130-
#[cfg(feature = "mp4parse_fallible")]
131-
{
132-
let mut buf: Vec<u8> = Vec::new();
133-
FallibleVec::try_reserve(&mut buf, size)?;
134-
buf.extend(std::iter::repeat(0).take(size));
135-
return Ok(buf);
136-
}
137-
138-
Ok(vec![0; size])
139-
}
140-
141128
fn vec_with_capacity<T>(capacity: usize) -> std::result::Result<Vec<T>, ()> {
142129
#[cfg(feature = "mp4parse_fallible")]
143130
{
@@ -3366,15 +3353,14 @@ fn read_buf<T: Read>(src: &mut T, size: u64) -> Result<Vec<u8>> {
33663353
if size > BUF_SIZE_LIMIT {
33673354
return Err(Error::InvalidData("read_buf size exceeds BUF_SIZE_LIMIT"));
33683355
}
3369-
if let Ok(mut buf) = allocate_read_buf(size.try_into()?) {
3370-
let r = src.read(&mut buf)?;
3371-
if r != size.try_into()? {
3372-
return Err(Error::InvalidData("failed buffer read"));
3373-
}
3374-
return Ok(buf);
3356+
3357+
let mut buf = vec![];
3358+
let r: u64 = read_to_end(&mut src.take(size), &mut buf)?.try_into()?;
3359+
if r != size {
3360+
return Err(Error::InvalidData("failed buffer read"));
33753361
}
33763362

3377-
Err(Error::OutOfMemory)
3363+
Ok(buf)
33783364
}
33793365

33803366
fn be_i16<T: ReadBytesExt>(src: &mut T) -> Result<i16> {

0 commit comments

Comments
 (0)