diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index d8305a3f57..731cc9b1c1 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -15,6 +15,7 @@ using SixLabors.ImageSharp.Formats.Png.Filters; using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.Memory; +using SixLabors.ImageSharp.Memory.Internals; using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.Metadata.Profiles.Exif; using SixLabors.ImageSharp.Metadata.Profiles.Icc; @@ -1863,6 +1864,11 @@ private void SkipChunkDataAndCrc(in PngChunk chunk) [MethodImpl(InliningOptions.ShortMethod)] private IMemoryOwner ReadChunkData(int length) { + if (length == 0) + { + return new BasicArrayBuffer(Array.Empty()); + } + // We rent the buffer here to return it afterwards in Decode() IMemoryOwner buffer = this.configuration.MemoryAllocator.Allocate(length, AllocationOptions.Clean); diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs index 2e11093db6..85f7bf7495 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs @@ -636,4 +636,13 @@ static void RunTest(string providerDump, string nonContiguousBuffersStr) "Disco") .Dispose(); } + + [Fact] + public void Binary_PrematureEof() + { + using EofHitCounter eofHitCounter = EofHitCounter.RunDecoder(TestImages.Png.Bad.FlagOfGermany0000016446); + + Assert.True(eofHitCounter.EofHitCount <= 2); + Assert.Equal(new Size(200, 120), eofHitCounter.Image.Size); + } } diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 43506e4dfb..0d9fc55007 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -180,6 +180,8 @@ public static class Bad // Invalid color type. public const string ColorTypeOne = "Png/xc1n0g08.png"; public const string ColorTypeNine = "Png/xc9n2c08.png"; + + public const string FlagOfGermany0000016446 = "Png/issues/flag_of_germany-0000016446.png"; } } diff --git a/tests/Images/Input/Png/issues/flag_of_germany-0000016446.png b/tests/Images/Input/Png/issues/flag_of_germany-0000016446.png new file mode 100644 index 0000000000..cb8b5bc941 --- /dev/null +++ b/tests/Images/Input/Png/issues/flag_of_germany-0000016446.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:64633823874512ff9f37bd321184d548816e97b8898622ae7d912b59f3099a35 +size 16446