Skip to content

Commit 7386702

Browse files
committed
Fix overflow in MemoryAllocator.Create(options) (#2730)
Fix an overlook from #2706. See 92b8277#r141770676. # Conflicts: # src/ImageSharp/Memory/Allocators/MemoryAllocator.cs
1 parent f21d641 commit 7386702

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/ImageSharp/Memory/Allocators/MemoryAllocator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static MemoryAllocator Create(MemoryAllocatorOptions options)
5151
UniformUnmanagedMemoryPoolMemoryAllocator allocator = new(options.MaximumPoolSizeMegabytes);
5252
if (options.AllocationLimitMegabytes.HasValue)
5353
{
54-
allocator.MemoryGroupAllocationLimitBytes = options.AllocationLimitMegabytes.Value * 1024 * 1024;
54+
allocator.MemoryGroupAllocationLimitBytes = options.AllocationLimitMegabytes.Value * 1024L * 1024L;
5555
allocator.SingleBufferAllocationLimitBytes = (int)Math.Min(allocator.SingleBufferAllocationLimitBytes, allocator.MemoryGroupAllocationLimitBytes);
5656
}
5757

tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,5 +436,21 @@ static void RunTest()
436436
}
437437
}
438438
#endif
439+
440+
[ConditionalFact(typeof(Environment), nameof(Environment.Is64BitProcess))]
441+
public void MemoryAllocator_Create_SetHighLimit()
442+
{
443+
RemoteExecutor.Invoke(RunTest).Dispose();
444+
static void RunTest()
445+
{
446+
const long threeGB = 3L * (1 << 30);
447+
MemoryAllocator allocator = MemoryAllocator.Create(new MemoryAllocatorOptions()
448+
{
449+
AllocationLimitMegabytes = (int)(threeGB / 1024)
450+
});
451+
using MemoryGroup<byte> memoryGroup = allocator.AllocateGroup<byte>(threeGB, 1024);
452+
Assert.Equal(threeGB, memoryGroup.TotalLength);
453+
}
454+
}
439455
}
440456
}

0 commit comments

Comments
 (0)