Skip to content

Commit a5a5bc8

Browse files
Merge pull request #2731 from SixLabors/af/fix-allocator-overflow-3.1
[3.1] Fix overflow in MemoryAllocator.Create(options)
2 parents da5f09a + 7d3f852 commit a5a5bc8

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
@@ -49,7 +49,7 @@ public static MemoryAllocator Create(MemoryAllocatorOptions options)
4949
UniformUnmanagedMemoryPoolMemoryAllocator allocator = new(options.MaximumPoolSizeMegabytes);
5050
if (options.AllocationLimitMegabytes.HasValue)
5151
{
52-
allocator.MemoryGroupAllocationLimitBytes = options.AllocationLimitMegabytes.Value * 1024 * 1024;
52+
allocator.MemoryGroupAllocationLimitBytes = options.AllocationLimitMegabytes.Value * 1024L * 1024L;
5353
allocator.SingleBufferAllocationLimitBytes = (int)Math.Min(allocator.SingleBufferAllocationLimitBytes, allocator.MemoryGroupAllocationLimitBytes);
5454
}
5555

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,4 +442,20 @@ public void AllocateGroup_OverLimit_ThrowsInvalidMemoryOperationException()
442442
allocator.AllocateGroup<byte>(4 * oneMb, 1024).Dispose(); // Should work
443443
Assert.Throws<InvalidMemoryOperationException>(() => allocator.AllocateGroup<byte>(5 * oneMb, 1024));
444444
}
445+
446+
[ConditionalFact(typeof(Environment), nameof(Environment.Is64BitProcess))]
447+
public void MemoryAllocator_Create_SetHighLimit()
448+
{
449+
RemoteExecutor.Invoke(RunTest).Dispose();
450+
static void RunTest()
451+
{
452+
const long threeGB = 3L * (1 << 30);
453+
MemoryAllocator allocator = MemoryAllocator.Create(new MemoryAllocatorOptions()
454+
{
455+
AllocationLimitMegabytes = (int)(threeGB / 1024)
456+
});
457+
using MemoryGroup<byte> memoryGroup = allocator.AllocateGroup<byte>(threeGB, 1024);
458+
Assert.Equal(threeGB, memoryGroup.TotalLength);
459+
}
460+
}
445461
}

0 commit comments

Comments
 (0)