From d76c40a43a85f3ff82bdbc05f577fe7236095939 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Mon, 21 Feb 2022 14:39:10 +0100 Subject: [PATCH] Ignore invalid gamma chunks --- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 3 ++- src/ImageSharp/Formats/Png/PngThrowHelper.cs | 3 --- tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs | 5 ++--- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 090e1e2b0..60437e015 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -432,7 +432,8 @@ namespace SixLabors.ImageSharp.Formats.Png { if (data.Length < 4) { - PngThrowHelper.ThrowInvalidGamma(); + // Ignore invalid gamma chunks. + return; } // For example, a gamma of 1/2.2 would be stored as 45455. diff --git a/src/ImageSharp/Formats/Png/PngThrowHelper.cs b/src/ImageSharp/Formats/Png/PngThrowHelper.cs index 07372dae2..ae7d16ec7 100644 --- a/src/ImageSharp/Formats/Png/PngThrowHelper.cs +++ b/src/ImageSharp/Formats/Png/PngThrowHelper.cs @@ -24,9 +24,6 @@ namespace SixLabors.ImageSharp.Formats.Png [MethodImpl(InliningOptions.ColdPath)] public static void ThrowMissingPalette() => throw new InvalidImageContentException("PNG Image does not contain a palette chunk"); - [MethodImpl(InliningOptions.ColdPath)] - public static void ThrowInvalidGamma() => throw new InvalidImageContentException("PNG Image does not contain enough data for the gamma chunk"); - [MethodImpl(InliningOptions.ColdPath)] public static void ThrowInvalidChunkType() => throw new InvalidImageContentException("Invalid PNG data."); diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs index 7fd87258b..0af5d9995 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs @@ -283,7 +283,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png [Theory] [WithFile(TestImages.Png.Bad.InvalidGammaChunk, PixelTypes.Rgba32)] - public void Decode_InvalidGammaChunk_ThrowsException(TestImageProvider provider) + public void Decode_InvalidGammaChunk_Ignored(TestImageProvider provider) where TPixel : unmanaged, IPixel { Exception ex = Record.Exception( @@ -292,8 +292,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png using Image image = provider.GetImage(PngDecoder); image.DebugSave(provider); }); - Assert.NotNull(ex); - Assert.Contains("PNG Image does not contain enough data for the gamma chunk", ex.Message); + Assert.Null(ex); } [Theory]