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

LibGfx/TGA: Make TGAImageDecoderPlugin::decode_tga_header() fallible

This commit is contained in:
Lucas CHOLLET 2023-07-09 00:57:20 -04:00 committed by Jelle Raaijmakers
parent e0ff3fbfad
commit a05516bb3e
2 changed files with 7 additions and 9 deletions

View file

@ -174,11 +174,11 @@ IntSize TGAImageDecoderPlugin::size()
return IntSize { m_context->header.width, m_context->header.height }; return IntSize { m_context->header.width, m_context->header.height };
} }
bool TGAImageDecoderPlugin::decode_tga_header() ErrorOr<void> TGAImageDecoderPlugin::decode_tga_header()
{ {
auto& reader = m_context->reader; auto& reader = m_context->reader;
if (reader->data().size() < sizeof(TGAHeader)) if (reader->data().size() < sizeof(TGAHeader))
return false; return Error::from_string_literal("Not enough data to be a TGA image");
m_context->header.id_length = reader->read_u8(); m_context->header.id_length = reader->read_u8();
m_context->header.color_map_type = reader->read_u8(); m_context->header.color_map_type = reader->read_u8();
@ -197,19 +197,17 @@ bool TGAImageDecoderPlugin::decode_tga_header()
// FIXME: Check for multiplication overflow! // FIXME: Check for multiplication overflow!
if (m_context->header.data_type_code == TGADataType::UncompressedRGB && bytes_remaining < static_cast<size_t>(m_context->header.width * m_context->header.height * (m_context->header.bits_per_pixel / 8))) if (m_context->header.data_type_code == TGADataType::UncompressedRGB && bytes_remaining < static_cast<size_t>(m_context->header.width * m_context->header.height * (m_context->header.bits_per_pixel / 8)))
return false; return Error::from_string_literal("Not enough data to read an image with the expected size");
if (m_context->header.bits_per_pixel < 8 || m_context->header.bits_per_pixel > 32) if (m_context->header.bits_per_pixel < 8 || m_context->header.bits_per_pixel > 32)
return false; return Error::from_string_literal("Invalid bit depth");
return true; return {};
} }
ErrorOr<void> TGAImageDecoderPlugin::initialize() ErrorOr<void> TGAImageDecoderPlugin::initialize()
{ {
if (decode_tga_header()) return decode_tga_header();
return {};
return Error::from_string_literal("Bad TGA header");
} }
ErrorOr<bool> TGAImageDecoderPlugin::validate_before_create(ReadonlyBytes data) ErrorOr<bool> TGAImageDecoderPlugin::validate_before_create(ReadonlyBytes data)

View file

@ -31,7 +31,7 @@ public:
virtual ErrorOr<Optional<ReadonlyBytes>> icc_data() override; virtual ErrorOr<Optional<ReadonlyBytes>> icc_data() override;
private: private:
bool decode_tga_header(); ErrorOr<void> decode_tga_header();
OwnPtr<TGALoadingContext> m_context; OwnPtr<TGALoadingContext> m_context;
}; };