From 05e6ed6ecbbc535e90f631a7fdbbcd54abab4efe Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sun, 12 Mar 2023 16:08:46 -0400 Subject: [PATCH] LibGfx/PortableFormat: Propagate errors from some `read_*` functions These functions are: - read_width - read_height - read_max_val --- .../ImageFormats/PortableImageLoaderCommon.h | 38 +++++++------------ 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h b/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h index 0126543637..48951df26d 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h +++ b/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h @@ -135,46 +135,34 @@ static ErrorOr read_whitespace(TContext& context, Streamer& streamer) } template -static bool read_width(TContext& context, Streamer& streamer) +static ErrorOr read_width(TContext& context, Streamer& streamer) { - auto number_or_error = read_number(streamer); - if (number_or_error.is_error()) - return false; - - context.width = number_or_error.value(); + context.width = TRY(read_number(streamer)); context.state = TContext::State::Width; - return true; + return {}; } template -static bool read_height(TContext& context, Streamer& streamer) +static ErrorOr read_height(TContext& context, Streamer& streamer) { - auto number_or_error = read_number(streamer); - if (number_or_error.is_error()) - return false; - - context.height = number_or_error.value(); + context.height = TRY(read_number(streamer)); context.state = TContext::State::Height; - return true; + return {}; } template -static bool read_max_val(TContext& context, Streamer& streamer) +static ErrorOr read_max_val(TContext& context, Streamer& streamer) { - auto number_or_error = read_number(streamer); - if (number_or_error.is_error()) - return false; - - context.format_details.max_val = number_or_error.value(); + context.format_details.max_val = TRY(read_number(streamer)); 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); context.state = TContext::State::Error; - return false; + return Error::from_string_literal("Can't parse 2 byte color"); } context.state = TContext::State::Maxval; - return true; + return {}; } template @@ -219,13 +207,13 @@ static bool decode(TContext& context) if (read_whitespace(context, streamer).is_error()) return false; - if (!read_width(context, streamer)) + if (read_width(context, streamer).is_error()) return false; if (read_whitespace(context, streamer).is_error()) return false; - if (!read_height(context, streamer)) + if (read_height(context, streamer).is_error()) return false; 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; if constexpr (requires { context.format_details.max_val; }) { - if (!read_max_val(context, streamer)) + if (read_max_val(context, streamer).is_error()) return false; if (read_whitespace(context, streamer).is_error())