summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGfx
diff options
context:
space:
mode:
authorLucas CHOLLET <lucas.chollet@free.fr>2023-03-12 22:48:24 -0400
committerAndreas Kling <kling@serenityos.org>2023-03-24 10:56:58 +0100
commit2356b48f1321d87d109f75085744367021318b6e (patch)
tree14265247da0c517562da62f75fdd5c0a34d431d6 /Userland/Libraries/LibGfx
parentf96b61fdd8ea859c9d9f84333f2e4fcc3b2c2e0e (diff)
downloadserenity-2356b48f1321d87d109f75085744367021318b6e.zip
LibGfx/PortableFormat: Propagate errors from `read_magic_number()`
Diffstat (limited to 'Userland/Libraries/LibGfx')
-rw-r--r--Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h27
1 files changed, 10 insertions, 17 deletions
diff --git a/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h b/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h
index cf655d123e..27c9f8d1db 100644
--- a/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h
+++ b/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h
@@ -72,40 +72,33 @@ static ErrorOr<void> read_comment(TContext& context)
}
template<typename TContext>
-static bool read_magic_number(TContext& context)
+static ErrorOr<void> read_magic_number(TContext& context)
{
- if (context.state >= TContext::State::MagicNumber) {
- return true;
- }
+ if (context.state >= TContext::State::MagicNumber)
+ return {};
- if (context.stream->size().release_value_but_fixme_should_propagate_errors() < 2) {
- context.state = TContext::State::Error;
+ if (TRY(context.stream->size()) < 2) {
dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "There is no enough data for {}", TContext::FormatDetails::image_type);
- return false;
+ return Error::from_string_literal("There is no enough data to read magic number.");
}
Array<u8, 2> magic_number {};
- if (context.stream->read_until_filled(Bytes { magic_number }).is_error()) {
- context.state = TContext::State::Error;
- dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "We can't read magic number for {}", TContext::FormatDetails::image_type);
- return false;
- }
+ TRY(context.stream->read_until_filled(Bytes { magic_number }));
if (magic_number[0] == 'P' && magic_number[1] == TContext::FormatDetails::ascii_magic_number) {
context.type = TContext::Type::ASCII;
context.state = TContext::State::MagicNumber;
- return true;
+ return {};
}
if (magic_number[0] == 'P' && magic_number[1] == TContext::FormatDetails::binary_magic_number) {
context.type = TContext::Type::RAWBITS;
context.state = TContext::State::MagicNumber;
- return true;
+ return {};
}
- context.state = TContext::State::Error;
dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "Magic number is not valid for {}{}{}", magic_number[0], magic_number[1], TContext::FormatDetails::image_type);
- return false;
+ return Error::from_string_literal("Unable to recognize magic bytes");
}
template<typename TContext>
@@ -205,7 +198,7 @@ static bool decode(TContext& context)
context.state = TContext::State::Error;
});
- if (!read_magic_number(context))
+ if (read_magic_number(context).is_error())
return false;
if (read_whitespace(context).is_error())