diff --git a/src/isobmff.rs b/src/isobmff.rs index 8f927ff..75be945 100644 --- a/src/isobmff.rs +++ b/src/isobmff.rs @@ -442,6 +442,44 @@ mod tests { assert!(buf.ends_with(b"xif\0")); } + #[test] + fn unknown_before_ftyp() { + let data = + b"\0\0\0\x09XXXXx\ + \0\0\0\x14ftypmif1\0\0\0\0mif1\ + \0\0\0\x57meta\0\0\0\0\ + \0\0\0\x18iloc\x01\0\0\0\0\0\0\x01\x1e\x1d\0\x01\0\0\0\x01\ + \0\0\0\x22iinf\0\0\0\0\0\x01\ + \0\0\0\x14infe\x02\0\0\0\x1e\x1d\0\0Exif\ + \0\0\0\x11idat\0\0\0\x01xabcd"; + assert!(is_heif(data)); + let exif = get_exif_attr(&mut Cursor::new(&data[..])).unwrap(); + assert_eq!(exif, b"abcd"); + } + + #[test] + fn bad_exif_data_block() { + let data = + b"\0\0\0\x14ftypmif1\0\0\0\0mif1\ + \0\0\0\x52meta\0\0\0\0\ + \0\0\0\x18iloc\x01\0\0\0\0\0\0\x01\x1e\x1d\0\x01\0\0\0\x01\ + \0\0\0\x22iinf\0\0\0\0\0\x01\ + \0\0\0\x14infe\x02\0\0\0\x1e\x1d\0\0Exif\ + \0\0\0\x0cidat\0\0\0\x01"; + assert_err_pat!(get_exif_attr(&mut Cursor::new(&data[..])), + Error::InvalidFormat("Invalid Exif header offset")); + + let data = + b"\0\0\0\x14ftypmif1\0\0\0\0mif1\ + \0\0\0\x51meta\0\0\0\0\ + \0\0\0\x18iloc\x01\0\0\0\0\0\0\x01\x1e\x1d\0\x01\0\0\0\x01\ + \0\0\0\x22iinf\0\0\0\0\0\x01\ + \0\0\0\x14infe\x02\0\0\0\x1e\x1d\0\0Exif\ + \0\0\0\x0bidat\0\0\0"; + assert_err_pat!(get_exif_attr(&mut Cursor::new(&data[..])), + Error::InvalidFormat("ExifDataBlock too small")); + } + #[test] fn parser_box_header() { // size diff --git a/src/jpeg.rs b/src/jpeg.rs index 01fab5f..d5fc36e 100644 --- a/src/jpeg.rs +++ b/src/jpeg.rs @@ -125,6 +125,12 @@ mod tests { assert_err_pat!(get_exif_attr(&mut Cursor::new(data)), Error::InvalidFormat("Broken JPEG file")); } + + let mut data = b"\xff\xd8\xff\xe1\x00\x08Exif\0\0".to_vec(); + get_exif_attr(&mut Cursor::new(&data)).unwrap(); + while let Some(_) = data.pop() { + get_exif_attr(&mut &data[..]).unwrap_err(); + } } #[test] diff --git a/src/tiff.rs b/src/tiff.rs index 46ea9f6..69ab000 100644 --- a/src/tiff.rs +++ b/src/tiff.rs @@ -531,6 +531,17 @@ mod tests { assert_eq!(format!("{:^10}", In(65535)), " IFD65535 "); } + #[test] + fn truncated() { + let mut data = + b"MM\0\x2a\0\0\0\x08\ + \0\x01\x01\0\0\x03\0\0\0\x01\0\x14\0\0\0\0\0\0".to_vec(); + parse_exif(&data).unwrap(); + while let Some(_) = data.pop() { + parse_exif(&data).unwrap_err(); + } + } + // Before the error is returned, the IFD is parsed multiple times // as the 0th, 1st, ..., and n-th IFDs. #[test]