Skip to content

Commit d7f7d5a

Browse files
Correctly break during decoding
1 parent b496109 commit d7f7d5a

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

src/ImageSharp/Formats/Png/PngDecoderCore.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,18 +204,13 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
204204
break;
205205
case PngChunkType.FrameControl:
206206
frameCount++;
207-
if (frameCount == this.maxFrames)
208-
{
209-
break;
210-
}
211-
212207
currentFrame = null;
213208
currentFrameControl = this.ReadFrameControlChunk(chunk.Data.GetSpan());
214209
break;
215210
case PngChunkType.FrameData:
216211
if (frameCount == this.maxFrames)
217212
{
218-
break;
213+
goto EOF;
219214
}
220215

221216
if (image is null)
@@ -271,6 +266,11 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
271266
previousFrameControl = currentFrameControl;
272267
}
273268

269+
if (frameCount == this.maxFrames)
270+
{
271+
goto EOF;
272+
}
273+
274274
break;
275275
case PngChunkType.Palette:
276276
this.palette = chunk.Data.GetSpan().ToArray();

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,4 +701,14 @@ public void Decode_BadPalette(string file)
701701
string path = Path.GetFullPath(Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, file));
702702
using Image image = Image.Load(path);
703703
}
704+
705+
[Theory]
706+
[WithFile(TestImages.Png.Issue2752, PixelTypes.Rgba32)]
707+
public void CanDecodeJustOneFrame<TPixel>(TestImageProvider<TPixel> provider)
708+
where TPixel : unmanaged, IPixel<TPixel>
709+
{
710+
DecoderOptions options = new() { MaxFrames = 1 };
711+
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance, options);
712+
Assert.Equal(1, image.Frames.Count);
713+
}
704714
}

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ public static class Png
156156
// Issue 2668: https://github.com/SixLabors/ImageSharp/issues/2668
157157
public const string Issue2668 = "Png/issues/Issue_2668.png";
158158

159+
// Issue 2752: https://github.com/SixLabors/ImageSharp/issues/2752
160+
public const string Issue2752 = "Png/issues/Issue_2752.png";
161+
159162
public static class Bad
160163
{
161164
public const string MissingDataChunk = "Png/xdtn0g01.png";
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)