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

LibGfx/PortableFormat: Propagate errors from read_magic_number()

This commit is contained in:
Lucas CHOLLET 2023-03-12 22:48:24 -04:00 committed by Andreas Kling
parent f96b61fdd8
commit 2356b48f13

View file

@ -72,40 +72,33 @@ static ErrorOr<void> read_comment(TContext& context)
} }
template<typename TContext> template<typename TContext>
static bool read_magic_number(TContext& context) static ErrorOr<void> read_magic_number(TContext& context)
{ {
if (context.state >= TContext::State::MagicNumber) { if (context.state >= TContext::State::MagicNumber)
return true; return {};
}
if (context.stream->size().release_value_but_fixme_should_propagate_errors() < 2) { if (TRY(context.stream->size()) < 2) {
context.state = TContext::State::Error;
dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "There is no enough data for {}", TContext::FormatDetails::image_type); 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 {}; Array<u8, 2> magic_number {};
if (context.stream->read_until_filled(Bytes { magic_number }).is_error()) { TRY(context.stream->read_until_filled(Bytes { magic_number }));
context.state = TContext::State::Error;
dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "We can't read magic number for {}", TContext::FormatDetails::image_type);
return false;
}
if (magic_number[0] == 'P' && magic_number[1] == TContext::FormatDetails::ascii_magic_number) { if (magic_number[0] == 'P' && magic_number[1] == TContext::FormatDetails::ascii_magic_number) {
context.type = TContext::Type::ASCII; context.type = TContext::Type::ASCII;
context.state = TContext::State::MagicNumber; context.state = TContext::State::MagicNumber;
return true; return {};
} }
if (magic_number[0] == 'P' && magic_number[1] == TContext::FormatDetails::binary_magic_number) { if (magic_number[0] == 'P' && magic_number[1] == TContext::FormatDetails::binary_magic_number) {
context.type = TContext::Type::RAWBITS; context.type = TContext::Type::RAWBITS;
context.state = TContext::State::MagicNumber; 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); 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> template<typename TContext>
@ -205,7 +198,7 @@ static bool decode(TContext& context)
context.state = TContext::State::Error; context.state = TContext::State::Error;
}); });
if (!read_magic_number(context)) if (read_magic_number(context).is_error())
return false; return false;
if (read_whitespace(context).is_error()) if (read_whitespace(context).is_error())