Skip to content

Commit 91500ed

Browse files
committed
fix(bmp): detect corrupt files where palette doesn't match bpp (#5030)
Extra protections for corrupted BMP files that claim to be palette images, but have a BPP that doesn't support palette images. Also an extra guard around accessing the palette array if it is empty. Add an extra test case for this kind of corruption. Signed-off-by: Larry Gritz <lg@larrygritz.com>
1 parent 6752666 commit 91500ed

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

src/bmp.imageio/bmpinput.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,13 @@ BmpInput::open(const std::string& name, ImageSpec& newspec,
259259
case WINDOWS_V5: m_spec.attribute("bmp:version", 5); break;
260260
}
261261

262+
if (m_dib_header.cpalete && !m_colortable.size()) {
263+
errorfmt(
264+
"BMP error: bad BPP ({}) for palette image -- presumed corrupt file",
265+
m_dib_header.bpp);
266+
return false;
267+
}
268+
262269
// Default presumption is that a BMP file is meant to look reasonable on a
263270
// display, so assume it's sRGB. This is not really correct -- see the
264271
// comments below.
@@ -391,8 +398,9 @@ BmpInput::read_native_scanline(int subimage, int miplevel, int y, int /*z*/,
391398

392399
size_t scanline_bytes = m_spec.scanline_bytes();
393400
uint8_t* mscanline = (uint8_t*)data;
394-
if (m_dib_header.compression == RLE4_COMPRESSION
395-
|| m_dib_header.compression == RLE8_COMPRESSION) {
401+
if ((m_dib_header.compression == RLE4_COMPRESSION
402+
|| m_dib_header.compression == RLE8_COMPRESSION)
403+
&& m_colortable.size()) {
396404
for (int x = 0; x < m_spec.width; ++x) {
397405
int p = m_uncompressed[(m_spec.height - 1 - y) * m_spec.width + x];
398406
auto& c = colortable(p);

testsuite/bmp/ref/out.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,6 @@ oiiotool ERROR: read : "src/bad-y.bmp": BMP might be corrupted, it is referencin
298298
BMP error reading rle-compressed image
299299
Full command line was:
300300
> oiiotool --info -v -a --hash src/bad-y.bmp
301+
oiiotool ERROR: read : "src/palette32bit-corrupt.bmp": BMP error: bad BPP (32) for palette image -- presumed corrupt file
302+
Full command line was:
303+
> oiiotool --info -v -a --hash src/palette32bit-corrupt.bmp

testsuite/bmp/run.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@
3333
# See if we handle these corrupt files with useful error messages
3434
command += info_command ("src/decodecolormap-corrupt.bmp")
3535
command += info_command ("src/bad-y.bmp")
36+
command += info_command ("src/palette32bit-corrupt.bmp")
67 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)