Browse Source

Better exceptions for images with degenerate dimensions

af/octree-no-pixelmap
Anton Firszov 6 years ago
parent
commit
59e2537103
  1. 2
      src/ImageSharp/Common/Exceptions/ImageFormatException.cs
  2. 2
      src/ImageSharp/Formats/IImageDecoder.cs
  3. 12
      src/ImageSharp/Formats/Jpeg/JpegDecoder.cs
  4. 3
      src/ImageSharp/Memory/InvalidMemoryOperationException.cs
  5. 3
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Baseline.cs
  6. 12
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs

2
src/ImageSharp/Common/Exceptions/ImageFormatException.cs

@ -7,7 +7,7 @@ namespace SixLabors.ImageSharp
{
/// <summary>
/// The exception that is thrown when the library tries to load
/// an image, which has an invalid format.
/// an image, which has format or content that is invalid or unsupported by ImageSharp.
/// </summary>
public class ImageFormatException : Exception
{

2
src/ImageSharp/Formats/IImageDecoder.cs

@ -18,6 +18,7 @@ namespace SixLabors.ImageSharp.Formats
/// <param name="configuration">The configuration for the image.</param>
/// <param name="stream">The <see cref="Stream"/> containing image data.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
// TODO: Document ImageFormatExceptions (https://github.com/SixLabors/ImageSharp/issues/1110)
Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
where TPixel : struct, IPixel<TPixel>;
@ -27,6 +28,7 @@ namespace SixLabors.ImageSharp.Formats
/// <param name="configuration">The configuration for the image.</param>
/// <param name="stream">The <see cref="Stream"/> containing image data.</param>
/// <returns>The <see cref="Image"/>.</returns>
// TODO: Document ImageFormatExceptions (https://github.com/SixLabors/ImageSharp/issues/1110)
Image Decode(Configuration configuration, Stream stream);
}
}

12
src/ImageSharp/Formats/Jpeg/JpegDecoder.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0.
using System.IO;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Formats.Jpeg
@ -22,10 +23,19 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
{
Guard.NotNull(stream, nameof(stream));
using (var decoder = new JpegDecoderCore(configuration, this))
using var decoder = new JpegDecoderCore(configuration, this);
try
{
return decoder.Decode<TPixel>(stream);
}
catch (InvalidMemoryOperationException ex)
{
(int w, int h) = (decoder.ImageWidth, decoder.ImageHeight);
// TODO: use InvalidImageContentException here, if we decide to define it
// https://github.com/SixLabors/ImageSharp/issues/1110
throw new ImageFormatException($"Can not decode the image having degenerate dimensions of {w}x{h}.", ex);
}
}
/// <inheritdoc />

3
src/ImageSharp/Memory/InvalidMemoryOperationException.cs

@ -6,7 +6,8 @@ using System;
namespace SixLabors.ImageSharp.Memory
{
/// <summary>
/// Exception thrown on invalid memory (allocation) requests.
/// Exception thrown when the library detects an invalid memory allocation request,
/// or an attempt has been made to use an invalidated <see cref="IMemoryGroup{T}"/>.
/// </summary>
public class InvalidMemoryOperationException : InvalidOperationException
{

3
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Baseline.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0.
using Microsoft.DotNet.RemoteExecutor;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Tests.TestUtilities;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
@ -48,7 +49,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
[Theory]
[WithFileCollection(nameof(UnrecoverableTestJpegs), PixelTypes.Rgba32)]
public void UnrecoverableImagesShouldThrowCorrectError<TPixel>(TestImageProvider<TPixel> provider)
public void UnrecoverableImage_Throws_ImageFormatException<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> => Assert.Throws<ImageFormatException>(provider.GetImage);
}
}

12
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs

@ -105,6 +105,18 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
appendPixelTypeToFileName: false);
}
[Theory]
[WithFile(TestImages.Jpeg.Baseline.Floorplan, PixelTypes.Rgba32)]
[WithFile(TestImages.Jpeg.Progressive.Festzug, PixelTypes.Rgba32)]
public void DegenerateMemoryRequest_ShouldTranslateTo_ImageFormatException<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
provider.LimitAllocatorBufferCapacity(100);
ImageFormatException ex = Assert.Throws<ImageFormatException>(provider.GetImage);
this.Output.WriteLine(ex.Message);
Assert.IsType<InvalidMemoryOperationException>(ex.InnerException);
}
// DEBUG ONLY!
// The PDF.js output should be saved by "tests\ImageSharp.Tests\Formats\Jpg\pdfjs\jpeg-converter.htm"
// into "\tests\Images\ActualOutput\JpegDecoderTests\"

Loading…
Cancel
Save