Browse Source

Merge pull request #2773 from SixLabors/js/v4-2769

V4 Correctly break during Png decoding
pull/2778/head
James Jackson-South 2 years ago
committed by GitHub
parent
commit
31a4e1031d
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 18
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  2. 4
      src/ImageSharp/Formats/Webp/WebpChunkParsingUtils.cs
  3. 10
      tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs
  4. 3
      tests/ImageSharp.Tests/TestImages.cs
  5. 3
      tests/Images/Input/Png/issues/Issue_2752.png

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

@ -210,18 +210,13 @@ internal sealed class PngDecoderCore : IImageDecoderInternals
break;
case PngChunkType.FrameControl:
frameCount++;
if (frameCount == this.maxFrames)
{
break;
}
currentFrame = null;
currentFrameControl = this.ReadFrameControlChunk(chunk.Data.GetSpan());
break;
case PngChunkType.FrameData:
if (frameCount == this.maxFrames)
if (frameCount >= this.maxFrames)
{
break;
goto EOF;
}
if (image is null)
@ -277,6 +272,11 @@ internal sealed class PngDecoderCore : IImageDecoderInternals
previousFrameControl = currentFrameControl;
}
if (frameCount >= this.maxFrames)
{
goto EOF;
}
break;
case PngChunkType.Palette:
this.palette = chunk.Data.GetSpan().ToArray();
@ -396,7 +396,7 @@ internal sealed class PngDecoderCore : IImageDecoderInternals
break;
case PngChunkType.FrameControl:
++frameCount;
if (frameCount == this.maxFrames)
if (frameCount >= this.maxFrames)
{
break;
}
@ -405,7 +405,7 @@ internal sealed class PngDecoderCore : IImageDecoderInternals
break;
case PngChunkType.FrameData:
if (frameCount == this.maxFrames)
if (frameCount >= this.maxFrames)
{
break;
}

4
src/ImageSharp/Formats/Webp/WebpChunkParsingUtils.cs

@ -218,10 +218,6 @@ internal static class WebpChunkParsingUtils
// 3 reserved bytes should follow which are supposed to be zero.
stream.Read(buffer, 0, 3);
if (buffer[0] != 0 || buffer[1] != 0 || buffer[2] != 0)
{
WebpThrowHelper.ThrowImageFormatException("reserved bytes should be zero");
}
// 3 bytes for the width.
uint width = ReadUInt24LittleEndian(stream, buffer) + 1;

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

@ -701,4 +701,14 @@ public partial class PngDecoderTests
string path = Path.GetFullPath(Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, file));
using Image image = Image.Load(path);
}
[Theory]
[WithFile(TestImages.Png.Issue2752, PixelTypes.Rgba32)]
public void CanDecodeJustOneFrame<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
DecoderOptions options = new() { MaxFrames = 1 };
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance, options);
Assert.Equal(1, image.Frames.Count);
}
}

3
tests/ImageSharp.Tests/TestImages.cs

@ -156,6 +156,9 @@ public static class TestImages
// Issue 2668: https://github.com/SixLabors/ImageSharp/issues/2668
public const string Issue2668 = "Png/issues/Issue_2668.png";
// Issue 2752: https://github.com/SixLabors/ImageSharp/issues/2752
public const string Issue2752 = "Png/issues/Issue_2752.png";
public static class Bad
{
public const string MissingDataChunk = "Png/xdtn0g01.png";

3
tests/Images/Input/Png/issues/Issue_2752.png

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:2d4cb44eea721009c616de30a1f18c1de59635de4b313b13d685456a529ced97
size 5590983
Loading…
Cancel
Save