Skip to content

Commit a7d6de9

Browse files
Merge pull request #2929 from SixLabors/js/v3-issue-2925
V3 - Do not throw exception for non-required chunks.
2 parents 2542706 + 3dd6bd6 commit a7d6de9

File tree

6 files changed

+30
-5
lines changed

6 files changed

+30
-5
lines changed

.github/workflows/build-and-test.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ jobs:
6868
steps:
6969
- name: Install libgdi+, which is required for tests running on ubuntu
7070
if: ${{ contains(matrix.options.os, 'ubuntu') }}
71-
run: sudo apt-get -y install libgdiplus libgif-dev libglib2.0-dev libcairo2-dev libtiff-dev libexif-dev
71+
run: |
72+
sudo apt-get update
73+
sudo apt-get -y install libgdiplus libgif-dev libglib2.0-dev libcairo2-dev libtiff-dev libexif-dev
7274
7375
- name: Git Config
7476
shell: bash

.github/workflows/code-coverage.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ jobs:
1818

1919
steps:
2020
- name: Install libgdi+, which is required for tests running on ubuntu
21-
run: sudo apt-get -y install libgdiplus libgif-dev libglib2.0-dev libcairo2-dev libtiff-dev libexif-dev
21+
run: |
22+
sudo apt-get update
23+
sudo apt-get -y install libgdiplus libgif-dev libglib2.0-dev libcairo2-dev libtiff-dev libexif-dev
2224
2325
- name: Git Config
2426
shell: bash

src/ImageSharp/Formats/Webp/WebpDecoderCore.cs

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

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

516523
/// <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
@@ -859,6 +859,7 @@ public static class Lossy
859859
public const string Issue2763 = "Webp/issues/Issue2763.png";
860860
public const string Issue2801 = "Webp/issues/Issue2801.webp";
861861
public const string Issue2866 = "Webp/issues/Issue2866.webp";
862+
public const string Issue2925 = "Webp/issues/Issue2925.webp";
862863
}
863864
}
864865

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)