Skip to content

Commit cb115c2

Browse files
Merge pull request #2971 from SixLabors/js/backport-v2-fix-2953
Backport V2 - GIF: Check for end of stream when reading comments.
2 parents d133ef9 + 075f3c8 commit cb115c2

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

Directory.Build.props

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,13 @@
3333
<PropertyGroup Condition="$(Configuration.StartsWith('Release')) == true">
3434
<Optimize>true</Optimize>
3535
</PropertyGroup>
36+
37+
<!-- Required restore feeds. -->
38+
<PropertyGroup>
39+
<RestoreSources>
40+
https://api.nuget.org/v3/index.json;
41+
https://f.feedz.io/sixlabors/sixlabors/nuget/index.json;
42+
https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-eng/nuget/v3/index.json;
43+
</RestoreSources>
44+
</PropertyGroup>
3645
</Project>

src/ImageSharp/Formats/Gif/GifDecoderCore.cs

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

405+
if (length == -1)
406+
{
407+
GifThrowHelper.ThrowInvalidImageContentException("Unexpected end of stream while reading gif comment");
408+
}
409+
405410
if (this.skipMetadata)
406411
{
407412
stream.Seek(length, SeekOrigin.Current);

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,16 @@ public void Issue1530_BadDescriptorDimensions<TPixel>(TestImageProvider<TPixel>
183183
image.CompareToReferenceOutputMultiFrame(provider, ImageComparer.Exact);
184184
}
185185

186-
// https://github.com/SixLabors/ImageSharp/issues/2758
187-
[Theory]
188-
[WithFile(TestImages.Gif.Issues.Issue2758, PixelTypes.Rgba32)]
189-
public void Issue2758_BadDescriptorDimensions<TPixel>(TestImageProvider<TPixel> provider)
190-
where TPixel : unmanaged, IPixel<TPixel>
191-
{
192-
using Image<TPixel> image = provider.GetImage();
193-
image.DebugSaveMultiFrame(provider);
194-
image.CompareToReferenceOutputMultiFrame(provider, ImageComparer.Exact);
195-
}
186+
// https://github.com/SixLabors/ImageSharp/issues/2758
187+
[Theory]
188+
[WithFile(TestImages.Gif.Issues.Issue2758, PixelTypes.Rgba32)]
189+
public void Issue2758_BadDescriptorDimensions<TPixel>(TestImageProvider<TPixel> provider)
190+
where TPixel : unmanaged, IPixel<TPixel>
191+
{
192+
using Image<TPixel> image = provider.GetImage();
193+
image.DebugSaveMultiFrame(provider);
194+
image.CompareToReferenceOutputMultiFrame(provider, ImageComparer.Exact);
195+
}
196196

197197
// https://github.com/SixLabors/ImageSharp/issues/405
198198
[Theory]
@@ -329,5 +329,18 @@ public void Issue2859_LZWPixelStackOverflow<TPixel>(TestImageProvider<TPixel> pr
329329
image.DebugSaveMultiFrame(provider);
330330
image.CompareToReferenceOutputMultiFrame(provider, ImageComparer.Exact);
331331
}
332+
333+
// https://github.com/SixLabors/ImageSharp/issues/2953
334+
[Theory]
335+
[WithFile(TestImages.Gif.Issues.Issue2953, PixelTypes.Rgba32)]
336+
public void Issue2953<TPixel>(TestImageProvider<TPixel> provider)
337+
where TPixel : unmanaged, IPixel<TPixel>
338+
{
339+
// We should throw a InvalidImageContentException when trying to identify or load an invalid GIF file.
340+
var testFile = TestFile.Create(provider.SourceFileOrDescription);
341+
342+
Assert.Throws<InvalidImageContentException>(() => Image.Identify(testFile.FullPath));
343+
Assert.Throws<InvalidImageContentException>(() => Image.Load(testFile.FullPath));
344+
}
332345
}
333346
}

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ public static class Issues
468468
public const string Issue2758 = "Gif/issues/issue_2758.gif";
469469
public const string Issue2859_A = "Gif/issues/issue_2859_A.gif";
470470
public const string Issue2859_B = "Gif/issues/issue_2859_B.gif";
471+
public const string Issue2953 = "Gif/issues/issue_2953.gif";
471472
}
472473

473474
public static readonly string[] All = { Rings, Giphy, Cheers, Trans, Kumin, Leo, Ratio4x1, Ratio1x4 };
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)