Skip to content

Truly independent web browser

LibGfx: Return an error instead of exiting on JPEG encode failure

Andreas Kling422e288cdb46
diff --git a/Libraries/LibGfx/ImageFormats/JPEGWriter.cpp b/Libraries/LibGfx/ImageFormats/JPEGWriter.cpp
index 1e81ac7ea8e..e633c976006 100644
--- a/Libraries/LibGfx/ImageFormats/JPEGWriter.cpp
+++ b/Libraries/LibGfx/ImageFormats/JPEGWriter.cpp
@@ -5,15 +5,21 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/ScopeGuard.h>
#include <AK/Stream.h>
#include <AK/Vector.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/CMYKBitmap.h>
#include <LibGfx/ImageFormats/JPEGWriter.h>
#include <jpeglib.h>
+#include <setjmp.h>
namespace Gfx {
+struct JPEGErrorManager : jpeg_error_mgr {
+ jmp_buf setjmp_buffer {};
+};
+
struct MemoryDestinationManager : public jpeg_destination_mgr {
Vector<u8>& buffer;
static constexpr size_t BUFFER_SIZE_INCREMENT = 65536;
@@ -49,53 +55,71 @@ struct MemoryDestinationManager : public jpeg_destination_mgr {
ErrorOr<void> JPEGWriter::encode_impl(Stream& stream, auto const& bitmap, Options const& options, ColorSpace color_space)
{
- struct jpeg_compress_struct cinfo {};
- struct jpeg_error_mgr jerr {};
-
- cinfo.err = jpeg_std_error(&jerr);
-
- jpeg_create_compress(&cinfo);
-
+ // NB: Everything with a destructor is constructed before setjmp(), so that a longjmp() from libjpeg does not
+ // skip any destructors. The guard comes last, so it destroys cinfo while the objects it points to still live.
+ // setjmp() is called in a separate function, as objects changed between setjmp() and longjmp() in the frame
+ // that calls setjmp() have indeterminate values after the longjmp().
Vector<u8> buffer;
MemoryDestinationManager dest_manager(buffer);
- cinfo.dest = &dest_manager;
-
- cinfo.image_width = bitmap.size().width();
- cinfo.image_height = bitmap.size().height();
- cinfo.input_components = 4;
-
- switch (color_space) {
- case ColorSpace::RGB:
- cinfo.in_color_space = JCS_EXT_BGRX;
- break;
- case ColorSpace::CMYK:
- cinfo.in_color_space = JCS_CMYK;
- break;
- default:
- VERIFY_NOT_REACHED();
- }
+ struct JPEGErrorManager jerr;
- jpeg_set_defaults(&cinfo);
- jpeg_set_colorspace(&cinfo, JCS_YCbCr);
- jpeg_set_quality(&cinfo, options.quality, TRUE);
-
- if (options.icc_data.has_value()) {
- jpeg_write_icc_profile(&cinfo, options.icc_data->data(), options.icc_data->size());
- }
-
- jpeg_start_compress(&cinfo, TRUE);
-
- Vector<JSAMPLE> row_buffer;
- row_buffer.resize(bitmap.size().width() * 4);
+ struct jpeg_compress_struct cinfo {};
+ ScopeGuard guard { [&]() { jpeg_destroy_compress(&cinfo); } };
- while (cinfo.next_scanline < cinfo.image_height) {
- auto const* row_ptr = reinterpret_cast<u8 const*>(bitmap.scanline(cinfo.next_scanline));
- JSAMPROW row_pointer = const_cast<JSAMPROW>(row_ptr);
- jpeg_write_scanlines(&cinfo, &row_pointer, 1);
- }
+ cinfo.err = jpeg_std_error(&jerr);
- jpeg_finish_compress(&cinfo);
- jpeg_destroy_compress(&cinfo);
+ jerr.error_exit = [](j_common_ptr cinfo) {
+ char buffer[JMSG_LENGTH_MAX];
+ (*cinfo->err->format_message)(cinfo, buffer);
+ dbgln("JPEG error: {}", buffer);
+ longjmp(static_cast<JPEGErrorManager*>(cinfo->err)->setjmp_buffer, 1);
+ };
+
+ auto compress = [&]() -> bool {
+ if (setjmp(jerr.setjmp_buffer))
+ return false;
+
+ jpeg_create_compress(&cinfo);
+
+ cinfo.dest = &dest_manager;
+
+ cinfo.image_width = bitmap.size().width();
+ cinfo.image_height = bitmap.size().height();
+ cinfo.input_components = 4;
+
+ switch (color_space) {
+ case ColorSpace::RGB:
+ cinfo.in_color_space = JCS_EXT_BGRX;
+ break;
+ case ColorSpace::CMYK:
+ cinfo.in_color_space = JCS_CMYK;
+ break;
+ default:
+ VERIFY_NOT_REACHED();
+ }
+
+ jpeg_set_defaults(&cinfo);
+ jpeg_set_colorspace(&cinfo, JCS_YCbCr);
+ jpeg_set_quality(&cinfo, options.quality, TRUE);
+
+ jpeg_start_compress(&cinfo, TRUE);
+
+ // NB: libjpeg only accepts markers after jpeg_start_compress() and before the first scanline.
+ if (options.icc_data.has_value())
+ jpeg_write_icc_profile(&cinfo, options.icc_data->data(), options.icc_data->size());
+
+ while (cinfo.next_scanline < cinfo.image_height) {
+ auto const* row_ptr = reinterpret_cast<u8 const*>(bitmap.scanline(cinfo.next_scanline));
+ JSAMPROW row_pointer = const_cast<JSAMPROW>(row_ptr);
+ jpeg_write_scanlines(&cinfo, &row_pointer, 1);
+ }
+
+ jpeg_finish_compress(&cinfo);
+ return true;
+ };
+
+ if (!compress())
+ return Error::from_string_literal("Failed to encode JPEG");
TRY(stream.write_until_depleted(buffer));
return {};
diff --git a/Tests/LibGfx/TestImageWriter.cpp b/Tests/LibGfx/TestImageWriter.cpp
index 596804b8a05..fdef26f93ec 100644
--- a/Tests/LibGfx/TestImageWriter.cpp
+++ b/Tests/LibGfx/TestImageWriter.cpp
@@ -108,6 +108,30 @@ TEST_CASE(test_jpeg)
(void)TRY_OR_FAIL((get_roundtrip_bitmap<Gfx::JPEGWriter, Gfx::JPEGImageDecoderPlugin>(TRY_OR_FAIL(create_test_rgb_bitmap()))));
}
+TEST_CASE(test_jpeg_icc_profile)
+{
+ // The profile is larger than one APP2 marker can hold, so libjpeg has to split it.
+ auto icc_data = TRY_OR_FAIL(ByteBuffer::create_uninitialized(70000));
+ for (size_t i = 0; i < icc_data.size(); ++i)
+ icc_data[i] = static_cast<u8>(i * 7);
+
+ auto bitmap = TRY_OR_FAIL(create_test_rgb_bitmap());
+ auto encoded_data = TRY_OR_FAIL(encode_bitmap<Gfx::JPEGWriter>(*bitmap, Gfx::JPEGWriter::Options { .icc_data = icc_data.bytes() }));
+
+ auto decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(encoded_data));
+ (void)TRY_OR_FAIL(expect_single_frame_of_size(*decoder, bitmap->size()));
+ auto decoded_icc_data = TRY_OR_FAIL(decoder->icc_data());
+ EXPECT(decoded_icc_data.has_value());
+ EXPECT_EQ(*decoded_icc_data, icc_data.bytes());
+}
+
+TEST_CASE(test_jpeg_too_wide)
+{
+ // libjpeg cannot encode an image with a side above 65500 pixels. This must fail without terminating the process.
+ auto bitmap = TRY_OR_FAIL(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { 65501, 1 }));
+ EXPECT(encode_bitmap<Gfx::JPEGWriter>(*bitmap).is_error());
+}
+
TEST_CASE(test_png)
{
TRY_OR_FAIL((test_roundtrip<Gfx::PNGWriter, Gfx::PNGImageDecoderPlugin>(TRY_OR_FAIL(create_test_rgb_bitmap()))));