Browse Source

Limit lzw bits to a maximum of 12 bits, fixes issue #2743

pull/2744/head
Brian Popow 2 years ago
parent
commit
4387db97f5
  1. 9
      src/ImageSharp/Formats/Gif/LzwDecoder.cs
  2. 17
      tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs
  3. 1
      tests/ImageSharp.Tests/TestImages.cs
  4. 3
      tests/Images/Input/Gif/issues/issue_2743.gif

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

@ -19,6 +19,11 @@ internal sealed class LzwDecoder : IDisposable
/// </summary> /// </summary>
private const int MaxStackSize = 4096; private const int MaxStackSize = 4096;
/// <summary>
/// The maximum bits for a lzw code.
/// </summary>
private const int MaximumLzwBits = 12;
/// <summary> /// <summary>
/// The null code. /// The null code.
/// </summary> /// </summary>
@ -73,7 +78,7 @@ internal sealed class LzwDecoder : IDisposable
// It is possible to specify a larger LZW minimum code size than the palette length in bits // It is possible to specify a larger LZW minimum code size than the palette length in bits
// which may leave a gap in the codes where no colors are assigned. // which may leave a gap in the codes where no colors are assigned.
// http://www.matthewflickinger.com/lab/whatsinagif/lzw_image_data.asp#lzw_compression // http://www.matthewflickinger.com/lab/whatsinagif/lzw_image_data.asp#lzw_compression
if (minCodeSize < 2 || clearCode > MaxStackSize) if (minCodeSize < 2 || minCodeSize > MaximumLzwBits || clearCode > MaxStackSize)
{ {
// Don't attempt to decode the frame indices. // Don't attempt to decode the frame indices.
// Theoretically we could determine a min code size from the length of the provided // Theoretically we could determine a min code size from the length of the provided
@ -245,7 +250,7 @@ internal sealed class LzwDecoder : IDisposable
// It is possible to specify a larger LZW minimum code size than the palette length in bits // It is possible to specify a larger LZW minimum code size than the palette length in bits
// which may leave a gap in the codes where no colors are assigned. // which may leave a gap in the codes where no colors are assigned.
// http://www.matthewflickinger.com/lab/whatsinagif/lzw_image_data.asp#lzw_compression // http://www.matthewflickinger.com/lab/whatsinagif/lzw_image_data.asp#lzw_compression
if (minCodeSize < 2 || clearCode > MaxStackSize) if (minCodeSize < 2 || minCodeSize > MaximumLzwBits || clearCode > MaxStackSize)
{ {
// Don't attempt to decode the frame indices. // Don't attempt to decode the frame indices.
// Theoretically we could determine a min code size from the length of the provided // Theoretically we could determine a min code size from the length of the provided

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

@ -318,4 +318,21 @@ public class GifDecoderTests
image.DebugSave(provider); image.DebugSave(provider);
image.CompareFirstFrameToReferenceOutput(ImageComparer.Exact, provider); image.CompareFirstFrameToReferenceOutput(ImageComparer.Exact, provider);
} }
// https://github.com/SixLabors/ImageSharp/issues/2743
[Theory]
[WithFile(TestImages.Gif.Issues.BadMaxLzwBits, PixelTypes.Rgba32)]
public void IssueTooLargeLzwBits<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
Exception ex = Record.Exception(
() =>
{
using Image<TPixel> image = provider.GetImage();
image.DebugSave(provider);
});
Assert.NotNull(ex);
Assert.Contains("Gif Image does not contain a valid LZW minimum code.", ex.Message);
}
} }

1
tests/ImageSharp.Tests/TestImages.cs

@ -516,6 +516,7 @@ public static class TestImages
public const string BadAppExtLength = "Gif/issues/issue405_badappextlength252.gif"; public const string BadAppExtLength = "Gif/issues/issue405_badappextlength252.gif";
public const string BadAppExtLength_2 = "Gif/issues/issue405_badappextlength252-2.gif"; public const string BadAppExtLength_2 = "Gif/issues/issue405_badappextlength252-2.gif";
public const string BadDescriptorWidth = "Gif/issues/issue403_baddescriptorwidth.gif"; public const string BadDescriptorWidth = "Gif/issues/issue403_baddescriptorwidth.gif";
public const string BadMaxLzwBits = "Gif/issues/issue_2743.gif";
public const string DeferredClearCode = "Gif/issues/bugzilla-55918.gif"; public const string DeferredClearCode = "Gif/issues/bugzilla-55918.gif";
public const string Issue1505 = "Gif/issues/issue1505_argumentoutofrange.png"; public const string Issue1505 = "Gif/issues/issue1505_argumentoutofrange.png";
public const string Issue1530 = "Gif/issues/issue1530.gif"; public const string Issue1530 = "Gif/issues/issue1530.gif";

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

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