Skip to content

Commit

Permalink
Merge pull request #2732 from SixLabors/fix-allocator-overlflow-2.1
Browse files Browse the repository at this point in the history
[2.1] Fix overflow in MemoryAllocator.Create(options)
  • Loading branch information
JimBobSquarePants authored May 10, 2024
2 parents f21d641 + 8699557 commit 7ce329a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
sdk-preview: true
runtime: -x64
codecov: false
- os: macos-latest
- os: macos-13 # macos-latest runs on arm64 runners
framework: net6.0
sdk: 6.0.x
sdk-preview: true
Expand All @@ -38,7 +38,7 @@ jobs:
framework: net5.0
runtime: -x64
codecov: false
- os: macos-latest
- os: macos-13 # macos-latest runs on arm64 runners
framework: net5.0
runtime: -x64
codecov: false
Expand All @@ -50,7 +50,7 @@ jobs:
framework: netcoreapp3.1
runtime: -x64
codecov: false
- os: macos-latest
- os: macos-13 # macos-latest runs on arm64 runners
framework: netcoreapp3.1
runtime: -x64
codecov: false
Expand Down
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 7ce329a

Please sign in to comment.