Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly break during Png decoding #2769

Merged
merged 3 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading