1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:47:35 +00:00

LibGfx: Return stream errors when reading a marker in JPGLoader

This commit is contained in:
Karol Kosek 2023-01-21 00:55:56 +01:00 committed by Andreas Kling
parent a9eea2e0c4
commit c39d3c30b7

View file

@ -455,12 +455,9 @@ static inline ErrorOr<u16> read_be_word(InputMemoryStream& stream)
return tmp; return tmp;
} }
static inline Marker read_marker_at_cursor(InputMemoryStream& stream) static inline ErrorOr<Marker> read_marker_at_cursor(InputMemoryStream& stream)
{ {
auto result = read_be_word(stream); auto marker = TRY(read_be_word(stream));
if (result.is_error())
return JPG_INVALID;
u16 marker = result.release_value();
if (is_valid_marker(marker)) if (is_valid_marker(marker))
return marker; return marker;
if (marker != 0xFFFF) if (marker != 0xFFFF)
@ -468,7 +465,8 @@ static inline Marker read_marker_at_cursor(InputMemoryStream& stream)
u8 next; u8 next;
do { do {
stream >> next; stream >> next;
if (stream.handle_any_error() || next == 0x00) TRY(stream.try_handle_any_error());
if (next == 0x00)
return JPG_INVALID; return JPG_INVALID;
} while (next == 0xFF); } while (next == 0xFF);
marker = 0xFF00 | (u16)next; marker = 0xFF00 | (u16)next;
@ -1030,15 +1028,13 @@ static ErrorOr<void> compose_bitmap(JPGLoadingContext& context, Vector<Macrobloc
static ErrorOr<void> parse_header(InputMemoryStream& stream, JPGLoadingContext& context) static ErrorOr<void> parse_header(InputMemoryStream& stream, JPGLoadingContext& context)
{ {
auto marker = read_marker_at_cursor(stream); auto marker = TRY(read_marker_at_cursor(stream));
TRY(stream.try_handle_any_error());
if (marker != JPG_SOI) { if (marker != JPG_SOI) {
dbgln_if(JPG_DEBUG, "{}: SOI not found: {:x}!", stream.offset(), marker); dbgln_if(JPG_DEBUG, "{}: SOI not found: {:x}!", stream.offset(), marker);
return Error::from_string_literal("SOI not found"); return Error::from_string_literal("SOI not found");
} }
for (;;) { for (;;) {
marker = read_marker_at_cursor(stream); marker = TRY(read_marker_at_cursor(stream));
TRY(stream.try_handle_any_error());
// Set frame type if the marker marks a new frame. // Set frame type if the marker marks a new frame.
if (marker >= 0xFFC0 && marker <= 0xFFCF) { if (marker >= 0xFFC0 && marker <= 0xFFCF) {