Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webp: Do not panic on overflow if size is maximum #1563

Merged
merged 1 commit into from
Oct 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/codecs/webp/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,17 @@ impl<R: Read> WebPDecoder<R> {
)));
}
_ => {
let mut len = self.r.read_u32::<LittleEndian>()?;
let mut len = u64::from(self.r.read_u32::<LittleEndian>()?);

if len % 2 != 0 {
// RIFF chunks containing an uneven number of bytes append
// an extra 0x00 at the end of the chunk
//
// The addition cannot overflow since we have a u64 that was created from a u32
len += 1;
}
io::copy(&mut self.r.by_ref().take(len as u64), &mut io::sink())?;

io::copy(&mut self.r.by_ref().take(len), &mut io::sink())?;
}
}
}
Expand Down Expand Up @@ -183,3 +187,23 @@ impl<'a, R: 'a + Read> ImageDecoder<'a> for WebPDecoder<R> {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn add_with_overflow_size() {
let bytes = vec![0x52, 0x49, 0x46, 0x46, 0xaf, 0x37, 0x80, 0x47, 0x57, 0x45, 0x42, 0x50,
0x6c, 0x64, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xfb, 0x7e, 0x73, 0x00,
0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x40, 0xfb, 0xff, 0xff, 0x65, 0x65,
0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x00, 0x00, 0x00, 0x00,
0x62, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x49, 0x49, 0x54,
0x55, 0x50, 0x4c, 0x54, 0x59, 0x50, 0x45, 0x33, 0x37, 0x44, 0x4d, 0x46];

let data = std::io::Cursor::new(bytes);

let _ = WebPDecoder::new(data);
}
}