Prevent panics in parse_ihdr_header (#392)

This commit is contained in:
Kornel 2021-02-24 20:48:30 +00:00 committed by GitHub
parent f44b5aa2d0
commit f2e89140ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -140,6 +140,8 @@ pub fn parse_next_header<'a>(
}
pub fn parse_ihdr_header(byte_data: &[u8]) -> PngResult<IhdrData> {
// This eliminates bounds checks for the rest of the function
let interlaced = byte_data.get(12).copied().ok_or(PngError::TruncatedData)?;
let mut rdr = Cursor::new(&byte_data[0..8]);
Ok(IhdrData {
color_type: match byte_data[9] {
@ -162,6 +164,6 @@ pub fn parse_ihdr_header(byte_data: &[u8]) -> PngResult<IhdrData> {
height: rdr.read_u32::<BigEndian>().unwrap(),
compression: byte_data[10],
filter: byte_data[11],
interlaced: byte_data[12],
interlaced,
})
}