diff --git a/src/ImageSharp/Formats/WebP/BitReader/BitReaderBase.cs b/src/ImageSharp/Formats/WebP/BitReader/BitReaderBase.cs index 365c6064bb..60ed96ab3a 100644 --- a/src/ImageSharp/Formats/WebP/BitReader/BitReaderBase.cs +++ b/src/ImageSharp/Formats/WebP/BitReader/BitReaderBase.cs @@ -29,23 +29,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.WebP.BitReader { this.Data = memoryAllocator.Allocate(bytesToRead); Span dataSpan = this.Data.Memory.Span; - - using (IManagedByteBuffer buffer = memoryAllocator.AllocateManagedByteBuffer(4096)) - { - Span bufferSpan = buffer.GetSpan(); - int read; - while (bytesToRead > 0 && (read = input.Read(buffer.Array, 0, Math.Min(bufferSpan.Length, bytesToRead))) > 0) - { - buffer.Array.AsSpan(0, read).CopyTo(dataSpan); - bytesToRead -= read; - dataSpan = dataSpan.Slice(read); - } - - if (bytesToRead > 0) - { - WebPThrowHelper.ThrowImageFormatException("webp image file has insufficient data"); - } - } + input.Read(dataSpan.Slice(0, bytesToRead), 0, bytesToRead); } /// diff --git a/src/ImageSharp/Formats/WebP/Lossless/WebPLosslessDecoder.cs b/src/ImageSharp/Formats/WebP/Lossless/WebPLosslessDecoder.cs index 8a6c365957..d396d3746f 100644 --- a/src/ImageSharp/Formats/WebP/Lossless/WebPLosslessDecoder.cs +++ b/src/ImageSharp/Formats/WebP/Lossless/WebPLosslessDecoder.cs @@ -945,17 +945,23 @@ namespace SixLabors.ImageSharp.Formats.Experimental.WebP.Lossless /// The number of pixels to copy. private static void CopyBlock(Span pixelData, int decodedPixels, int dist, int length) { + int start = decodedPixels - dist; + if (start < 0) + { + WebPThrowHelper.ThrowImageFormatException("webp image data seems to be invalid"); + } + if (dist >= length) { // no overlap. - Span src = pixelData.Slice(decodedPixels - dist, length); + Span src = pixelData.Slice(start, length); Span dest = pixelData.Slice(decodedPixels); src.CopyTo(dest); } else { // There is overlap between the backward reference distance and the pixels to copy. - Span src = pixelData.Slice(decodedPixels - dist); + Span src = pixelData.Slice(start); Span dest = pixelData.Slice(decodedPixels); for (int i = 0; i < length; i++) { diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebPDecoderTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebPDecoderTests.cs index 562befa215..c58d4b4e45 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/WebPDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/WebPDecoderTests.cs @@ -321,8 +321,19 @@ namespace SixLabors.ImageSharp.Tests.Formats.WebP [Theory] [WithFile(Lossless.LossLessCorruptImage1, PixelTypes.Rgba32)] [WithFile(Lossless.LossLessCorruptImage2, PixelTypes.Rgba32)] - [WithFile(Lossless.LossLessCorruptImage3, PixelTypes.Rgba32)] [WithFile(Lossless.LossLessCorruptImage4, PixelTypes.Rgba32)] + public void WebpDecoder_CanDecodeLosslessWithIssues(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + // Just make sure no exception is thrown. The reference decoder fails to load the image. + using (Image image = provider.GetImage(WebpDecoder)) + { + image.DebugSave(provider); + } + } + + [Theory] + [WithFile(Lossless.LossLessCorruptImage3, PixelTypes.Rgba32)] public void WebpDecoder_ThrowImageFormatException_OnInvalidImages(TestImageProvider provider) where TPixel : unmanaged, IPixel {