Truly independent web browser
LibImageDecoders: Tolerate bad plane counts and palette indices in BMPs
Andreas Kling291d4e550cf3
diff --git a/Libraries/LibImageDecoders/Rust/src/bmp.rs b/Libraries/LibImageDecoders/Rust/src/bmp.rsindex a8bc840c5c8..8cef89ba393 100644--- a/Libraries/LibImageDecoders/Rust/src/bmp.rs+++ b/Libraries/LibImageDecoders/Rust/src/bmp.rs@@ -247,9 +247,8 @@ fn decode_core_dib(reader: &mut Reader, dib: &mut DIBHeader, dib_type: DIBType)if dib.width < 0 || dib.height == i32::MIN {return None;}- if reader.read_u16()? != 1 {- return None;- }+ // INTEROP: The plane count should be 1, but is ignored.+ reader.skip(2)?;dib.bpp = reader.read_u16()?;match dib.bpp {1 | 2 | 4 | 8 | 16 | 24 | 32 => Some(()),@@ -263,9 +262,8 @@ fn decode_osv2_dib(reader: &mut Reader, dib: &mut DIBHeader, dib_type: DIBType)if dib.width < 0 || dib.height == i32::MIN {return None;}- if reader.read_u16()? != 1 {- return None;- }+ // INTEROP: The plane count should be 1, but is ignored.+ reader.skip(2)?;dib.bpp = reader.read_u16()?;// OS/2 did not expect 16 or 32 bpp to be popular.if !matches!(dib.bpp, 1 | 2 | 4 | 8 | 24) {@@ -769,7 +767,8 @@ impl BMPDecoder {(height - 1 - row) * stride}};- let palette_color = |index: u8| -> Option<u32> { color_table.get(usize::from(index)).copied() };+ // INTEROP: Indices past the end of the color table are opaque black.+ let palette_color = |index: u8| -> u32 { color_table.get(usize::from(index)).copied().unwrap_or(0xff00_0000) };let skip_row_padding = |reader: &mut Reader, consumed: usize| -> Option<()> {let remainder = consumed % 4;@@ -799,13 +798,13 @@ impl BMPDecoder {break;}let shift = 8 - bits_per_pixel as usize * (i + 1);- set_pixel(column, palette_color((byte >> shift) & index_mask)?);+ set_pixel(column, palette_color((byte >> shift) & index_mask));column += 1;}}8 => {let byte = reader.read_u8()?;- set_pixel(column, palette_color(byte)?);+ set_pixel(column, palette_color(byte));column += 1;}16 => {diff --git a/Tests/LibGfx/TestImageDecoder.cpp b/Tests/LibGfx/TestImageDecoder.cppindex c442d7f96ca..ec05f80a797 100644--- a/Tests/LibGfx/TestImageDecoder.cpp+++ b/Tests/LibGfx/TestImageDecoder.cpp@@ -383,6 +383,38 @@ TEST_CASE(test_bmp_v4_rgb16_with_empty_alpha_mask_is_opaque)EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::Red);}+TEST_CASE(test_bmp_palette_index_out_of_range_is_black)+{+ // 2x1 8-bit image with a one-color palette, whose second pixel uses an index past the end of the palette.+ Vector<u8> dib;+ append_bitmap_info_header(dib, 40, 2, 1, 8, 0 /* BI_RGB */, 1);+ append_u32(dib, 0xff0000); // Red.+ auto pixel_offset = dib.size();+ Array<u8, 4> pixels { 0, 5, 0, 0 };+ dib.append(pixels.data(), pixels.size());+ auto bmp_data = make_bmp_file(dib, pixel_offset);++ auto plugin_decoder = TRY_OR_FAIL(Gfx::BMPImageDecoderPlugin::create(bmp_data));+ auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 2, 1 }));+ EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::Red);+ EXPECT_EQ(frame.image->get_pixel(1, 0), Gfx::Color::NamedColor::Black);+}++TEST_CASE(test_bmp_ignores_plane_count)+{+ // 1x1 24-bit image whose plane count is 0 instead of 1.+ Vector<u8> dib;+ append_bitmap_info_header(dib, 40, 1, 1, 24, 0 /* BI_RGB */, 0);+ dib[12] = 0;+ auto pixel_offset = dib.size();+ append_u32(dib, 0xff0000); // Red pixel in BGR order, then row padding.+ auto bmp_data = make_bmp_file(dib, pixel_offset);++ auto plugin_decoder = TRY_OR_FAIL(Gfx::BMPImageDecoderPlugin::create(bmp_data));+ auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 1, 1 }));+ EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::Red);+}+TEST_CASE(test_bmp_rle8_skipped_pixels_are_transparent){// 4x1 BI_RLE8 image: one red pixel, a delta that skips two pixels, then one green pixel.