Browse Source

Ignore invalid gamma chunks

pull/2021/head
Brian Popow 4 years ago
parent
commit
d76c40a43a
  1. 3
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  2. 3
      src/ImageSharp/Formats/Png/PngThrowHelper.cs
  3. 5
      tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs

3
src/ImageSharp/Formats/Png/PngDecoderCore.cs

@ -432,7 +432,8 @@ namespace SixLabors.ImageSharp.Formats.Png
{ {
if (data.Length < 4) if (data.Length < 4)
{ {
PngThrowHelper.ThrowInvalidGamma(); // Ignore invalid gamma chunks.
return;
} }
// For example, a gamma of 1/2.2 would be stored as 45455. // For example, a gamma of 1/2.2 would be stored as 45455.

3
src/ImageSharp/Formats/Png/PngThrowHelper.cs

@ -24,9 +24,6 @@ namespace SixLabors.ImageSharp.Formats.Png
[MethodImpl(InliningOptions.ColdPath)] [MethodImpl(InliningOptions.ColdPath)]
public static void ThrowMissingPalette() => throw new InvalidImageContentException("PNG Image does not contain a palette chunk"); 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)] [MethodImpl(InliningOptions.ColdPath)]
public static void ThrowInvalidChunkType() => throw new InvalidImageContentException("Invalid PNG data."); public static void ThrowInvalidChunkType() => throw new InvalidImageContentException("Invalid PNG data.");

5
tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs

@ -283,7 +283,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
[Theory] [Theory]
[WithFile(TestImages.Png.Bad.InvalidGammaChunk, PixelTypes.Rgba32)] [WithFile(TestImages.Png.Bad.InvalidGammaChunk, PixelTypes.Rgba32)]
public void Decode_InvalidGammaChunk_ThrowsException<TPixel>(TestImageProvider<TPixel> provider) public void Decode_InvalidGammaChunk_Ignored<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
Exception ex = Record.Exception( Exception ex = Record.Exception(
@ -292,8 +292,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
using Image<TPixel> image = provider.GetImage(PngDecoder); using Image<TPixel> image = provider.GetImage(PngDecoder);
image.DebugSave(provider); image.DebugSave(provider);
}); });
Assert.NotNull(ex); Assert.Null(ex);
Assert.Contains("PNG Image does not contain enough data for the gamma chunk", ex.Message);
} }
[Theory] [Theory]

Loading…
Cancel
Save