Browse Source

Merge pull request #2561 from SixLabors/js/faster-png-eof-handling

Png- Do not attempt to read data for chunks of length 0.
pull/2594/head
James Jackson-South 2 years ago
committed by GitHub
parent
commit
6cf09dc1c0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  2. 9
      tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs
  3. 2
      tests/ImageSharp.Tests/TestImages.cs
  4. 3
      tests/Images/Input/Png/issues/flag_of_germany-0000016446.png

6
src/ImageSharp/Formats/Png/PngDecoderCore.cs

@ -15,6 +15,7 @@ using SixLabors.ImageSharp.Formats.Png.Chunks;
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 @@ internal sealed class PngDecoderCore : IImageDecoderInternals
[MethodImpl(InliningOptions.ShortMethod)]
private IMemoryOwner<byte> ReadChunkData(int length)
{
if (length == 0)
{
return new BasicArrayBuffer<byte>(Array.Empty<byte>());
}
// We rent the buffer here to return it afterwards in Decode()
IMemoryOwner<byte> buffer = this.configuration.MemoryAllocator.Allocate<byte>(length, AllocationOptions.Clean);

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

@ -636,4 +636,13 @@ public partial class PngDecoderTests
"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);
}
}

2
tests/ImageSharp.Tests/TestImages.cs

@ -180,6 +180,8 @@ public static class TestImages
// 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";
}
}

3
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
Loading…
Cancel
Save