From f7f9f1f47fa385e8ec38d2183a050abfa8626c14 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Tue, 11 Jul 2023 08:52:41 -0400 Subject: [PATCH] LibGfx/PNG: Make invalid sRGB chunk size non-fatal https://www.haiku-os.org/images/bg-page.png has a size of 0 for example. Just ignoring the chunk instead of assuming that the image is sRGB and has Perceptual rendering intent matches what libpng does (cf `png_handle_sRGB()`), so let's do that too. (All other chunk handlers are still strict about size.) --- Userland/Libraries/LibGfx/ImageFormats/PNGLoader.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/PNGLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/PNGLoader.cpp index 039d944eab..3e546819ee 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PNGLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/PNGLoader.cpp @@ -1104,8 +1104,11 @@ static ErrorOr process_gAMA(ReadonlyBytes data, PNGLoadingContext& context static ErrorOr process_sRGB(ReadonlyBytes data, PNGLoadingContext& context) { // https://www.w3.org/TR/png/#srgb-standard-colour-space - if (data.size() != 1) - return Error::from_string_literal("sRGB chunk has an abnormal size"); + if (data.size() != 1) { + // Invalid per spec, but (rarely) happens in the wild. Log and ignore. + warnln("warning: PNG sRGB chunk has an abnormal size; ignoring"); + return {}; + } u8 rendering_intent = data[0]; if (rendering_intent > 3)