Skip to content

Commit 020c776

Browse files
committed
core: Don't propagate error when decoding malformed PNG data, create a transparent bitmap with the same dimensions instead (fixes #18831)
1 parent 36a987e commit 020c776

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

render/src/utils.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::bitmap::{Bitmap, BitmapFormat};
22
use crate::error::Error;
3+
use png::DecodingError;
34
use std::borrow::Cow;
45
use std::io::Read;
56
use swf::Color;
@@ -379,7 +380,24 @@ fn decode_png(data: &[u8]) -> Result<Bitmap, Error> {
379380
let mut reader = decoder.read_info()?;
380381

381382
let mut data = vec![0; reader.output_buffer_size()];
382-
let info = reader.next_frame(&mut data)?;
383+
let info = match reader.next_frame(&mut data) {
384+
Ok(info) => info,
385+
Err(e) => {
386+
if let DecodingError::Format(_) = e {
387+
// PNG is malformed, however we don't want to panic here
388+
// we just return an empty bitmap.
389+
tracing::warn!("Malformed PNG: {:?}", e);
390+
return Ok(Bitmap::new(
391+
reader.info().width,
392+
reader.info().height,
393+
BitmapFormat::Rgba,
394+
vec![],
395+
));
396+
} else {
397+
return Err(Error::InvalidPng(e));
398+
}
399+
}
400+
};
383401

384402
let (format, data) = match info.color_type {
385403
ColorType::Rgb => (BitmapFormat::Rgb, data),

0 commit comments

Comments
 (0)