1
0
Fork 0
mirror of https://github.com/alfg/mp4-rust.git synced 2024-06-02 13:39:54 +00:00

don't subtract overflow when 64-bit box size is less than 8

This commit is contained in:
Jessa 2023-01-08 13:59:07 -08:00
parent df6e0b5ea0
commit c4cc7023b1

View file

@ -245,7 +245,9 @@ impl BoxHeader {
Ok(BoxHeader {
name: BoxType::from(typ),
size: largesize - HEADER_SIZE,
size: largesize
.checked_sub(HEADER_SIZE)
.ok_or(Error::InvalidData("64-bit box size too small"))?,
})
} else {
Ok(BoxHeader {
@ -356,4 +358,22 @@ mod tests {
let ftyp_fcc2: u32 = ftyp_value.into();
assert_eq!(ftyp_fcc, ftyp_fcc2);
}
#[test]
fn test_largesize_too_small() {
let error = BoxHeader::read(&mut &[0, 0, 0, 1, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 7][..]);
assert!(matches!(error, Err(Error::InvalidData(_))));
}
#[test]
fn test_zero_largesize() {
let header = BoxHeader::read(&mut &[0, 0, 0, 1, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 8][..]);
assert!(matches!(header, Ok(BoxHeader { size: 0, .. })));
}
#[test]
fn test_nonzero_largesize() {
let header = BoxHeader::read(&mut &[0, 0, 0, 1, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 9][..]);
assert!(matches!(header, Ok(BoxHeader { size: 1, .. })));
}
}