From c39d3c30b75067a249a2b56bb8e914a023ae9668 Mon Sep 17 00:00:00 2001 From: Karol Kosek Date: Sat, 21 Jan 2023 00:55:56 +0100 Subject: [PATCH] LibGfx: Return stream errors when reading a marker in JPGLoader --- Userland/Libraries/LibGfx/JPGLoader.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/Userland/Libraries/LibGfx/JPGLoader.cpp b/Userland/Libraries/LibGfx/JPGLoader.cpp index 1bab4ff45e0..2247a655466 100644 --- a/Userland/Libraries/LibGfx/JPGLoader.cpp +++ b/Userland/Libraries/LibGfx/JPGLoader.cpp @@ -455,12 +455,9 @@ static inline ErrorOr read_be_word(InputMemoryStream& stream) return tmp; } -static inline Marker read_marker_at_cursor(InputMemoryStream& stream) +static inline ErrorOr read_marker_at_cursor(InputMemoryStream& stream) { - auto result = read_be_word(stream); - if (result.is_error()) - return JPG_INVALID; - u16 marker = result.release_value(); + auto marker = TRY(read_be_word(stream)); if (is_valid_marker(marker)) return marker; if (marker != 0xFFFF) @@ -468,7 +465,8 @@ static inline Marker read_marker_at_cursor(InputMemoryStream& stream) u8 next; do { stream >> next; - if (stream.handle_any_error() || next == 0x00) + TRY(stream.try_handle_any_error()); + if (next == 0x00) return JPG_INVALID; } while (next == 0xFF); marker = 0xFF00 | (u16)next; @@ -1030,15 +1028,13 @@ static ErrorOr compose_bitmap(JPGLoadingContext& context, Vector parse_header(InputMemoryStream& stream, JPGLoadingContext& context) { - auto marker = read_marker_at_cursor(stream); - TRY(stream.try_handle_any_error()); + auto marker = TRY(read_marker_at_cursor(stream)); if (marker != JPG_SOI) { dbgln_if(JPG_DEBUG, "{}: SOI not found: {:x}!", stream.offset(), marker); return Error::from_string_literal("SOI not found"); } for (;;) { - marker = read_marker_at_cursor(stream); - TRY(stream.try_handle_any_error()); + marker = TRY(read_marker_at_cursor(stream)); // Set frame type if the marker marks a new frame. if (marker >= 0xFFC0 && marker <= 0xFFCF) {