Skip to content

Backport V3 - GIF: Check for end of stream when reading comments. #2955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/ImageSharp/Formats/Gif/GifDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,11 @@ private void ReadComments(BufferedReadStream stream)
GifThrowHelper.ThrowInvalidImageContentException($"Gif comment length '{length}' exceeds max '{GifConstants.MaxCommentSubBlockLength}' of a comment data block");
}

if (length == -1)
{
GifThrowHelper.ThrowInvalidImageContentException("Unexpected end of stream while reading gif comment");
}

if (this.skipMetadata)
{
stream.Seek(length, SeekOrigin.Current);
Expand Down
17 changes: 17 additions & 0 deletions tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,21 @@ public void Issue2859_LZWPixelStackOverflow<TPixel>(TestImageProvider<TPixel> pr
image.DebugSaveMultiFrame(provider);
image.CompareToReferenceOutputMultiFrame(provider, ImageComparer.Exact);
}

// https://github.com/SixLabors/ImageSharp/issues/2953
[Theory]
[WithFile(TestImages.Gif.Issues.Issue2953, PixelTypes.Rgba32)]
public void Issue2953<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
// We should throw a InvalidImageContentException when trying to identify or load an invalid GIF file.
TestFile testFile = TestFile.Create(provider.SourceFileOrDescription);

Assert.Throws<InvalidImageContentException>(() => Image.Identify(testFile.FullPath));
Assert.Throws<InvalidImageContentException>(() => Image.Load(testFile.FullPath));

DecoderOptions options = new() { SkipMetadata = true };
Assert.Throws<InvalidImageContentException>(() => Image.Identify(options, testFile.FullPath));
Assert.Throws<InvalidImageContentException>(() => Image.Load(options, testFile.FullPath));
}
}
1 change: 1 addition & 0 deletions tests/ImageSharp.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ public static class Issues
public const string Issue2866 = "Gif/issues/issue_2866.gif";
public const string Issue2859_A = "Gif/issues/issue_2859_A.gif";
public const string Issue2859_B = "Gif/issues/issue_2859_B.gif";
public const string Issue2953 = "Gif/issues/issue_2953.gif";
}

public static readonly string[] Animated =
Expand Down
3 changes: 3 additions & 0 deletions tests/Images/Input/Gif/issues/issue_2953.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading