Browse Source

Fix #2925

pull/2930/head
James Jackson-South 8 months ago
parent
commit
04d03136bb
  1. 13
      src/ImageSharp/Formats/Webp/WebpDecoderCore.cs
  2. 10
      tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs
  3. 1
      tests/ImageSharp.Tests/TestImages.cs
  4. 3
      tests/Images/Input/Webp/issues/Issue2925.webp

13
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
/// </summary>
/// <param name="stream">The stream to decode from.</param>
/// <param name="buffer">Temporary buffer.</param>
/// <param name="required">If true, the chunk size is required to be read, otherwise it can be skipped.</param>
/// <returns>The chunk size in bytes.</returns>
/// <exception cref="ImageFormatException">Invalid data.</exception>
private static uint ReadChunkSize(BufferedReadStream stream, Span<byte> buffer)
private static uint ReadChunkSize(BufferedReadStream stream, Span<byte> 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);
}
/// <inheritdoc/>

10
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<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> image = provider.GetImage(WebpDecoder.Instance);
image.DebugSave(provider);
image.CompareToOriginal(provider, ReferenceDecoder);
}
}

1
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";

3
tests/Images/Input/Webp/issues/Issue2925.webp

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:e12207babf122af7a62246938c2e78faa0d3f730edb3182f4f9d6adae6bfc602
size 262144
Loading…
Cancel
Save