Skip to content

Commit d001a98

Browse files
authored
Merge pull request #345 from mozilla/hdlr-name-multiple-nul
Check for multiple nul bytes in `hdlr` box `name` field
2 parents d039a74 + bc99c77 commit d001a98

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

mp4parse/src/lib.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4514,12 +4514,23 @@ fn read_hdlr<T: Read>(src: &mut BMFFBox<T>, strictness: ParseStrictness) -> Resu
45144514

45154515
match std::str::from_utf8(src.read_into_try_vec()?.as_slice()) {
45164516
Ok(name) => {
4517-
if name.bytes().last() != Some(b'\0') {
4518-
fail_if(
4517+
match name.bytes().filter(|&b| b == b'\0').count() {
4518+
0 => fail_if(
45194519
strictness != ParseStrictness::Permissive,
45204520
"The HandlerBox 'name' field shall be null-terminated \
45214521
per ISOBMFF (ISO 14496-12:2020) § 8.4.3.2",
4522-
)?
4522+
)?,
4523+
1 => (),
4524+
_ =>
4525+
// See https://github.com/MPEGGroup/FileFormat/issues/35
4526+
{
4527+
fail_if(
4528+
strictness == ParseStrictness::Strict,
4529+
"The HandlerBox 'name' field shall have a NUL byte \
4530+
only in the final position \
4531+
per ISOBMFF (ISO 14496-12:2020) § 8.4.3.2",
4532+
)?
4533+
}
45234534
}
45244535
}
45254536
Err(_) => fail_if(

mp4parse/src/tests.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,24 @@ fn read_hdlr() {
475475
assert_eq!(parsed.handler_type, b"vide");
476476
}
477477

478+
#[test]
479+
fn read_hdlr_multiple_nul_in_name() {
480+
let mut stream = make_fullbox(BoxSize::Short(45), b"hdlr", 0, |s| {
481+
s.B32(0)
482+
.append_bytes(b"vide")
483+
.B32(0)
484+
.B32(0)
485+
.B32(0)
486+
.append_bytes(b"Vide\0Handler")
487+
.B8(0) // null-terminate string
488+
});
489+
let mut iter = super::BoxIter::new(&mut stream);
490+
let mut stream = iter.next_box().unwrap().unwrap();
491+
assert_eq!(stream.head.name, BoxType::HandlerBox);
492+
assert_eq!(stream.head.size, 45);
493+
assert!(super::read_hdlr(&mut stream, ParseStrictness::Strict).is_err());
494+
}
495+
478496
#[test]
479497
fn read_hdlr_short_name() {
480498
let mut stream = make_fullbox(BoxSize::Short(33), b"hdlr", 0, |s| {

0 commit comments

Comments
 (0)