Skip to content

Commit 168f3f5

Browse files
Merge pull request #2985 from mus65/backport_2926_2.1
Backport #2926 to release/2.1.x
2 parents cb115c2 + 85ce6b6 commit 168f3f5

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

src/ImageSharp/Formats/Png/PngDecoderCore.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ internal sealed class PngDecoderCore : IImageDecoderInternals
114114
/// </summary>
115115
private PngChunk? nextChunk;
116116

117+
/// <summary>
118+
/// A value indicating whether the image data has been read.
119+
/// </summary>
120+
private bool hasImageData;
121+
117122
/// <summary>
118123
/// Initializes a new instance of the <see cref="PngDecoderCore"/> class.
119124
/// </summary>
@@ -565,7 +570,11 @@ private void ReadScanlines<TPixel>(PngChunk chunk, ImageFrame<TPixel> image, Png
565570
where TPixel : unmanaged, IPixel<TPixel>
566571
{
567572
using var deframeStream = new ZlibInflateStream(this.currentStream, this.ReadNextDataChunk);
568-
deframeStream.AllocateNewBytes(chunk.Length, true);
573+
if (!deframeStream.AllocateNewBytes(chunk.Length, !this.hasImageData))
574+
{
575+
return;
576+
}
577+
569578
DeflateStream dataStream = deframeStream.CompressedStream;
570579

571580
if (this.header.InterlaceMethod == PngInterlaceMode.Adam7)
@@ -596,7 +605,7 @@ private void DecodePixelData<TPixel>(DeflateStream compressedStream, ImageFrame<
596605
int bytesRead = compressedStream.Read(scanlineSpan, this.currentRowBytesRead, this.bytesPerScanline - this.currentRowBytesRead);
597606
if (bytesRead <= 0)
598607
{
599-
return;
608+
goto EXIT;
600609
}
601610

602611
this.currentRowBytesRead += bytesRead;
@@ -635,6 +644,9 @@ private void DecodePixelData<TPixel>(DeflateStream compressedStream, ImageFrame<
635644
this.SwapScanlineBuffers();
636645
this.currentRow++;
637646
}
647+
648+
EXIT:
649+
this.hasImageData = true;
638650
}
639651

640652
/// <summary>
@@ -672,7 +684,7 @@ private void DecodeInterlacedPixelData<TPixel>(DeflateStream compressedStream, I
672684
int bytesRead = compressedStream.Read(this.scanline.GetSpan(), this.currentRowBytesRead, bytesPerInterlaceScanline - this.currentRowBytesRead);
673685
if (bytesRead <= 0)
674686
{
675-
return;
687+
goto EXIT;
676688
}
677689

678690
this.currentRowBytesRead += bytesRead;
@@ -729,6 +741,9 @@ private void DecodeInterlacedPixelData<TPixel>(DeflateStream compressedStream, I
729741
pass = 0;
730742
break;
731743
}
744+
745+
EXIT:
746+
this.hasImageData = true;
732747
}
733748
}
734749

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,5 +534,15 @@ public void Decode_BadPalette(string file)
534534
string path = Path.GetFullPath(Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, file));
535535
using Image image = Image.Load(path);
536536
}
537+
538+
[Theory]
539+
[WithFile(TestImages.Png.Issue2924, PixelTypes.Rgba32)]
540+
public void CanDecode_Issue2924<TPixel>(TestImageProvider<TPixel> provider)
541+
where TPixel : unmanaged, IPixel<TPixel>
542+
{
543+
using Image<TPixel> image = provider.GetImage(PngDecoder);
544+
image.DebugSave(provider);
545+
image.CompareToReferenceOutput(provider);
546+
}
537547
}
538548
}

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ public static class Png
125125
// Discussion 1875: https://github.com/SixLabors/ImageSharp/discussions/1875
126126
public const string Issue1875 = "Png/raw-profile-type-exif.png";
127127

128+
// Issue 2924: https://github.com/SixLabors/ImageSharp/issues/2924
129+
public const string Issue2924 = "Png/issues/Issue_2924.png";
130+
128131
public static class Bad
129132
{
130133
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)