Skip to content

Commit ad25188

Browse files
Fix #2924
1 parent 5d8c199 commit ad25188

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

src/ImageSharp/Formats/Png/PngDecoderCore.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ internal sealed class PngDecoderCore : ImageDecoderCore
125125
/// </summary>
126126
private readonly int maxUncompressedLength;
127127

128+
/// <summary>
129+
/// A value indicating whether the image data has been read.
130+
/// </summary>
131+
private bool hasImageData;
132+
128133
/// <summary>
129134
/// Initializes a new instance of the <see cref="PngDecoderCore"/> class.
130135
/// </summary>
@@ -746,7 +751,11 @@ private void ReadScanlines<TPixel>(
746751
where TPixel : unmanaged, IPixel<TPixel>
747752
{
748753
using ZlibInflateStream inflateStream = new(this.currentStream, getData);
749-
inflateStream.AllocateNewBytes(chunkLength, true);
754+
if (!inflateStream.AllocateNewBytes(chunkLength, !this.hasImageData))
755+
{
756+
return;
757+
}
758+
750759
DeflateStream dataStream = inflateStream.CompressedStream!;
751760

752761
if (this.header.InterlaceMethod is PngInterlaceMode.Adam7)
@@ -800,7 +809,7 @@ private void DecodePixelData<TPixel>(
800809
int bytesRead = compressedStream.Read(scanSpan, currentRowBytesRead, bytesPerFrameScanline - currentRowBytesRead);
801810
if (bytesRead <= 0)
802811
{
803-
return;
812+
goto EXIT;
804813
}
805814

806815
currentRowBytesRead += bytesRead;
@@ -845,6 +854,7 @@ private void DecodePixelData<TPixel>(
845854
}
846855

847856
EXIT:
857+
this.hasImageData = true;
848858
blendMemory?.Dispose();
849859
}
850860

@@ -903,7 +913,7 @@ private void DecodeInterlacedPixelData<TPixel>(
903913
int bytesRead = compressedStream.Read(this.scanline.GetSpan(), currentRowBytesRead, bytesPerInterlaceScanline - currentRowBytesRead);
904914
if (bytesRead <= 0)
905915
{
906-
return;
916+
goto EXIT;
907917
}
908918

909919
currentRowBytesRead += bytesRead;
@@ -976,6 +986,7 @@ private void DecodeInterlacedPixelData<TPixel>(
976986
}
977987

978988
EXIT:
989+
this.hasImageData = true;
979990
blendMemory?.Dispose();
980991
}
981992

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,10 +705,20 @@ public void Decode_BadPalette(string file)
705705
[Theory]
706706
[WithFile(TestImages.Png.Issue2752, PixelTypes.Rgba32)]
707707
public void CanDecodeJustOneFrame<TPixel>(TestImageProvider<TPixel> provider)
708-
where TPixel : unmanaged, IPixel<TPixel>
708+
where TPixel : unmanaged, IPixel<TPixel>
709709
{
710710
DecoderOptions options = new() { MaxFrames = 1 };
711711
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance, options);
712712
Assert.Equal(1, image.Frames.Count);
713713
}
714+
715+
[Theory]
716+
[WithFile(TestImages.Png.Issue2924, PixelTypes.Rgba32)]
717+
public void CanDecode_Issue2924<TPixel>(TestImageProvider<TPixel> provider)
718+
where TPixel : unmanaged, IPixel<TPixel>
719+
{
720+
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance);
721+
image.DebugSave(provider);
722+
image.CompareToReferenceOutput(provider);
723+
}
714724
}

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ public static class Png
160160
// Issue 2752: https://github.com/SixLabors/ImageSharp/issues/2752
161161
public const string Issue2752 = "Png/issues/Issue_2752.png";
162162

163+
// Issue 2924: https://github.com/SixLabors/ImageSharp/issues/2924
164+
public const string Issue2924 = "Png/issues/Issue_2924.png";
165+
163166
public static class Bad
164167
{
165168
public const string MissingDataChunk = "Png/xdtn0g01.png";
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)