Browse Source

Merge pull request #3020 from SixLabors/js/fix-3009

Throw UnknownImageFormatException for empty input
pull/3021/head
James Jackson-South 6 months ago
committed by GitHub
parent
commit
84476b8d84
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 28
      src/ImageSharp/Image.FromBytes.cs
  2. 5
      src/ImageSharp/Image.LoadPixelData.cs
  3. 4
      tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs
  4. 4
      tests/ImageSharp.Tests/Image/ImageTests.Identify.cs
  5. 11
      tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes_PassLocalConfiguration.cs
  6. 4
      tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes_UseGlobalConfiguration.cs
  7. 13
      tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_ThrowsRightException.cs

28
src/ImageSharp/Image.FromBytes.cs

@ -34,7 +34,12 @@ public abstract partial class Image
/// <exception cref="UnknownImageFormatException">The encoded image format is unknown.</exception>
public static unsafe IImageFormat DetectFormat(DecoderOptions options, ReadOnlySpan<byte> buffer)
{
Guard.NotNull(options, nameof(options.Configuration));
Guard.NotNull(options, nameof(options));
if (buffer.IsEmpty)
{
throw new UnknownImageFormatException("Cannot detect image format from empty data.");
}
fixed (byte* ptr = buffer)
{
@ -66,6 +71,13 @@ public abstract partial class Image
/// <exception cref="UnknownImageFormatException">The encoded image format is unknown.</exception>
public static unsafe ImageInfo Identify(DecoderOptions options, ReadOnlySpan<byte> buffer)
{
Guard.NotNull(options, nameof(options));
if (buffer.IsEmpty)
{
throw new UnknownImageFormatException("Cannot identify image format from empty data.");
}
fixed (byte* ptr = buffer)
{
using UnmanagedMemoryStream stream = new(ptr, buffer.Length);
@ -99,6 +111,13 @@ public abstract partial class Image
/// <exception cref="UnknownImageFormatException">The encoded image format is unknown.</exception>
public static unsafe Image Load(DecoderOptions options, ReadOnlySpan<byte> buffer)
{
Guard.NotNull(options, nameof(options));
if (buffer.IsEmpty)
{
throw new UnknownImageFormatException("Cannot load image from empty data.");
}
fixed (byte* ptr = buffer)
{
using UnmanagedMemoryStream stream = new(ptr, buffer.Length);
@ -133,6 +152,13 @@ public abstract partial class Image
public static unsafe Image<TPixel> Load<TPixel>(DecoderOptions options, ReadOnlySpan<byte> data)
where TPixel : unmanaged, IPixel<TPixel>
{
Guard.NotNull(options, nameof(options));
if (data.IsEmpty)
{
throw new UnknownImageFormatException("Cannot load image from empty data.");
}
fixed (byte* ptr = data)
{
using UnmanagedMemoryStream stream = new(ptr, data.Length);

5
src/ImageSharp/Image.LoadPixelData.cs

@ -69,6 +69,11 @@ public abstract partial class Image
{
Guard.NotNull(configuration, nameof(configuration));
if (data.IsEmpty)
{
throw new ArgumentException("Pixel data cannot be empty.", nameof(data));
}
int count = width * height;
Guard.MustBeGreaterThanOrEqualTo(data.Length, count, nameof(data));

4
tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs

@ -50,6 +50,10 @@ public partial class ImageTests
Assert.Equal(this.LocalImageFormat, format);
}
[Fact]
public void FromBytes_EmptySpan_Throws()
=> Assert.Throws<UnknownImageFormatException>(() => Image.DetectFormat([]));
[Fact]
public void FromFileSystemPath_GlobalConfiguration()
{

4
tests/ImageSharp.Tests/Image/ImageTests.Identify.cs

@ -38,6 +38,10 @@ public partial class ImageTests
Assert.Equal(ExpectedGlobalFormat, info.Metadata.DecodedImageFormat);
}
[Fact]
public void FromBytes_EmptySpan_Throws()
=> Assert.Throws<UnknownImageFormatException>(() => Image.Identify([]));
[Fact]
public void FromBytes_CustomConfiguration()
{

11
tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes_PassLocalConfiguration.cs

@ -79,5 +79,16 @@ public partial class ImageTests
this.TestFormat.VerifyAgnosticDecodeCall(this.Marker, this.TopLevelConfiguration);
}
[Fact]
public void FromBytes_EmptySpan_Throws()
{
DecoderOptions options = new()
{
Configuration = this.TopLevelConfiguration
};
Assert.Throws<UnknownImageFormatException>(() => Image.Load(options, []));
}
}
}

4
tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes_UseGlobalConfiguration.cs

@ -45,5 +45,9 @@ public partial class ImageTests
VerifyDecodedImage(img);
Assert.IsType<BmpFormat>(img.Metadata.DecodedImageFormat);
}
[Fact]
public void FromBytes_EmptySpan_Throws()
=> Assert.ThrowsAny<UnknownImageFormatException>(() => Image.Load<Rgba32>([]));
}
}

13
tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_ThrowsRightException.cs

@ -32,6 +32,17 @@ public partial class ImageTests
}
});
public void Dispose() => this.Stream?.Dispose();
[Fact]
public void FromStream_Empty_Throws()
{
using MemoryStream ms = new();
Assert.Throws<UnknownImageFormatException>(() => Image.Load(DecoderOptions.Default, ms));
}
public void Dispose()
{
this.Stream?.Dispose();
GC.SuppressFinalize(this);
}
}
}

Loading…
Cancel
Save