Fix loading bitmaps with malformed biClrUsed field

Stop throwing std::bad_alloc when BMP has large/negative biClrUsed by
checking that biClrUsed has a reasonable value before attempting to
allocate however much memory it indicates.

Add unit tests showing the loading such invalid bitmaps now correctly
returns an error rather than throwing an exception.

Closes https://github.com/wxWidgets/wxWidgets/pull/2583

Closes #19295.
This commit is contained in:
David Costanzo
2021-11-05 19:30:24 -07:00
committed by Vadim Zeitlin
parent 58d2243f7b
commit 20208cc81f
8 changed files with 44 additions and 4 deletions

View File

@@ -98,6 +98,7 @@ private:
CPPUNIT_TEST( BMPFlippingAndRLECompression );
CPPUNIT_TEST( ScaleCompare );
CPPUNIT_TEST( CreateBitmapFromCursor );
CPPUNIT_TEST( MalformedBMP );
CPPUNIT_TEST_SUITE_END();
void LoadFromSocketStream();
@@ -119,6 +120,7 @@ private:
void BMPFlippingAndRLECompression();
void ScaleCompare();
void CreateBitmapFromCursor();
void MalformedBMP();
wxDECLARE_NO_COPY_CLASS(ImageTestCase);
};
@@ -1520,6 +1522,25 @@ void ImageTestCase::CreateBitmapFromCursor()
#endif
}
// This function assumes that the file is malformed in a way that it cannot
// be loaded. If the file is malformed such that wxImage can salvage part
// of it, then CompareBMPImage should be called instead.
static void LoadMalformedBMP(const wxString& file)
{
wxImage image(file);
WX_ASSERT_MESSAGE
(
("wxImage::isOk() returned true after loading \"%s\"", file),
!image.IsOk()
);
}
void ImageTestCase::MalformedBMP()
{
LoadMalformedBMP("image/8bpp-colorsused-negative.bmp");
LoadMalformedBMP("image/8bpp-colorsused-large.bmp");
}
#endif //wxUSE_IMAGE
TEST_CASE("wxImage::Paste", "[image][paste]")