Skip to content

Commit 1ed88e4

Browse files
authored
tga: reject empty images (#2614)
1 parent 9ad4348 commit 1ed88e4

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/codecs/tga/decoder.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ impl<R: Read> TgaDecoder<R> {
8787
let raw_bytes_per_pixel = (header.pixel_depth as usize).div_ceil(8);
8888
let num_alpha_bits = header.image_desc & ALPHA_BIT_MASK;
8989

90+
if width == 0 || height == 0 {
91+
return Err(ImageError::Decoding(DecodingError::new(
92+
ImageFormat::Tga.into(),
93+
"Invalid empty image",
94+
)));
95+
}
96+
9097
// Validate header
9198
if ![8, 16, 24, 32].contains(&header.pixel_depth) || ![0, 8].contains(&num_alpha_bits) {
9299
return Err(ImageError::Unsupported(

src/codecs/tga/encoder.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ enum EncoderError {
1111

1212
/// Invalid TGA height.
1313
HeightInvalid(u32),
14+
15+
/// Empty
16+
Empty(u32, u32),
1417
}
1518

1619
impl fmt::Display for EncoderError {
1720
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1821
match self {
1922
EncoderError::WidthInvalid(s) => f.write_fmt(format_args!("Invalid TGA width: {s}")),
2023
EncoderError::HeightInvalid(s) => f.write_fmt(format_args!("Invalid TGA height: {s}")),
24+
EncoderError::Empty(w, h) => f.write_fmt(format_args!("Invalid TGA size: {w}x{h}")),
2125
}
2226
}
2327
}
@@ -173,6 +177,10 @@ impl<W: Write> TgaEncoder<W> {
173177
);
174178

175179
// Validate dimensions.
180+
if width == 0 || height == 0 {
181+
return Err(ImageError::from(EncoderError::Empty(width, height)));
182+
}
183+
176184
let width = u16::try_from(width)
177185
.map_err(|_| ImageError::from(EncoderError::WidthInvalid(width)))?;
178186

0 commit comments

Comments
 (0)