Skip to content

Commit

Permalink
Merge pull request #2769 from SixLabors/js/fix-2752
Browse files Browse the repository at this point in the history
Correctly break during Png decoding
  • Loading branch information
JimBobSquarePants authored Jul 16, 2024
2 parents 1d4a287 + 8adcabe commit d7ef0e2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/ImageSharp/Formats/Png/PngDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,13 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
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)
Expand Down Expand Up @@ -271,6 +266,11 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
previousFrameControl = currentFrameControl;
}

if (frameCount >= this.maxFrames)
{
goto EOF;
}

break;
case PngChunkType.Palette:
this.palette = chunk.Data.GetSpan().ToArray();
Expand Down Expand Up @@ -389,15 +389,15 @@ public ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellat
break;
case PngChunkType.FrameControl:
++frameCount;
if (frameCount == this.maxFrames)
if (frameCount >= this.maxFrames)
{
break;
}

lastFrameControl = this.ReadFrameControlChunk(chunk.Data.GetSpan());
break;
case PngChunkType.FrameData:
if (frameCount == this.maxFrames)
if (frameCount >= this.maxFrames)
{
break;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -701,4 +701,14 @@ public void Decode_BadPalette(string file)
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 changes: 3 additions & 0 deletions tests/ImageSharp.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ public static class Png
// 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";
Expand Down
3 changes: 3 additions & 0 deletions tests/Images/Input/Png/issues/Issue_2752.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d7ef0e2

Please sign in to comment.