Browse Source
Merge pull request #2731 from SixLabors/af/fix-allocator-overflow-3.1
[3.1] Fix overflow in MemoryAllocator.Create(options)
pull/2754/head
James Jackson-South
2 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with
17 additions and
1 deletions
-
src/ImageSharp/Memory/Allocators/MemoryAllocator.cs
-
tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.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); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -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); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|