Browse Source

Disallow allocation attempts of unrepresentable sizes

backport of #2545
pull/2553/head
antonfirsov 2 years ago
parent
commit
f36ec12695
  1. 5
      src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs
  2. 4
      tests/ImageSharp.Tests/Image/ImageTests.cs
  3. 7
      tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs

5
src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs

@ -121,6 +121,11 @@ namespace SixLabors.ImageSharp.Memory
AllocationOptions options = AllocationOptions.None)
{
long totalLengthInBytes = totalLength * Unsafe.SizeOf<T>();
if (totalLengthInBytes < 0)
{
throw new InvalidMemoryOperationException("Attempted to allocate a MemoryGroup of a size that is not representable.");
}
if (totalLengthInBytes <= this.sharedArrayPoolThresholdInBytes)
{
var buffer = new SharedArrayPoolBuffer<T>((int)totalLength);

4
tests/ImageSharp.Tests/Image/ImageTests.cs

@ -40,6 +40,10 @@ namespace SixLabors.ImageSharp.Tests
}
}
[Fact]
public void Width_Height_SizeNotRepresentable_ThrowsInvalidImageOperationException()
=> Assert.Throws<InvalidMemoryOperationException>(() => new Image<Rgba32>(int.MaxValue, int.MaxValue));
[Fact]
public void Configuration_Width_Height()
{

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

@ -111,6 +111,13 @@ namespace SixLabors.ImageSharp.Tests.Memory.Allocators
}
}
[Fact]
public void AllocateGroup_SizeInBytesOverLongMaxValue_ThrowsInvalidMemoryOperationException()
{
var allocator = new UniformUnmanagedMemoryPoolMemoryAllocator(null);
Assert.Throws<InvalidMemoryOperationException>(() => allocator.AllocateGroup<S4>(int.MaxValue * (long)int.MaxValue, int.MaxValue));
}
[Fact]
public unsafe void Allocate_MemoryIsPinnableMultipleTimes()
{

Loading…
Cancel
Save