Userland: Return empty if ImageDecoder client receives an invalid frame

This simplifies error checking for all the users of the ImageDecoder
client.
This commit is contained in:
Tim Ledbetter 2023-10-01 19:56:19 +01:00 committed by Andreas Kling
parent e6c1429311
commit eaa6304aab
6 changed files with 11 additions and 15 deletions

View file

@ -246,7 +246,7 @@ ErrorOr<void> ViewWidget::try_open_file(String const& path, Core::File& file)
frames.ensure_capacity(decoded_image->frames.size());
for (u32 i = 0; i < decoded_image->frames.size(); i++) {
auto& frame_data = decoded_image->frames[i];
frames.unchecked_append({ BitmapImage::create(*frame_data.bitmap), int(frame_data.duration) });
frames.unchecked_append({ BitmapImage::create(frame_data.bitmap), int(frame_data.duration) });
}
}

View file

@ -66,10 +66,7 @@ ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::decode_bitmap(ReadonlyBytes bitmap_da
if (decoded_image.frames.is_empty())
return Error::from_string_literal("Image decode failed (no frames)");
auto decoded_bitmap = decoded_image.frames.first().bitmap;
if (decoded_bitmap.is_null())
return Error::from_string_literal("Image decode failed (no bitmap for frame)");
return decoded_bitmap.release_nonnull();
return decoded_image.frames.first().bitmap;
}
ErrorOr<NonnullRefPtr<Image>> Image::create_from_bitmap(NonnullRefPtr<Gfx::Bitmap> const& bitmap)

View file

@ -48,12 +48,13 @@ Optional<DecodedImage> Client::decode_image(ReadonlyBytes encoded_data, Optional
DecodedImage image;
image.is_animated = response.is_animated();
image.loop_count = response.loop_count();
image.frames.resize(response.bitmaps().size());
image.frames.ensure_capacity(response.bitmaps().size());
auto bitmaps = response.take_bitmaps();
for (size_t i = 0; i < image.frames.size(); ++i) {
auto& frame = image.frames[i];
frame.bitmap = bitmaps[i].bitmap();
frame.duration = response.durations()[i];
for (size_t i = 0; i < bitmaps.size(); ++i) {
if (!bitmaps[i].is_valid())
return {};
image.frames.empend(*bitmaps[i].bitmap(), response.durations()[i]);
}
return image;
}

View file

@ -14,7 +14,7 @@
namespace ImageDecoderClient {
struct Frame {
RefPtr<Gfx::Bitmap> bitmap;
NonnullRefPtr<Gfx::Bitmap> bitmap;
u32 duration { 0 };
};

View file

@ -31,9 +31,7 @@ Optional<Web::Platform::DecodedImage> ImageCodecPluginSerenity::decode_image(Rea
decoded_image.is_animated = result.is_animated;
decoded_image.loop_count = result.loop_count;
for (auto const& frame : result.frames) {
if (!frame.bitmap)
return {};
decoded_image.frames.empend(move(frame.bitmap), frame.duration);
decoded_image.frames.empend(frame.bitmap, frame.duration);
}
return decoded_image;

View file

@ -80,7 +80,7 @@ ErrorOr<NonnullRefPtr<Client>> Client::create(StringView image_path, StringView
if (!maybe_image.has_value())
return Error::from_string_view("Image could not be read"sv);
auto image = maybe_image->frames.take_first().bitmap.release_nonnull();
auto image = maybe_image->frames.take_first().bitmap;
// Make sure to not draw out of bounds; some servers will disconnect us for that!
if (image->width() > canvas_size.width()) {