Skip to content

Commit 57d8ffd

Browse files
committed
codecs/bmp: reject sub-byte indexed color to prevent out-of-bounds slice panic
1 parent 075e65d commit 57d8ffd

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

‎src/codecs/bmp/decoder.rs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,17 @@ impl<R: BufRead + Seek> BmpDecoder<R> {
18431843
}
18441844

18451845
fn read_palettized_pixel_data(&mut self, buf: &mut [u8]) -> ImageResult<()> {
1846+
if self.indexed_color && self.bit_count != 8 {
1847+
return Err(ImageError::Unsupported(
1848+
UnsupportedError::from_format_and_kind(
1849+
ImageFormat::Bmp.into(),
1850+
UnsupportedErrorKind::GenericFeature(
1851+
"Indexed color decoding is only supported for 8-bit palettized BMP".into(),
1852+
),
1853+
),
1854+
));
1855+
}
1856+
18461857
let num_channels = self.num_channels();
18471858
let row_byte_length = ((i32::from(self.bit_count) * self.width + 31) / 32 * 4) as usize;
18481859
let mut indices = vec![0; row_byte_length];
@@ -3167,4 +3178,25 @@ mod test {
31673178
"Expected error in strict mode for truncated file"
31683179
);
31693180
}
3181+
3182+
#[test]
3183+
fn test_indexed_color_sub_byte_rejected() {
3184+
use std::io::Cursor;
3185+
use crate::ImageDecoder;
3186+
3187+
// 1-bit BMP 2x2 image
3188+
let data = vec![
3189+
0x42, 0x4D, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00,
3190+
0x28, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00,
3191+
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3192+
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
3193+
0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
3194+
];
3195+
3196+
let mut decoder = BmpDecoder::new(Cursor::new(data)).unwrap();
3197+
decoder.set_indexed_color(true);
3198+
let mut buf = vec![0u8; 4];
3199+
let res = decoder.read_image(&mut buf);
3200+
assert!(res.is_err());
3201+
}
31703202
}

0 commit comments

Comments
 (0)