Skip to content

Commit 1de220a

Browse files
Added test cases for width*height overflow when using WrapMemory
1 parent 665ab2f commit 1de220a

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -294,19 +294,22 @@ public void WrapSystemDrawingBitmap_FromBytes_WhenObserved()
294294
}
295295
}
296296

297-
[Fact]
298-
public unsafe void WrapMemory_Throws_OnTooLessWrongSize()
297+
[Theory]
298+
[InlineData(20, 5, 5)]
299+
[InlineData(1023, 32, 32)]
300+
[InlineData(65536, 65537, 65536)]
301+
public unsafe void WrapMemory_Throws_OnTooLessWrongSize(int size, int width, int height)
299302
{
300303
var cfg = Configuration.CreateDefaultInstance();
301304
var metaData = new ImageMetadata();
302305

303-
var array = new Rgba32[25];
306+
var array = new Rgba32[size];
304307
Exception thrownException = null;
305308
fixed (void* ptr = array)
306309
{
307310
try
308311
{
309-
using var image = Image.WrapMemory<Rgba32>(cfg, ptr, 24, 5, 5, metaData);
312+
using var image = Image.WrapMemory<Rgba32>(cfg, ptr, size * sizeof(Rgba32), width, height, metaData);
310313
}
311314
catch (Exception e)
312315
{
@@ -317,24 +320,30 @@ public unsafe void WrapMemory_Throws_OnTooLessWrongSize()
317320
Assert.IsType<ArgumentOutOfRangeException>(thrownException);
318321
}
319322

320-
[Fact]
321-
public unsafe void WrapMemory_FromPointer_CreatedImageIsCorrect()
323+
[Theory]
324+
[InlineData(25, 5, 5)]
325+
[InlineData(26, 5, 5)]
326+
[InlineData(2, 1, 1)]
327+
[InlineData(1024, 32, 32)]
328+
[InlineData(2048, 32, 32)]
329+
public unsafe void WrapMemory_FromPointer_CreatedImageIsCorrect(int size, int width, int height)
322330
{
323331
var cfg = Configuration.CreateDefaultInstance();
324332
var metaData = new ImageMetadata();
325333

326-
var array = new Rgba32[25];
334+
var array = new Rgba32[size];
327335

328336
fixed (void* ptr = array)
329337
{
330-
using (var image = Image.WrapMemory<Rgba32>(cfg, ptr, 25, 5, 5, metaData))
338+
using (var image = Image.WrapMemory<Rgba32>(cfg, ptr, size * sizeof(Rgba32), width, height, metaData))
331339
{
332340
Assert.True(image.DangerousTryGetSinglePixelMemory(out Memory<Rgba32> imageMem));
333341
Span<Rgba32> imageSpan = imageMem.Span;
342+
Span<Rgba32> sourceSpan = array.AsSpan(0, width * height);
334343
ref Rgba32 pixel0 = ref imageSpan[0];
335-
Assert.True(Unsafe.AreSame(ref array[0], ref pixel0));
344+
Assert.True(Unsafe.AreSame(ref sourceSpan[0], ref pixel0));
336345
ref Rgba32 pixel_1 = ref imageSpan[imageSpan.Length - 1];
337-
Assert.True(Unsafe.AreSame(ref array[array.Length - 1], ref pixel_1));
346+
Assert.True(Unsafe.AreSame(ref sourceSpan[sourceSpan.Length - 1], ref pixel_1));
338347

339348
Assert.Equal(cfg, image.Configuration);
340349
Assert.Equal(metaData, image.Metadata);
@@ -395,6 +404,7 @@ public unsafe void WrapSystemDrawingBitmap_FromPointer()
395404
[InlineData(0, 5, 5)]
396405
[InlineData(20, 5, 5)]
397406
[InlineData(1023, 32, 32)]
407+
[InlineData(65536, 65537, 65536)]
398408
public void WrapMemory_MemoryOfT_InvalidSize(int size, int height, int width)
399409
{
400410
var array = new Rgba32[size];
@@ -430,6 +440,7 @@ private class TestMemoryOwner<T> : IMemoryOwner<T>
430440
[InlineData(0, 5, 5)]
431441
[InlineData(20, 5, 5)]
432442
[InlineData(1023, 32, 32)]
443+
[InlineData(65536, 65537, 65536)]
433444
public void WrapMemory_IMemoryOwnerOfT_InvalidSize(int size, int height, int width)
434445
{
435446
var array = new Rgba32[size];
@@ -476,6 +487,7 @@ public void WrapMemory_IMemoryOwnerOfT_ValidSize(int size, int height, int width
476487
[InlineData(0, 5, 5)]
477488
[InlineData(20, 5, 5)]
478489
[InlineData(1023, 32, 32)]
490+
[InlineData(65536, 65537, 65536)]
479491
public void WrapMemory_IMemoryOwnerOfByte_InvalidSize(int size, int height, int width)
480492
{
481493
var array = new byte[size * Unsafe.SizeOf<Rgba32>()];
@@ -523,6 +535,7 @@ public void WrapMemory_IMemoryOwnerOfByte_ValidSize(int size, int height, int wi
523535
[InlineData(0, 5, 5)]
524536
[InlineData(20, 5, 5)]
525537
[InlineData(1023, 32, 32)]
538+
[InlineData(65536, 65537, 65536)]
526539
public void WrapMemory_MemoryOfByte_InvalidSize(int size, int height, int width)
527540
{
528541
var array = new byte[size * Unsafe.SizeOf<Rgba32>()];

0 commit comments

Comments
 (0)