diff --git a/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs b/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs index 9ca6e2bee..51379a32a 100644 --- a/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs +++ b/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs @@ -220,7 +220,7 @@ internal sealed class WebpDecoderCore : ImageDecoderCore, IDisposable else { // Ignore unknown chunks. - uint chunkSize = ReadChunkSize(stream, buffer); + uint chunkSize = ReadChunkSize(stream, buffer, false); stream.Skip((int)chunkSize); } } @@ -498,9 +498,10 @@ internal sealed class WebpDecoderCore : ImageDecoderCore, IDisposable /// /// The stream to decode from. /// Temporary buffer. + /// If true, the chunk size is required to be read, otherwise it can be skipped. /// The chunk size in bytes. /// Invalid data. - private static uint ReadChunkSize(BufferedReadStream stream, Span buffer) + private static uint ReadChunkSize(BufferedReadStream stream, Span buffer, bool required = true) { if (stream.Read(buffer, 0, 4) == 4) { @@ -508,7 +509,13 @@ internal sealed class WebpDecoderCore : ImageDecoderCore, IDisposable return (chunkSize % 2 == 0) ? chunkSize : chunkSize + 1; } - throw new ImageFormatException("Invalid Webp data."); + if (required) + { + throw new ImageFormatException("Invalid Webp data."); + } + + // Return the size of the remaining data in the stream. + return (uint)(stream.Length - stream.Position); } /// diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs index adabb727d..660ab2e66 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs @@ -549,4 +549,14 @@ public class WebpDecoderTests Assert.Equal(37.8, meta.VerticalResolution); Assert.Equal(PixelResolutionUnit.PixelsPerCentimeter, meta.ResolutionUnits); } + + [Theory] + [WithFile(Lossy.Issue2925, PixelTypes.Rgba32)] + public void WebpDecoder_CanDecode_Issue2925(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + using Image image = provider.GetImage(WebpDecoder.Instance); + image.DebugSave(provider); + image.CompareToOriginal(provider, ReferenceDecoder); + } } diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 78632e7dc..e49d95ef6 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -873,6 +873,7 @@ public static class TestImages public const string Issue2763 = "Webp/issues/Issue2763.png"; public const string Issue2801 = "Webp/issues/Issue2801.webp"; public const string Issue2866 = "Webp/issues/Issue2866.webp"; + public const string Issue2925 = "Webp/issues/Issue2925.webp"; } public const string AlphaBlend = "Webp/alpha-blend.webp"; diff --git a/tests/Images/Input/Webp/issues/Issue2925.webp b/tests/Images/Input/Webp/issues/Issue2925.webp new file mode 100644 index 000000000..414a06caa --- /dev/null +++ b/tests/Images/Input/Webp/issues/Issue2925.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e12207babf122af7a62246938c2e78faa0d3f730edb3182f4f9d6adae6bfc602 +size 262144