Skip to content

Commit 6cf09dc

Browse files
Merge pull request #2561 from SixLabors/js/faster-png-eof-handling
Png- Do not attempt to read data for chunks of length 0.
2 parents 2a572d1 + 2927fcc commit 6cf09dc

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
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;
@@ -1863,6 +1864,11 @@ private void SkipChunkDataAndCrc(in PngChunk chunk)
18631864
[MethodImpl(InliningOptions.ShortMethod)]
18641865
private IMemoryOwner<byte> ReadChunkData(int length)
18651866
{
1867+
if (length == 0)
1868+
{
1869+
return new BasicArrayBuffer<byte>(Array.Empty<byte>());
1870+
}
1871+
18661872
// We rent the buffer here to return it afterwards in Decode()
18671873
IMemoryOwner<byte> buffer = this.configuration.MemoryAllocator.Allocate<byte>(length, AllocationOptions.Clean);
18681874

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,4 +636,13 @@ static void RunTest(string providerDump, string nonContiguousBuffersStr)
636636
"Disco")
637637
.Dispose();
638638
}
639+
640+
[Fact]
641+
public void Binary_PrematureEof()
642+
{
643+
using EofHitCounter eofHitCounter = EofHitCounter.RunDecoder(TestImages.Png.Bad.FlagOfGermany0000016446);
644+
645+
Assert.True(eofHitCounter.EofHitCount <= 2);
646+
Assert.Equal(new Size(200, 120), eofHitCounter.Image.Size);
647+
}
639648
}

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ public static class Bad
180180
// Invalid color type.
181181
public const string ColorTypeOne = "Png/xc1n0g08.png";
182182
public const string ColorTypeNine = "Png/xc9n2c08.png";
183+
184+
public const string FlagOfGermany0000016446 = "Png/issues/flag_of_germany-0000016446.png";
183185
}
184186
}
185187

Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)