From 6ea3c1659ac8faa584c476a7aa880fd7c8678077 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Wed, 8 Feb 2023 18:21:41 +0100 Subject: [PATCH] LibGfx: Propagate errors from serializing bitmaps We essentially just end up moving `release_value_but_fixme_...` one layer down, but it makes adding more fallible operations in the serialization function more comfortable. --- Userland/Libraries/LibGUI/DragOperation.cpp | 2 +- Userland/Libraries/LibGUI/Model.cpp | 2 +- Userland/Libraries/LibGfx/Bitmap.cpp | 11 +++++------ Userland/Libraries/LibGfx/Bitmap.h | 2 +- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/Userland/Libraries/LibGUI/DragOperation.cpp b/Userland/Libraries/LibGUI/DragOperation.cpp index 3e64f09375..5f1c28b8ad 100644 --- a/Userland/Libraries/LibGUI/DragOperation.cpp +++ b/Userland/Libraries/LibGUI/DragOperation.cpp @@ -84,7 +84,7 @@ void DragOperation::set_bitmap(Gfx::Bitmap const* bitmap) if (!m_mime_data) m_mime_data = Core::MimeData::construct(); if (bitmap) - m_mime_data->set_data("image/x-raw-bitmap", bitmap->serialize_to_byte_buffer()); + m_mime_data->set_data("image/x-raw-bitmap", bitmap->serialize_to_byte_buffer().release_value_but_fixme_should_propagate_errors()); } void DragOperation::set_data(DeprecatedString const& data_type, DeprecatedString const& data) { diff --git a/Userland/Libraries/LibGUI/Model.cpp b/Userland/Libraries/LibGUI/Model.cpp index af17ebc9ec..401c76b98c 100644 --- a/Userland/Libraries/LibGUI/Model.cpp +++ b/Userland/Libraries/LibGUI/Model.cpp @@ -128,7 +128,7 @@ RefPtr Model::mime_data(ModelSelection const& selection) const mime_data->set_data(drag_data_type(), data_builder.to_byte_buffer()); mime_data->set_text(text_builder.to_deprecated_string()); if (bitmap) - mime_data->set_data("image/x-raw-bitmap", bitmap->serialize_to_byte_buffer()); + mime_data->set_data("image/x-raw-bitmap", bitmap->serialize_to_byte_buffer().release_value_but_fixme_should_propagate_errors()); return mime_data; } diff --git a/Userland/Libraries/LibGfx/Bitmap.cpp b/Userland/Libraries/LibGfx/Bitmap.cpp index f8bd838b22..c716455249 100644 --- a/Userland/Libraries/LibGfx/Bitmap.cpp +++ b/Userland/Libraries/LibGfx/Bitmap.cpp @@ -256,10 +256,9 @@ ErrorOr> Bitmap::create_from_serialized_bytes(ReadonlyByte return bitmap; } -ByteBuffer Bitmap::serialize_to_byte_buffer() const +ErrorOr Bitmap::serialize_to_byte_buffer() const { - // FIXME: Somehow handle possible OOM situation here. - auto buffer = ByteBuffer::create_uninitialized(sizeof(size_t) + 4 * sizeof(unsigned) + sizeof(BitmapFormat) + sizeof(ARGB32) * palette_size(m_format) + size_in_bytes()).release_value_but_fixme_should_propagate_errors(); + auto buffer = TRY(ByteBuffer::create_uninitialized(sizeof(size_t) + 4 * sizeof(unsigned) + sizeof(BitmapFormat) + sizeof(ARGB32) * palette_size(m_format) + size_in_bytes())); DeprecatedOutputMemoryStream stream { buffer }; auto write = [&](T value) { @@ -271,17 +270,17 @@ ByteBuffer Bitmap::serialize_to_byte_buffer() const auto palette = palette_to_vector(); if (!write(size_in_bytes()) || !write((unsigned)size().width()) || !write((unsigned)size().height()) || !write((unsigned)scale()) || !write(m_format) || !write((unsigned)palette.size())) - return {}; + return Error::from_string_literal("Failed to write serialized image metadata to the buffer"); for (auto& p : palette) { if (!write(p)) - return {}; + return Error::from_string_literal("Failed to write serialized palette to the buffer"); } auto size = size_in_bytes(); VERIFY(stream.remaining() == size); if (stream.write({ scanline(0), size }) != size) - return {}; + return Error::from_string_literal("Failed to write serialized image data to the buffer"); return buffer; } diff --git a/Userland/Libraries/LibGfx/Bitmap.h b/Userland/Libraries/LibGfx/Bitmap.h index 6d098a0cbf..68d04f95b5 100644 --- a/Userland/Libraries/LibGfx/Bitmap.h +++ b/Userland/Libraries/LibGfx/Bitmap.h @@ -121,7 +121,7 @@ public: ErrorOr> scaled(float sx, float sy) const; ErrorOr> cropped(Gfx::IntRect, Optional new_bitmap_format = {}) const; ErrorOr> to_bitmap_backed_by_anonymous_buffer() const; - [[nodiscard]] ByteBuffer serialize_to_byte_buffer() const; + [[nodiscard]] ErrorOr serialize_to_byte_buffer() const; [[nodiscard]] ShareableBitmap to_shareable_bitmap() const;