Browse Source

Don't throw for bad min code, just don't decode indices.

pull/2014/head
James Jackson-South 4 years ago
parent
commit
c129278cae
  1. 2
      src/ImageSharp/Formats/Gif/GifDecoderCore.cs
  2. 12
      src/ImageSharp/Formats/Gif/LzwDecoder.cs
  3. 12
      tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs
  4. 1
      tests/ImageSharp.Tests/TestImages.cs
  5. 3
      tests/Images/External/ReferenceOutput/GifDecoderTests/Issue2012BadMinCode_Rgba32_issue2012_drona1.png
  6. 3
      tests/Images/Input/Gif/issues/issue2012_drona1.gif

2
src/ImageSharp/Formats/Gif/GifDecoderCore.cs

@ -378,8 +378,8 @@ namespace SixLabors.ImageSharp.Formats.Gif
}
indices = this.Configuration.MemoryAllocator.Allocate2D<byte>(this.imageDescriptor.Width, this.imageDescriptor.Height, AllocationOptions.Clean);
this.ReadFrameIndices(indices);
Span<byte> rawColorTable = default;
if (localColorTable != null)
{

12
src/ImageSharp/Formats/Gif/LzwDecoder.cs

@ -68,16 +68,20 @@ namespace SixLabors.ImageSharp.Formats.Gif
/// <param name="pixels">The pixel array to decode to.</param>
public void DecodePixels(int dataSize, Buffer2D<byte> pixels)
{
Guard.MustBeLessThanOrEqualTo(1 << dataSize, MaxStackSize, nameof(dataSize));
// Calculate the clear code. The value of the clear code is 2 ^ dataSize
int clearCode = 1 << dataSize;
if (clearCode > MaxStackSize)
{
// Don't attempt to decode the frame indices.
// The image is most likely corrupted.
return;
}
// The resulting index table length.
int width = pixels.Width;
int height = pixels.Height;
int length = width * height;
// Calculate the clear code. The value of the clear code is 2 ^ dataSize
int clearCode = 1 << dataSize;
int codeSize = dataSize + 1;
// Calculate the end code

12
tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs

@ -271,5 +271,17 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
image.DebugSave(provider);
image.CompareFirstFrameToReferenceOutput(ImageComparer.Exact, provider);
}
// https://github.com/SixLabors/ImageSharp/issues/2012
[Theory]
[WithFile(TestImages.Gif.Issues.Issue2012BadMinCode, PixelTypes.Rgba32)]
public void Issue2012BadMinCode<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> image = provider.GetImage();
image.DebugSave(provider);
image.CompareFirstFrameToReferenceOutput(ImageComparer.Exact, provider);
}
}
}

1
tests/ImageSharp.Tests/TestImages.cs

@ -453,6 +453,7 @@ namespace SixLabors.ImageSharp.Tests
public const string InvalidColorIndex = "Gif/issues/issue1668_invalidcolorindex.gif";
public const string Issue1962NoColorTable = "Gif/issues/issue1962_tiniest_gif_1st.gif";
public const string Issue2012EmptyXmp = "Gif/issues/issue2012_Stronghold-Crusader-Extreme-Cover.gif";
public const string Issue2012BadMinCode = "Gif/issues/issue2012_drona1.gif";
}
public static readonly string[] All = { Rings, Giphy, Cheers, Trans, Kumin, Leo, Ratio4x1, Ratio1x4 };

3
tests/Images/External/ReferenceOutput/GifDecoderTests/Issue2012BadMinCode_Rgba32_issue2012_drona1.png

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:cf4bc93479140b05b25066eb10c33fa9f82c617828a56526d47f5a8c72035ef0
size 1871

3
tests/Images/Input/Gif/issues/issue2012_drona1.gif

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:cbb23b2a19e314969c6da99374ae133d834d76c3f0ab9df4a7edc9334bb065e6
size 10508
Loading…
Cancel
Save