Browse Source

Fix overflow in MemoryAllocator.Create(options) (#2730)

Fix an overlook from #2706. See 92b82779ac (r141770676).
pull/2731/head
antonfirsov 2 years ago
parent
commit
7d3f852245
  1. 2
      src/ImageSharp/Memory/Allocators/MemoryAllocator.cs
  2. 16
      tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs

2
src/ImageSharp/Memory/Allocators/MemoryAllocator.cs

@ -49,7 +49,7 @@ public abstract class MemoryAllocator
UniformUnmanagedMemoryPoolMemoryAllocator allocator = new(options.MaximumPoolSizeMegabytes);
if (options.AllocationLimitMegabytes.HasValue)
{
allocator.MemoryGroupAllocationLimitBytes = options.AllocationLimitMegabytes.Value * 1024 * 1024;
allocator.MemoryGroupAllocationLimitBytes = options.AllocationLimitMegabytes.Value * 1024L * 1024L;
allocator.SingleBufferAllocationLimitBytes = (int)Math.Min(allocator.SingleBufferAllocationLimitBytes, allocator.MemoryGroupAllocationLimitBytes);
}

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

@ -442,4 +442,20 @@ public class UniformUnmanagedPoolMemoryAllocatorTests
allocator.AllocateGroup<byte>(4 * oneMb, 1024).Dispose(); // Should work
Assert.Throws<InvalidMemoryOperationException>(() => allocator.AllocateGroup<byte>(5 * oneMb, 1024));
}
[ConditionalFact(typeof(Environment), nameof(Environment.Is64BitProcess))]
public void MemoryAllocator_Create_SetHighLimit()
{
RemoteExecutor.Invoke(RunTest).Dispose();
static void RunTest()
{
const long threeGB = 3L * (1 << 30);
MemoryAllocator allocator = MemoryAllocator.Create(new MemoryAllocatorOptions()
{
AllocationLimitMegabytes = (int)(threeGB / 1024)
});
using MemoryGroup<byte> memoryGroup = allocator.AllocateGroup<byte>(threeGB, 1024);
Assert.Equal(threeGB, memoryGroup.TotalLength);
}
}
}

Loading…
Cancel
Save