Skip to content

Commit

Permalink
Fix overflow in MemoryAllocator.Create(options) (#2730)
Browse files Browse the repository at this point in the history
Fix an overlook from #2706. See 92b8277#r141770676.
# Conflicts:
#	src/ImageSharp/Memory/Allocators/MemoryAllocator.cs
  • Loading branch information
antonfirsov committed May 8, 2024
1 parent f21d641 commit 7386702
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ImageSharp/Memory/Allocators/MemoryAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static MemoryAllocator Create(MemoryAllocatorOptions options)
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,5 +436,21 @@ static void RunTest()
}
}
#endif

[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);
}
}
}
}

0 comments on commit 7386702

Please sign in to comment.