From f2e89140ed26a26886d4dda26a9ab7c55f85a7b8 Mon Sep 17 00:00:00 2001 From: Kornel Date: Wed, 24 Feb 2021 20:48:30 +0000 Subject: [PATCH] Prevent panics in parse_ihdr_header (#392) --- src/headers.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/headers.rs b/src/headers.rs index 7a328e7b..049ec00c 100644 --- a/src/headers.rs +++ b/src/headers.rs @@ -140,6 +140,8 @@ pub fn parse_next_header<'a>( } pub fn parse_ihdr_header(byte_data: &[u8]) -> PngResult { + // 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 { height: rdr.read_u32::().unwrap(), compression: byte_data[10], filter: byte_data[11], - interlaced: byte_data[12], + interlaced, }) }