Skip to content

Commit f4a2684

Browse files
Merge pull request #2955 from SixLabors/js/v3-issue-2953
Backport V3 - GIF: Check for end of stream when reading comments.
2 parents f7d1a9e + 13c8485 commit f4a2684

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

src/ImageSharp/Formats/Gif/GifDecoderCore.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,11 @@ private void ReadComments(BufferedReadStream stream)
406406
GifThrowHelper.ThrowInvalidImageContentException($"Gif comment length '{length}' exceeds max '{GifConstants.MaxCommentSubBlockLength}' of a comment data block");
407407
}
408408

409+
if (length == -1)
410+
{
411+
GifThrowHelper.ThrowInvalidImageContentException("Unexpected end of stream while reading gif comment");
412+
}
413+
409414
if (this.skipMetadata)
410415
{
411416
stream.Seek(length, SeekOrigin.Current);

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,4 +381,21 @@ public void Issue2859_LZWPixelStackOverflow<TPixel>(TestImageProvider<TPixel> pr
381381
image.DebugSaveMultiFrame(provider);
382382
image.CompareToReferenceOutputMultiFrame(provider, ImageComparer.Exact);
383383
}
384+
385+
// https://github.com/SixLabors/ImageSharp/issues/2953
386+
[Theory]
387+
[WithFile(TestImages.Gif.Issues.Issue2953, PixelTypes.Rgba32)]
388+
public void Issue2953<TPixel>(TestImageProvider<TPixel> provider)
389+
where TPixel : unmanaged, IPixel<TPixel>
390+
{
391+
// We should throw a InvalidImageContentException when trying to identify or load an invalid GIF file.
392+
TestFile testFile = TestFile.Create(provider.SourceFileOrDescription);
393+
394+
Assert.Throws<InvalidImageContentException>(() => Image.Identify(testFile.FullPath));
395+
Assert.Throws<InvalidImageContentException>(() => Image.Load(testFile.FullPath));
396+
397+
DecoderOptions options = new() { SkipMetadata = true };
398+
Assert.Throws<InvalidImageContentException>(() => Image.Identify(options, testFile.FullPath));
399+
Assert.Throws<InvalidImageContentException>(() => Image.Load(options, testFile.FullPath));
400+
}
384401
}

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ public static class Issues
568568
public const string Issue2866 = "Gif/issues/issue_2866.gif";
569569
public const string Issue2859_A = "Gif/issues/issue_2859_A.gif";
570570
public const string Issue2859_B = "Gif/issues/issue_2859_B.gif";
571+
public const string Issue2953 = "Gif/issues/issue_2953.gif";
571572
}
572573

573574
public static readonly string[] Animated =
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)