Skip to content

Commit 9dda64a

Browse files
Merge pull request #2754 from SixLabors/backport-v3/gif-lzw
v3 - Backport. Handle out of bounds Gif LZW max code
2 parents fd77d25 + 9f889f7 commit 9dda64a

File tree

14 files changed

+74
-13
lines changed

14 files changed

+74
-13
lines changed

src/ImageSharp/Formats/Gif/LzwDecoder.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ internal sealed class LzwDecoder : IDisposable
1919
/// </summary>
2020
private const int MaxStackSize = 4096;
2121

22+
/// <summary>
23+
/// The maximum bits for a lzw code.
24+
/// </summary>
25+
private const int MaximumLzwBits = 12;
26+
2227
/// <summary>
2328
/// The null code.
2429
/// </summary>
@@ -73,12 +78,12 @@ public void DecodePixels(int minCodeSize, Buffer2D<byte> pixels)
7378
// It is possible to specify a larger LZW minimum code size than the palette length in bits
7479
// which may leave a gap in the codes where no colors are assigned.
7580
// http://www.matthewflickinger.com/lab/whatsinagif/lzw_image_data.asp#lzw_compression
76-
if (minCodeSize < 2 || clearCode > MaxStackSize)
81+
if (minCodeSize < 2 || minCodeSize > MaximumLzwBits || clearCode > MaxStackSize)
7782
{
7883
// Don't attempt to decode the frame indices.
7984
// Theoretically we could determine a min code size from the length of the provided
8085
// color palette but we won't bother since the image is most likely corrupted.
81-
GifThrowHelper.ThrowInvalidImageContentException("Gif Image does not contain a valid LZW minimum code.");
86+
return;
8287
}
8388

8489
// The resulting index table length.
@@ -245,12 +250,12 @@ public void SkipIndices(int minCodeSize, int length)
245250
// It is possible to specify a larger LZW minimum code size than the palette length in bits
246251
// which may leave a gap in the codes where no colors are assigned.
247252
// http://www.matthewflickinger.com/lab/whatsinagif/lzw_image_data.asp#lzw_compression
248-
if (minCodeSize < 2 || clearCode > MaxStackSize)
253+
if (minCodeSize < 2 || minCodeSize > MaximumLzwBits || clearCode > MaxStackSize)
249254
{
250255
// Don't attempt to decode the frame indices.
251256
// Theoretically we could determine a min code size from the length of the provided
252257
// color palette but we won't bother since the image is most likely corrupted.
253-
GifThrowHelper.ThrowInvalidImageContentException("Gif Image does not contain a valid LZW minimum code.");
258+
return;
254259
}
255260

256261
int codeSize = minCodeSize + 1;

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,9 @@ public void Issue2012EmptyXmp<TPixel>(TestImageProvider<TPixel> provider)
296296
public void Issue2012BadMinCode<TPixel>(TestImageProvider<TPixel> provider)
297297
where TPixel : unmanaged, IPixel<TPixel>
298298
{
299-
Exception ex = Record.Exception(
300-
() =>
301-
{
302-
using Image<TPixel> image = provider.GetImage();
303-
image.DebugSave(provider);
304-
});
305-
306-
Assert.NotNull(ex);
307-
Assert.Contains("Gif Image does not contain a valid LZW minimum code.", ex.Message);
299+
using Image<TPixel> image = provider.GetImage();
300+
image.DebugSave(provider);
301+
image.CompareToReferenceOutput(provider);
308302
}
309303

310304
// https://bugzilla.mozilla.org/show_bug.cgi?id=55918
@@ -318,4 +312,15 @@ public void IssueDeferredClearCode<TPixel>(TestImageProvider<TPixel> provider)
318312
image.DebugSave(provider);
319313
image.CompareFirstFrameToReferenceOutput(ImageComparer.Exact, provider);
320314
}
315+
316+
// https://github.com/SixLabors/ImageSharp/issues/2743
317+
[Theory]
318+
[WithFile(TestImages.Gif.Issues.BadMaxLzwBits, PixelTypes.Rgba32)]
319+
public void IssueTooLargeLzwBits<TPixel>(TestImageProvider<TPixel> provider)
320+
where TPixel : unmanaged, IPixel<TPixel>
321+
{
322+
using Image<TPixel> image = provider.GetImage();
323+
image.DebugSaveMultiFrame(provider);
324+
image.CompareToReferenceOutputMultiFrame(provider, ImageComparer.Exact);
325+
}
321326
}

tests/ImageSharp.Tests/Formats/Gif/GifMetadataTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,24 @@ public void Identify_Frames(
214214
Assert.Equal(frameDelay, gifFrameMetadata.FrameDelay);
215215
Assert.Equal(disposalMethod, gifFrameMetadata.DisposalMethod);
216216
}
217+
218+
[Theory]
219+
[InlineData(TestImages.Gif.Issues.BadMaxLzwBits, 8)]
220+
[InlineData(TestImages.Gif.Issues.Issue2012BadMinCode, 1)]
221+
public void Identify_Frames_Bad_Lzw(string imagePath, int framesCount)
222+
{
223+
TestFile testFile = TestFile.Create(imagePath);
224+
using MemoryStream stream = new(testFile.Bytes, false);
225+
226+
ImageInfo imageInfo = Image.Identify(stream);
227+
228+
Assert.NotNull(imageInfo);
229+
GifMetadata gifMetadata = imageInfo.Metadata.GetGifMetadata();
230+
Assert.NotNull(gifMetadata);
231+
232+
Assert.Equal(framesCount, imageInfo.FrameMetadataCollection.Count);
233+
GifFrameMetadata gifFrameMetadata = imageInfo.FrameMetadataCollection[imageInfo.FrameMetadataCollection.Count - 1].GetGifMetadata();
234+
235+
Assert.NotNull(gifFrameMetadata);
236+
}
217237
}

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ public static class Issues
515515
public const string BadAppExtLength = "Gif/issues/issue405_badappextlength252.gif";
516516
public const string BadAppExtLength_2 = "Gif/issues/issue405_badappextlength252-2.gif";
517517
public const string BadDescriptorWidth = "Gif/issues/issue403_baddescriptorwidth.gif";
518+
public const string BadMaxLzwBits = "Gif/issues/issue_2743.gif";
518519
public const string DeferredClearCode = "Gif/issues/bugzilla-55918.gif";
519520
public const string Issue1505 = "Gif/issues/issue1505_argumentoutofrange.png";
520521
public const string Issue1530 = "Gif/issues/issue1530.gif";
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)