From da394abe04e56c3e5949e213b6c46ffd9434617e Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Sun, 7 May 2023 19:27:07 +0200 Subject: LibGfx+Fuzz: Convert ImageDecoder::initialize to ErrorOr This prevents callers from accidentally discarding the result of initialize(), which was the root cause of this OSS Fuzz bug: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=55896&q=label%3AProj-serenity&sort=summary --- Tests/LibGfx/TestICCProfile.cpp | 8 +++--- Tests/LibGfx/TestImageDecoder.cpp | 58 +++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 33 deletions(-) (limited to 'Tests') diff --git a/Tests/LibGfx/TestICCProfile.cpp b/Tests/LibGfx/TestICCProfile.cpp index 7707f0af46..dfd43bbaee 100644 --- a/Tests/LibGfx/TestICCProfile.cpp +++ b/Tests/LibGfx/TestICCProfile.cpp @@ -24,7 +24,7 @@ TEST_CASE(png) { auto file = MUST(Core::MappedFile::map(TEST_INPUT("icc-v2.png"sv))); auto png = MUST(Gfx::PNGImageDecoderPlugin::create(file->bytes())); - EXPECT(png->initialize()); + MUST(png->initialize()); auto icc_bytes = MUST(png->icc_data()); EXPECT(icc_bytes.has_value()); @@ -36,7 +36,7 @@ TEST_CASE(jpg) { auto file = MUST(Core::MappedFile::map(TEST_INPUT("icc-v4.jpg"sv))); auto jpg = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes())); - EXPECT(jpg->initialize()); + MUST(jpg->initialize()); auto icc_bytes = MUST(jpg->icc_data()); EXPECT(icc_bytes.has_value()); @@ -48,7 +48,7 @@ TEST_CASE(webp_extended_lossless) { auto file = MUST(Core::MappedFile::map(TEST_INPUT("extended-lossless.webp"sv))); auto webp = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes())); - EXPECT(webp->initialize()); + MUST(webp->initialize()); auto icc_bytes = MUST(webp->icc_data()); EXPECT(icc_bytes.has_value()); @@ -60,7 +60,7 @@ TEST_CASE(webp_extended_lossy) { auto file = MUST(Core::MappedFile::map(TEST_INPUT("extended-lossy.webp"sv))); auto webp = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes())); - EXPECT(webp->initialize()); + MUST(webp->initialize()); auto icc_bytes = MUST(webp->icc_data()); EXPECT(icc_bytes.has_value()); diff --git a/Tests/LibGfx/TestImageDecoder.cpp b/Tests/LibGfx/TestImageDecoder.cpp index b3b711885b..d8dd90da8d 100644 --- a/Tests/LibGfx/TestImageDecoder.cpp +++ b/Tests/LibGfx/TestImageDecoder.cpp @@ -33,7 +33,7 @@ TEST_CASE(test_bmp) auto file = MUST(Core::MappedFile::map(TEST_INPUT("rgba32-1.bmp"sv))); EXPECT(Gfx::BMPImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::BMPImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); EXPECT(plugin_decoder->frame_count()); EXPECT(!plugin_decoder->is_animated()); @@ -48,7 +48,7 @@ TEST_CASE(test_gif) auto file = MUST(Core::MappedFile::map(TEST_INPUT("download-animation.gif"sv))); EXPECT(Gfx::GIFImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::GIFImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); EXPECT(plugin_decoder->frame_count()); EXPECT(plugin_decoder->is_animated()); @@ -63,7 +63,7 @@ TEST_CASE(test_not_ico) auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie.png"sv))); EXPECT(!Gfx::ICOImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::ICOImageDecoderPlugin::create(file->bytes())); - EXPECT(!plugin_decoder->initialize()); + EXPECT(plugin_decoder->initialize().is_error()); EXPECT(plugin_decoder->frame_count()); EXPECT(!plugin_decoder->is_animated()); @@ -77,13 +77,13 @@ TEST_CASE(test_bmp_embedded_in_ico) auto file = MUST(Core::MappedFile::map(TEST_INPUT("serenity.ico"sv))); EXPECT(Gfx::ICOImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::ICOImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); EXPECT(plugin_decoder->frame_count()); EXPECT(!plugin_decoder->is_animated()); EXPECT(!plugin_decoder->loop_count()); - EXPECT(!plugin_decoder->frame(0).is_error()); + MUST(plugin_decoder->frame(0)); } TEST_CASE(test_jpeg_sof0_one_scan) @@ -91,7 +91,7 @@ TEST_CASE(test_jpeg_sof0_one_scan) auto file = MUST(Core::MappedFile::map(TEST_INPUT("rgb24.jpg"sv))); EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); EXPECT(plugin_decoder->frame_count()); EXPECT(!plugin_decoder->is_animated()); @@ -106,7 +106,7 @@ TEST_CASE(test_jpeg_sof0_several_scans) auto file = MUST(Core::MappedFile::map(TEST_INPUT("several_scans.jpg"sv))); EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); auto frame = MUST(plugin_decoder->frame(0)); EXPECT_EQ(frame.image->size(), Gfx::IntSize(592, 800)); @@ -117,7 +117,7 @@ TEST_CASE(test_jpeg_rgb_components) auto file = MUST(Core::MappedFile::map(TEST_INPUT("rgb_components.jpg"sv))); EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); auto frame = MUST(plugin_decoder->frame(0)); EXPECT_EQ(frame.image->size(), Gfx::IntSize(592, 800)); @@ -128,7 +128,7 @@ TEST_CASE(test_jpeg_sof2_spectral_selection) auto file = MUST(Core::MappedFile::map(TEST_INPUT("spectral_selection.jpg"sv))); EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); auto frame = MUST(plugin_decoder->frame(0)); EXPECT_EQ(frame.image->size(), Gfx::IntSize(592, 800)); @@ -139,7 +139,7 @@ TEST_CASE(test_jpeg_sof0_several_scans_odd_number_mcu) auto file = MUST(Core::MappedFile::map(TEST_INPUT("several_scans_odd_number_mcu.jpg"sv))); EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); auto frame = MUST(plugin_decoder->frame(0)); EXPECT_EQ(frame.image->size(), Gfx::IntSize(600, 600)); @@ -150,7 +150,7 @@ TEST_CASE(test_jpeg_sof2_successive_aproximation) auto file = MUST(Core::MappedFile::map(TEST_INPUT("successive_approximation.jpg"sv))); EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); auto frame = MUST(plugin_decoder->frame(0)); EXPECT_EQ(frame.image->size(), Gfx::IntSize(600, 800)); @@ -161,7 +161,7 @@ TEST_CASE(test_jpeg_sof1_12bits) auto file = MUST(Core::MappedFile::map(TEST_INPUT("12-bit.jpg"sv))); EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); auto frame = MUST(plugin_decoder->frame(0)); EXPECT_EQ(frame.image->size(), Gfx::IntSize(320, 240)); @@ -172,7 +172,7 @@ TEST_CASE(test_jpeg_sof2_12bits) auto file = MUST(Core::MappedFile::map(TEST_INPUT("12-bit-progressive.jpg"sv))); EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); auto frame = MUST(plugin_decoder->frame(0)); EXPECT_EQ(frame.image->size(), Gfx::IntSize(320, 240)); @@ -183,7 +183,7 @@ TEST_CASE(test_pbm) auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-raw.pbm"sv))); EXPECT(Gfx::PBMImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::PBMImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); EXPECT(plugin_decoder->frame_count()); EXPECT(!plugin_decoder->is_animated()); @@ -198,7 +198,7 @@ TEST_CASE(test_pgm) auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-raw.pgm"sv))); EXPECT(Gfx::PGMImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::PGMImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); EXPECT(plugin_decoder->frame_count()); EXPECT(!plugin_decoder->is_animated()); @@ -213,7 +213,7 @@ TEST_CASE(test_png) auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie.png"sv))); EXPECT(Gfx::PNGImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::PNGImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); EXPECT(plugin_decoder->frame_count()); EXPECT(!plugin_decoder->is_animated()); @@ -228,7 +228,7 @@ TEST_CASE(test_ppm) auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-raw.ppm"sv))); EXPECT(Gfx::PPMImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::PPMImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); EXPECT(plugin_decoder->frame_count()); EXPECT(!plugin_decoder->is_animated()); @@ -243,7 +243,7 @@ TEST_CASE(test_targa_bottom_left) auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-bottom-left-uncompressed.tga"sv))); EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes()))); auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); EXPECT_EQ(plugin_decoder->frame_count(), 1u); EXPECT(!plugin_decoder->is_animated()); @@ -258,7 +258,7 @@ TEST_CASE(test_targa_top_left) auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-top-left-uncompressed.tga"sv))); EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes()))); auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); EXPECT_EQ(plugin_decoder->frame_count(), 1u); EXPECT(!plugin_decoder->is_animated()); @@ -273,7 +273,7 @@ TEST_CASE(test_targa_bottom_left_compressed) auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-bottom-left-compressed.tga"sv))); EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes()))); auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); EXPECT_EQ(plugin_decoder->frame_count(), 1u); EXPECT(!plugin_decoder->is_animated()); @@ -288,7 +288,7 @@ TEST_CASE(test_targa_top_left_compressed) auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-top-left-compressed.tga"sv))); EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes()))); auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); EXPECT_EQ(plugin_decoder->frame_count(), 1u); EXPECT(!plugin_decoder->is_animated()); @@ -303,7 +303,7 @@ TEST_CASE(test_webp_simple_lossy) auto file = MUST(Core::MappedFile::map(TEST_INPUT("simple-vp8.webp"sv))); EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); EXPECT_EQ(plugin_decoder->frame_count(), 1u); EXPECT(!plugin_decoder->is_animated()); @@ -319,7 +319,7 @@ TEST_CASE(test_webp_simple_lossless) auto file = MUST(Core::MappedFile::map(TEST_INPUT("simple-vp8l.webp"sv))); EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); EXPECT_EQ(plugin_decoder->frame_count(), 1u); EXPECT(!plugin_decoder->is_animated()); @@ -345,7 +345,7 @@ TEST_CASE(test_webp_extended_lossy) auto file = MUST(Core::MappedFile::map(TEST_INPUT("extended-lossy.webp"sv))); EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); EXPECT_EQ(plugin_decoder->frame_count(), 1u); EXPECT(!plugin_decoder->is_animated()); @@ -361,7 +361,7 @@ TEST_CASE(test_webp_extended_lossless) auto file = MUST(Core::MappedFile::map(TEST_INPUT("extended-lossless.webp"sv))); EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); EXPECT_EQ(plugin_decoder->frame_count(), 1u); EXPECT(!plugin_decoder->is_animated()); @@ -392,7 +392,7 @@ TEST_CASE(test_webp_simple_lossless_color_index_transform) auto file = MUST(Core::MappedFile::map(TEST_INPUT("Qpalette.webp"sv))); EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); EXPECT_EQ(plugin_decoder->frame_count(), 1u); EXPECT(!plugin_decoder->is_animated()); @@ -429,7 +429,7 @@ TEST_CASE(test_webp_simple_lossless_color_index_transform_pixel_bundling) auto file = MUST(Core::MappedFile::map(MUST(String::formatted("{}{}", TEST_INPUT(""), test_case.file_name)))); EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); EXPECT_EQ(plugin_decoder->frame_count(), 1u); EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(32, 32)); @@ -457,7 +457,7 @@ TEST_CASE(test_webp_simple_lossless_color_index_transform_pixel_bundling_odd_wid for (auto file_name : file_names) { auto file = MUST(Core::MappedFile::map(MUST(String::formatted("{}{}", TEST_INPUT(""), file_name)))); auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); EXPECT_EQ(plugin_decoder->frame_count(), 1u); EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(11, 11)); @@ -472,7 +472,7 @@ TEST_CASE(test_webp_extended_lossless_animated) auto file = MUST(Core::MappedFile::map(TEST_INPUT("extended-lossless-animated.webp"sv))); EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes())); auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes())); - EXPECT(plugin_decoder->initialize()); + MUST(plugin_decoder->initialize()); EXPECT_EQ(plugin_decoder->frame_count(), 8u); EXPECT(plugin_decoder->is_animated()); -- cgit v1.2.3