Skip to content

Commit df2f698

Browse files
Do not attempt to read data for chunks of length 0.
1 parent d93bc6c commit df2f698

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

src/ImageSharp/Formats/Png/PngDecoderCore.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using SixLabors.ImageSharp.Formats.Png.Filters;
1616
using SixLabors.ImageSharp.IO;
1717
using SixLabors.ImageSharp.Memory;
18+
using SixLabors.ImageSharp.Memory.Internals;
1819
using SixLabors.ImageSharp.Metadata;
1920
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
2021
using SixLabors.ImageSharp.Metadata.Profiles.Icc;
@@ -1569,6 +1570,11 @@ private void SkipChunkDataAndCrc(in PngChunk chunk)
15691570
[MethodImpl(InliningOptions.ShortMethod)]
15701571
private IMemoryOwner<byte> ReadChunkData(int length)
15711572
{
1573+
if (length == 0)
1574+
{
1575+
return new BasicArrayBuffer<byte>(Array.Empty<byte>());
1576+
}
1577+
15721578
// We rent the buffer here to return it afterwards in Decode()
15731579
IMemoryOwner<byte> buffer = this.configuration.MemoryAllocator.Allocate<byte>(length, AllocationOptions.Clean);
15741580

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using SixLabors.ImageSharp.Formats.Png;
88
using SixLabors.ImageSharp.Memory;
99
using SixLabors.ImageSharp.PixelFormats;
10-
using SixLabors.ImageSharp.Processing.Processors.Quantization;
1110
using SixLabors.ImageSharp.Tests.TestUtilities;
1211
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
1312
using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs;
@@ -612,4 +611,13 @@ static void RunTest(string providerDump, string nonContiguousBuffersStr)
612611
"Disco")
613612
.Dispose();
614613
}
614+
615+
[Fact]
616+
public void Binary_PrematureEof()
617+
{
618+
using EofHitCounter eofHitCounter = EofHitCounter.RunDecoder(TestImages.Png.Bad.FlagOfGermany0000016446);
619+
620+
Assert.True(eofHitCounter.EofHitCount <= 2);
621+
Assert.Equal(new Size(200, 120), eofHitCounter.Image.Size);
622+
}
615623
}

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ public static class Bad
169169
// Invalid color type.
170170
public const string ColorTypeOne = "Png/xc1n0g08.png";
171171
public const string ColorTypeNine = "Png/xc9n2c08.png";
172+
173+
public const string FlagOfGermany0000016446 = "Png/issues/flag_of_germany-0000016446.png";
172174
}
173175
}
174176

Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)