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

LibGfx/PortableFormat: Propagate errors from some read_* functions

These functions are:
 - read_width
 - read_height
 - read_max_val
This commit is contained in:
Lucas CHOLLET 2023-03-12 16:08:46 -04:00 committed by Andreas Kling
parent bab2113ec1
commit 05e6ed6ecb

View file

@ -135,46 +135,34 @@ static ErrorOr<void> read_whitespace(TContext& context, Streamer& streamer)
} }
template<typename TContext> template<typename TContext>
static bool read_width(TContext& context, Streamer& streamer) static ErrorOr<void> read_width(TContext& context, Streamer& streamer)
{ {
auto number_or_error = read_number(streamer); context.width = TRY(read_number(streamer));
if (number_or_error.is_error())
return false;
context.width = number_or_error.value();
context.state = TContext::State::Width; context.state = TContext::State::Width;
return true; return {};
} }
template<typename TContext> template<typename TContext>
static bool read_height(TContext& context, Streamer& streamer) static ErrorOr<void> read_height(TContext& context, Streamer& streamer)
{ {
auto number_or_error = read_number(streamer); context.height = TRY(read_number(streamer));
if (number_or_error.is_error())
return false;
context.height = number_or_error.value();
context.state = TContext::State::Height; context.state = TContext::State::Height;
return true; return {};
} }
template<typename TContext> template<typename TContext>
static bool read_max_val(TContext& context, Streamer& streamer) static ErrorOr<void> read_max_val(TContext& context, Streamer& streamer)
{ {
auto number_or_error = read_number(streamer); context.format_details.max_val = TRY(read_number(streamer));
if (number_or_error.is_error())
return false;
context.format_details.max_val = number_or_error.value();
if (context.format_details.max_val > 255) { if (context.format_details.max_val > 255) {
dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "We can't parse 2 byte color for {}", TContext::FormatDetails::image_type); dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "We can't parse 2 byte color for {}", TContext::FormatDetails::image_type);
context.state = TContext::State::Error; context.state = TContext::State::Error;
return false; return Error::from_string_literal("Can't parse 2 byte color");
} }
context.state = TContext::State::Maxval; context.state = TContext::State::Maxval;
return true; return {};
} }
template<typename TContext> template<typename TContext>
@ -219,13 +207,13 @@ static bool decode(TContext& context)
if (read_whitespace(context, streamer).is_error()) if (read_whitespace(context, streamer).is_error())
return false; return false;
if (!read_width(context, streamer)) if (read_width(context, streamer).is_error())
return false; return false;
if (read_whitespace(context, streamer).is_error()) if (read_whitespace(context, streamer).is_error())
return false; return false;
if (!read_height(context, streamer)) if (read_height(context, streamer).is_error())
return false; return false;
if (context.width > maximum_width_for_decoded_images || context.height > maximum_height_for_decoded_images) { if (context.width > maximum_width_for_decoded_images || context.height > maximum_height_for_decoded_images) {
@ -237,7 +225,7 @@ static bool decode(TContext& context)
return false; return false;
if constexpr (requires { context.format_details.max_val; }) { if constexpr (requires { context.format_details.max_val; }) {
if (!read_max_val(context, streamer)) if (read_max_val(context, streamer).is_error())
return false; return false;
if (read_whitespace(context, streamer).is_error()) if (read_whitespace(context, streamer).is_error())