Skip to content

Commit 04d0313

Browse files
Fix #2925
1 parent c2a8d6f commit 04d0313

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

src/ImageSharp/Formats/Webp/WebpDecoderCore.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ private WebpImageInfo ReadVp8Info(BufferedReadStream stream, ImageMetadata metad
220220
else
221221
{
222222
// Ignore unknown chunks.
223-
uint chunkSize = ReadChunkSize(stream, buffer);
223+
uint chunkSize = ReadChunkSize(stream, buffer, false);
224224
stream.Skip((int)chunkSize);
225225
}
226226
}
@@ -498,17 +498,24 @@ private static WebpChunkType ReadChunkType(BufferedReadStream stream, Span<byte>
498498
/// </summary>
499499
/// <param name="stream">The stream to decode from.</param>
500500
/// <param name="buffer">Temporary buffer.</param>
501+
/// <param name="required">If true, the chunk size is required to be read, otherwise it can be skipped.</param>
501502
/// <returns>The chunk size in bytes.</returns>
502503
/// <exception cref="ImageFormatException">Invalid data.</exception>
503-
private static uint ReadChunkSize(BufferedReadStream stream, Span<byte> buffer)
504+
private static uint ReadChunkSize(BufferedReadStream stream, Span<byte> buffer, bool required = true)
504505
{
505506
if (stream.Read(buffer, 0, 4) == 4)
506507
{
507508
uint chunkSize = BinaryPrimitives.ReadUInt32LittleEndian(buffer);
508509
return (chunkSize % 2 == 0) ? chunkSize : chunkSize + 1;
509510
}
510511

511-
throw new ImageFormatException("Invalid Webp data.");
512+
if (required)
513+
{
514+
throw new ImageFormatException("Invalid Webp data.");
515+
}
516+
517+
// Return the size of the remaining data in the stream.
518+
return (uint)(stream.Length - stream.Position);
512519
}
513520

514521
/// <inheritdoc/>

tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,4 +549,14 @@ public void Identify_VerifyRatio(string imagePath)
549549
Assert.Equal(37.8, meta.VerticalResolution);
550550
Assert.Equal(PixelResolutionUnit.PixelsPerCentimeter, meta.ResolutionUnits);
551551
}
552+
553+
[Theory]
554+
[WithFile(Lossy.Issue2925, PixelTypes.Rgba32)]
555+
public void WebpDecoder_CanDecode_Issue2925<TPixel>(TestImageProvider<TPixel> provider)
556+
where TPixel : unmanaged, IPixel<TPixel>
557+
{
558+
using Image<TPixel> image = provider.GetImage(WebpDecoder.Instance);
559+
image.DebugSave(provider);
560+
image.CompareToOriginal(provider, ReferenceDecoder);
561+
}
552562
}

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,7 @@ public static class Lossy
873873
public const string Issue2763 = "Webp/issues/Issue2763.png";
874874
public const string Issue2801 = "Webp/issues/Issue2801.webp";
875875
public const string Issue2866 = "Webp/issues/Issue2866.webp";
876+
public const string Issue2925 = "Webp/issues/Issue2925.webp";
876877
}
877878

878879
public const string AlphaBlend = "Webp/alpha-blend.webp";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:e12207babf122af7a62246938c2e78faa0d3f730edb3182f4f9d6adae6bfc602
3+
size 262144

0 commit comments

Comments
 (0)