Browse Source

Do not attempt to read data for chunks of length 0.

pull/2561/head
James Jackson-South 2 years ago
parent
commit
df2f6987a5
  1. 6
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  2. 10
      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;
@ -1569,6 +1570,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);

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

@ -7,7 +7,6 @@ using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing.Processors.Quantization;
using SixLabors.ImageSharp.Tests.TestUtilities;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs;
@ -612,4 +611,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

@ -169,6 +169,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