Skip to content

Commit 04f170b

Browse files
Fix #3009
1 parent 24576ef commit 04f170b

7 files changed

+67
-2
lines changed

src/ImageSharp/Image.FromBytes.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ public static IImageFormat DetectFormat(ReadOnlySpan<byte> buffer)
3434
/// <exception cref="UnknownImageFormatException">The encoded image format is unknown.</exception>
3535
public static unsafe IImageFormat DetectFormat(DecoderOptions options, ReadOnlySpan<byte> buffer)
3636
{
37-
Guard.NotNull(options, nameof(options.Configuration));
37+
Guard.NotNull(options, nameof(options));
38+
39+
if (buffer.IsEmpty)
40+
{
41+
throw new UnknownImageFormatException("Cannot detect image format from empty data.");
42+
}
3843

3944
fixed (byte* ptr = buffer)
4045
{
@@ -66,6 +71,13 @@ public static ImageInfo Identify(ReadOnlySpan<byte> buffer)
6671
/// <exception cref="UnknownImageFormatException">The encoded image format is unknown.</exception>
6772
public static unsafe ImageInfo Identify(DecoderOptions options, ReadOnlySpan<byte> buffer)
6873
{
74+
Guard.NotNull(options, nameof(options));
75+
76+
if (buffer.IsEmpty)
77+
{
78+
throw new UnknownImageFormatException("Cannot identify image format from empty data.");
79+
}
80+
6981
fixed (byte* ptr = buffer)
7082
{
7183
using UnmanagedMemoryStream stream = new(ptr, buffer.Length);
@@ -99,6 +111,13 @@ public static Image Load(ReadOnlySpan<byte> buffer)
99111
/// <exception cref="UnknownImageFormatException">The encoded image format is unknown.</exception>
100112
public static unsafe Image Load(DecoderOptions options, ReadOnlySpan<byte> buffer)
101113
{
114+
Guard.NotNull(options, nameof(options));
115+
116+
if (buffer.IsEmpty)
117+
{
118+
throw new UnknownImageFormatException("Cannot load image from empty data.");
119+
}
120+
102121
fixed (byte* ptr = buffer)
103122
{
104123
using UnmanagedMemoryStream stream = new(ptr, buffer.Length);
@@ -133,6 +152,13 @@ public static Image<TPixel> Load<TPixel>(ReadOnlySpan<byte> data)
133152
public static unsafe Image<TPixel> Load<TPixel>(DecoderOptions options, ReadOnlySpan<byte> data)
134153
where TPixel : unmanaged, IPixel<TPixel>
135154
{
155+
Guard.NotNull(options, nameof(options));
156+
157+
if (data.IsEmpty)
158+
{
159+
throw new UnknownImageFormatException("Cannot load image from empty data.");
160+
}
161+
136162
fixed (byte* ptr = data)
137163
{
138164
using UnmanagedMemoryStream stream = new(ptr, data.Length);

src/ImageSharp/Image.LoadPixelData.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public static Image<TPixel> LoadPixelData<TPixel>(Configuration configuration, R
6969
{
7070
Guard.NotNull(configuration, nameof(configuration));
7171

72+
if (data.IsEmpty)
73+
{
74+
throw new ArgumentException("Pixel data cannot be empty.", nameof(data));
75+
}
76+
7277
int count = width * height;
7378
Guard.MustBeGreaterThanOrEqualTo(data.Length, count, nameof(data));
7479

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public void FromBytes_CustomConfiguration()
5050
Assert.Equal(this.LocalImageFormat, format);
5151
}
5252

53+
[Fact]
54+
public void FromBytes_EmptySpan_Throws()
55+
=> Assert.Throws<UnknownImageFormatException>(() => Image.DetectFormat([]));
56+
5357
[Fact]
5458
public void FromFileSystemPath_GlobalConfiguration()
5559
{

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public void FromBytes_GlobalConfiguration()
3838
Assert.Equal(ExpectedGlobalFormat, info.Metadata.DecodedImageFormat);
3939
}
4040

41+
[Fact]
42+
public void FromBytes_EmptySpan_Throws()
43+
=> Assert.Throws<UnknownImageFormatException>(() => Image.Identify([]));
44+
4145
[Fact]
4246
public void FromBytes_CustomConfiguration()
4347
{

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,16 @@ public void Configuration_Bytes_OutFormat_Agnostic()
7979

8080
this.TestFormat.VerifyAgnosticDecodeCall(this.Marker, this.TopLevelConfiguration);
8181
}
82+
83+
[Fact]
84+
public void FromBytes_EmptySpan_Throws()
85+
{
86+
DecoderOptions options = new()
87+
{
88+
Configuration = this.TopLevelConfiguration
89+
};
90+
91+
Assert.Throws<UnknownImageFormatException>(() => Image.Load(options, []));
92+
}
8293
}
8394
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,9 @@ public void Bytes_OutFormat_Agnostic()
4545
VerifyDecodedImage(img);
4646
Assert.IsType<BmpFormat>(img.Metadata.DecodedImageFormat);
4747
}
48+
49+
[Fact]
50+
public void FromBytes_EmptySpan_Throws()
51+
=> Assert.ThrowsAny<UnknownImageFormatException>(() => Image.Load<Rgba32>([]));
4852
}
4953
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ public void Image_Load_T_Throws_UnknownImageFormatException()
3232
}
3333
});
3434

35-
public void Dispose() => this.Stream?.Dispose();
35+
[Fact]
36+
public void FromStream_Empty_Throws()
37+
{
38+
using MemoryStream ms = new();
39+
Assert.Throws<UnknownImageFormatException>(() => Image.Load(DecoderOptions.Default, ms));
40+
}
41+
42+
public void Dispose()
43+
{
44+
this.Stream?.Dispose();
45+
GC.SuppressFinalize(this);
46+
}
3647
}
3748
}

0 commit comments

Comments
 (0)