Skip to content

Commit 339b26d

Browse files
Merge pull request #2749 from SixLabors/js/identify-bad-lzw
GifDecoder : Allow skipping bad metadata using identify
2 parents ede2f2d + af007fa commit 339b26d

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/ImageSharp/Formats/Gif/LzwDecoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public void SkipIndices(int minCodeSize, int length)
255255
// Don't attempt to decode the frame indices.
256256
// Theoretically we could determine a min code size from the length of the provided
257257
// color palette but we won't bother since the image is most likely corrupted.
258-
GifThrowHelper.ThrowInvalidImageContentException("Gif Image does not contain a valid LZW minimum code.");
258+
return;
259259
}
260260

261261
int codeSize = minCodeSize + 1;

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void CloneIsDeep()
3535
RepeatCount = 1,
3636
ColorTableMode = GifColorTableMode.Global,
3737
GlobalColorTable = new[] { Color.Black, Color.White },
38-
Comments = new List<string> { "Foo" }
38+
Comments = ["Foo"]
3939
};
4040

4141
GifMetadata clone = (GifMetadata)meta.DeepClone();
@@ -126,7 +126,7 @@ public void Identify_VerifyRatio(string imagePath, int xResolution, int yResolut
126126
public async Task Identify_VerifyRatioAsync(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit)
127127
{
128128
TestFile testFile = TestFile.Create(imagePath);
129-
using MemoryStream stream = new(testFile.Bytes, false);
129+
await using MemoryStream stream = new(testFile.Bytes, false);
130130
ImageInfo image = await GifDecoder.Instance.IdentifyAsync(DecoderOptions.Default, stream);
131131
ImageMetadata meta = image.Metadata;
132132
Assert.Equal(xResolution, meta.HorizontalResolution);
@@ -152,7 +152,7 @@ public void Decode_VerifyRatio(string imagePath, int xResolution, int yResolutio
152152
public async Task Decode_VerifyRatioAsync(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit)
153153
{
154154
TestFile testFile = TestFile.Create(imagePath);
155-
using MemoryStream stream = new(testFile.Bytes, false);
155+
await using MemoryStream stream = new(testFile.Bytes, false);
156156
using Image<Rgba32> image = await GifDecoder.Instance.DecodeAsync<Rgba32>(DecoderOptions.Default, stream);
157157
ImageMetadata meta = image.Metadata;
158158
Assert.Equal(xResolution, meta.HorizontalResolution);
@@ -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
}

0 commit comments

Comments
 (0)