diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index ad073ccbb1..d9f6ae3ac4 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -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); diff --git a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs index dcce56e881..0e0b240203 100644 --- a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs @@ -381,4 +381,21 @@ public void Issue2859_LZWPixelStackOverflow(TestImageProvider 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(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + // We should throw a InvalidImageContentException when trying to identify or load an invalid GIF file. + TestFile testFile = TestFile.Create(provider.SourceFileOrDescription); + + Assert.Throws(() => Image.Identify(testFile.FullPath)); + Assert.Throws(() => Image.Load(testFile.FullPath)); + + DecoderOptions options = new() { SkipMetadata = true }; + Assert.Throws(() => Image.Identify(options, testFile.FullPath)); + Assert.Throws(() => Image.Load(options, testFile.FullPath)); + } } diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index e75e7fa482..39b90b28a4 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -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 = diff --git a/tests/Images/Input/Gif/issues/issue_2953.gif b/tests/Images/Input/Gif/issues/issue_2953.gif new file mode 100644 index 0000000000..98c06e5c58 --- /dev/null +++ b/tests/Images/Input/Gif/issues/issue_2953.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4fa4a002b264a41677cc10f115f3572111852993a53ee8cea5f4ec0bf8dec195 +size 40