From a842e981910a7ecce7a64a54776ebe861535d397 Mon Sep 17 00:00:00 2001 From: Fredrik Eilertsen Date: Sat, 15 Jun 2019 01:27:29 +0200 Subject: [PATCH] Throw UnkownFormatException on Image.Load (#932) * Throw ImageFormatException on load * Unseal class and make constructor internal - This is so that no one can new it up / inherit it outside of the assembly * Add new exception for distinguish between different exception - This will be used on image.load operations with invalid image streams * ImageFormatException -> UnkownImageFormatException * Add Image.Load throws exception tests --- .../Common/Exceptions/ImageFormatException.cs | 8 +-- .../Exceptions/UnknownImageFormatException.cs | 36 +++++++++++++ src/ImageSharp/Image.FromStream.cs | 20 +++++-- ...ts.Load_FromStream_ThrowsRightException.cs | 52 +++++++++++++++++++ 4 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 src/ImageSharp/Common/Exceptions/UnknownImageFormatException.cs create mode 100644 tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_ThrowsRightException.cs diff --git a/src/ImageSharp/Common/Exceptions/ImageFormatException.cs b/src/ImageSharp/Common/Exceptions/ImageFormatException.cs index 89877b1b6..8b9dbe1b8 100644 --- a/src/ImageSharp/Common/Exceptions/ImageFormatException.cs +++ b/src/ImageSharp/Common/Exceptions/ImageFormatException.cs @@ -1,4 +1,4 @@ -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using System; @@ -9,14 +9,14 @@ namespace SixLabors.ImageSharp /// The exception that is thrown when the library tries to load /// an image, which has an invalid format. /// - public sealed class ImageFormatException : Exception + public class ImageFormatException : Exception { /// /// Initializes a new instance of the class with the name of the /// parameter that causes this exception. /// /// The error message that explains the reason for this exception. - public ImageFormatException(string errorMessage) + internal ImageFormatException(string errorMessage) : base(errorMessage) { } @@ -28,7 +28,7 @@ namespace SixLabors.ImageSharp /// The error message that explains the reason for this exception. /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) /// if no inner exception is specified. - public ImageFormatException(string errorMessage, Exception innerException) + internal ImageFormatException(string errorMessage, Exception innerException) : base(errorMessage, innerException) { } diff --git a/src/ImageSharp/Common/Exceptions/UnknownImageFormatException.cs b/src/ImageSharp/Common/Exceptions/UnknownImageFormatException.cs new file mode 100644 index 000000000..ca6e1fe6e --- /dev/null +++ b/src/ImageSharp/Common/Exceptions/UnknownImageFormatException.cs @@ -0,0 +1,36 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; + +namespace SixLabors.ImageSharp +{ + /// + /// The exception that is thrown when the library tries to load + /// an image which has an unkown format. + /// + public sealed class UnknownImageFormatException : ImageFormatException + { + /// + /// Initializes a new instance of the class with the name of the + /// parameter that causes this exception. + /// + /// The error message that explains the reason for this exception. + public UnknownImageFormatException(string errorMessage) + : base(errorMessage) + { + } + + /// + /// Initializes a new instance of the class with a specified + /// error message and the exception that is the cause of this exception. + /// + /// The error message that explains the reason for this exception. + /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) + /// if no inner exception is specified. + public UnknownImageFormatException(string errorMessage, Exception innerException) + : base(errorMessage, innerException) + { + } + } +} diff --git a/src/ImageSharp/Image.FromStream.cs b/src/ImageSharp/Image.FromStream.cs index 74b99dfb3..c4336c9ac 100644 --- a/src/ImageSharp/Image.FromStream.cs +++ b/src/ImageSharp/Image.FromStream.cs @@ -1,4 +1,4 @@ -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using System; @@ -62,6 +62,7 @@ namespace SixLabors.ImageSharp /// The stream containing image information. /// the mime type of the decoded image. /// Thrown if the stream is not readable. + /// Image cannot be loaded. /// A new .> public static Image Load(Stream stream, out IImageFormat format) => Load(Configuration.Default, stream, out format); @@ -71,6 +72,7 @@ namespace SixLabors.ImageSharp /// /// The stream containing image information. /// Thrown if the stream is not readable. + /// Image cannot be loaded. /// A new .> public static Image Load(Stream stream) => Load(Configuration.Default, stream); @@ -81,6 +83,7 @@ namespace SixLabors.ImageSharp /// The stream containing image information. /// The decoder. /// Thrown if the stream is not readable. + /// Image cannot be loaded. /// A new .> public static Image Load(Stream stream, IImageDecoder decoder) => Load(Configuration.Default, stream, decoder); @@ -92,6 +95,7 @@ namespace SixLabors.ImageSharp /// The stream containing image information. /// The decoder. /// Thrown if the stream is not readable. + /// Image cannot be loaded. /// A new .> public static Image Load(Configuration config, Stream stream, IImageDecoder decoder) => WithSeekableStream(config, stream, s => decoder.Decode(config, s)); @@ -102,6 +106,7 @@ namespace SixLabors.ImageSharp /// The config for the decoder. /// The stream containing image information. /// Thrown if the stream is not readable. + /// Image cannot be loaded. /// A new .> public static Image Load(Configuration config, Stream stream) => Load(config, stream, out _); @@ -110,6 +115,7 @@ namespace SixLabors.ImageSharp /// /// The stream containing image information. /// Thrown if the stream is not readable. + /// Image cannot be loaded. /// The pixel format. /// A new .> public static Image Load(Stream stream) @@ -122,6 +128,7 @@ namespace SixLabors.ImageSharp /// The stream containing image information. /// the mime type of the decoded image. /// Thrown if the stream is not readable. + /// Image cannot be loaded. /// The pixel format. /// A new .> public static Image Load(Stream stream, out IImageFormat format) @@ -134,6 +141,7 @@ namespace SixLabors.ImageSharp /// The stream containing image information. /// The decoder. /// Thrown if the stream is not readable. + /// Image cannot be loaded. /// The pixel format. /// A new .> public static Image Load(Stream stream, IImageDecoder decoder) @@ -147,6 +155,7 @@ namespace SixLabors.ImageSharp /// The stream containing image information. /// The decoder. /// Thrown if the stream is not readable. + /// Image cannot be loaded. /// The pixel format. /// A new .> public static Image Load(Configuration config, Stream stream, IImageDecoder decoder) @@ -159,6 +168,7 @@ namespace SixLabors.ImageSharp /// The configuration options. /// The stream containing image information. /// Thrown if the stream is not readable. + /// Image cannot be loaded. /// The pixel format. /// A new .> public static Image Load(Configuration config, Stream stream) @@ -172,6 +182,7 @@ namespace SixLabors.ImageSharp /// The stream containing image information. /// the mime type of the decoded image. /// Thrown if the stream is not readable. + /// Image cannot be loaded. /// The pixel format. /// A new .> public static Image Load(Configuration config, Stream stream, out IImageFormat format) @@ -195,7 +206,7 @@ namespace SixLabors.ImageSharp sb.AppendLine($" - {val.Key.Name} : {val.Value.GetType().Name}"); } - throw new NotSupportedException(sb.ToString()); + throw new UnknownImageFormatException(sb.ToString()); } /// @@ -206,6 +217,7 @@ namespace SixLabors.ImageSharp /// The stream containing image information. /// the mime type of the decoded image. /// Thrown if the stream is not readable. + /// Image cannot be loaded. /// A new . public static Image Load(Configuration config, Stream stream, out IImageFormat format) { @@ -227,7 +239,7 @@ namespace SixLabors.ImageSharp sb.AppendLine($" - {val.Key.Name} : {val.Value.GetType().Name}"); } - throw new NotSupportedException(sb.ToString()); + throw new UnknownImageFormatException(sb.ToString()); } private static T WithSeekableStream(Configuration config, Stream stream, Func action) @@ -257,4 +269,4 @@ namespace SixLabors.ImageSharp } } } -} \ No newline at end of file +} diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_ThrowsRightException.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_ThrowsRightException.cs new file mode 100644 index 000000000..df1f73c7a --- /dev/null +++ b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_ThrowsRightException.cs @@ -0,0 +1,52 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.IO; + +using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.Formats.Bmp; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.Primitives; + +using Xunit; + +namespace SixLabors.ImageSharp.Tests +{ + public partial class ImageTests + { + public class Load_FromStream_Throws : IDisposable + { + private static readonly byte[] Data = new byte[] { 0x01 }; + + private MemoryStream Stream { get; } = new MemoryStream(Data); + + [Fact] + public void Image_Load_Throws_UknownImageFormatException() + { + Assert.Throws(() => + { + using (var img = Image.Load(Configuration.Default, this.Stream, out IImageFormat format)) + { + } + }); + } + + [Fact] + public void Image_Load_T_Throws_UknownImageFormatException() + { + Assert.Throws(() => + { + using (var img = Image.Load(Configuration.Default, this.Stream, out IImageFormat format)) + { + } + }); + } + + public void Dispose() + { + this.Stream?.Dispose(); + } + } + } +}