Skip to content

Commit 665ab2f

Browse files
Fixed width*height overflows
1 parent 2880d72 commit 665ab2f

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/ImageSharp/Image.WrapMemory.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static Image<TPixel> WrapMemory<TPixel>(
5050
{
5151
Guard.NotNull(configuration, nameof(configuration));
5252
Guard.NotNull(metadata, nameof(metadata));
53-
Guard.IsTrue(pixelMemory.Length >= width * height, nameof(pixelMemory), "The length of the input memory is less than the specified image size");
53+
Guard.IsTrue(pixelMemory.Length >= (long)width * height, nameof(pixelMemory), "The length of the input memory is less than the specified image size");
5454

5555
MemoryGroup<TPixel> memorySource = MemoryGroup<TPixel>.Wrap(pixelMemory);
5656
return new Image<TPixel>(configuration, memorySource, width, height, metadata);
@@ -145,7 +145,7 @@ public static Image<TPixel> WrapMemory<TPixel>(
145145
{
146146
Guard.NotNull(configuration, nameof(configuration));
147147
Guard.NotNull(metadata, nameof(metadata));
148-
Guard.IsTrue(pixelMemoryOwner.Memory.Length >= width * height, nameof(pixelMemoryOwner), "The length of the input memory is less than the specified image size");
148+
Guard.IsTrue(pixelMemoryOwner.Memory.Length >= (long)width * height, nameof(pixelMemoryOwner), "The length of the input memory is less than the specified image size");
149149

150150
MemoryGroup<TPixel> memorySource = MemoryGroup<TPixel>.Wrap(pixelMemoryOwner);
151151
return new Image<TPixel>(configuration, memorySource, width, height, metadata);
@@ -232,7 +232,7 @@ public static Image<TPixel> WrapMemory<TPixel>(
232232

233233
ByteMemoryManager<TPixel> memoryManager = new(byteMemory);
234234

235-
Guard.IsTrue(memoryManager.Memory.Length >= width * height, nameof(byteMemory), "The length of the input memory is less than the specified image size");
235+
Guard.IsTrue(memoryManager.Memory.Length >= (long)width * height, nameof(byteMemory), "The length of the input memory is less than the specified image size");
236236

237237
MemoryGroup<TPixel> memorySource = MemoryGroup<TPixel>.Wrap(memoryManager.Memory);
238238
return new Image<TPixel>(configuration, memorySource, width, height, metadata);
@@ -422,6 +422,7 @@ public static unsafe Image<TPixel> WrapMemory<TPixel>(
422422
Guard.IsFalse(pointer == null, nameof(pointer), "Pointer must be not null");
423423
Guard.NotNull(configuration, nameof(configuration));
424424
Guard.NotNull(metadata, nameof(metadata));
425+
Guard.MustBeLessThanOrEqualTo(height * (long)width, int.MaxValue, "Total amount of pixels exceeds int.MaxValue");
425426

426427
UnmanagedMemoryManager<TPixel> memoryManager = new(pointer, width * height);
427428

0 commit comments

Comments
 (0)