Browse Source

Ignore invalid EXIF or XMP chunks

pull/2077/head
Brian Popow 5 years ago
parent
commit
d22e50c0d0
  1. 12
      src/ImageSharp/Formats/Webp/WebpDecoderCore.cs
  2. 19
      tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs

12
src/ImageSharp/Formats/Webp/WebpDecoderCore.cs

@ -465,22 +465,18 @@ namespace SixLabors.ImageSharp.Formats.Webp
/// <param name="features">The webp image features.</param>
private void ParseOptionalExtendedChunks(WebpChunkType chunkType, WebpFeatures features)
{
int bytesRead;
switch (chunkType)
{
case WebpChunkType.Iccp:
this.ReadIccProfile();
break;
case WebpChunkType.Exif:
this.ReadExifProfile();
break;
case WebpChunkType.Xmp:
this.ReadXmpProfile();
break;
case WebpChunkType.Animation:
@ -492,7 +488,7 @@ namespace SixLabors.ImageSharp.Formats.Webp
features.AlphaChunkHeader = (byte)this.currentStream.ReadByte();
int alphaDataSize = (int)(alphaChunkSize - 1);
features.AlphaData = this.memoryAllocator.Allocate<byte>(alphaDataSize);
bytesRead = this.currentStream.Read(features.AlphaData.Memory.Span, 0, alphaDataSize);
int bytesRead = this.currentStream.Read(features.AlphaData.Memory.Span, 0, alphaDataSize);
if (bytesRead != alphaDataSize)
{
WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the alpha chunk");
@ -556,7 +552,8 @@ namespace SixLabors.ImageSharp.Formats.Webp
int bytesRead = this.currentStream.Read(exifData, 0, (int)exifChunkSize);
if (bytesRead != exifChunkSize)
{
WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the exif chunk");
// Ignore invalid chunk.
return;
}
var profile = new ExifProfile(exifData);
@ -580,7 +577,8 @@ namespace SixLabors.ImageSharp.Formats.Webp
int bytesRead = this.currentStream.Read(xmpData, 0, (int)xmpChunkSize);
if (bytesRead != xmpChunkSize)
{
WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the xmp chunk");
// Ignore invalid chunk.
return;
}
var profile = new XmpProfile(xmpData);

19
tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs

@ -1,6 +1,7 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.IO;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Formats.Webp;
@ -153,14 +154,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
[Theory]
[WithFile(TestImages.Webp.Lossy.WithExifNotEnoughData, PixelTypes.Rgba32)]
public void WebpDecoder_ThrowInvalidImageContentException_OnWithInvalidExifData<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> =>
Assert.Throws<InvalidImageContentException>(
() =>
{
using (provider.GetImage(WebpDecoder))
{
}
});
public void WebpDecoder_IgnoresInvalidExifChunk<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
Exception ex = Record.Exception(() =>
{
using Image<TPixel> image = provider.GetImage();
});
Assert.Null(ex);
}
}
}

Loading…
Cancel
Save