Browse Source
Merge pull request #2545 from SixLabors/af/fix-memorygroup-overallocation
Disallow allocation attempts of unrepresentable sizes
pull/2549/head
James Jackson-South
3 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with
19 additions and
2 deletions
-
src/ImageSharp/ImageSharp.csproj
-
src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs
-
tests/ImageSharp.Tests/Image/ImageTests.cs
-
tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs
|
|
|
@ -22,8 +22,8 @@ |
|
|
|
</PropertyGroup> |
|
|
|
|
|
|
|
<PropertyGroup> |
|
|
|
<!--Bump to V3 prior to tagged release.--> |
|
|
|
<MinVerMinimumMajorMinor>3.0</MinVerMinimumMajorMinor> |
|
|
|
<!--Bump to v3.1 prior to tagged release.--> |
|
|
|
<MinVerMinimumMajorMinor>3.1</MinVerMinimumMajorMinor> |
|
|
|
</PropertyGroup> |
|
|
|
|
|
|
|
<Choose> |
|
|
|
|
|
|
|
@ -117,6 +117,11 @@ internal sealed class UniformUnmanagedMemoryPoolMemoryAllocator : MemoryAllocato |
|
|
|
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); |
|
|
|
|
|
|
|
@ -6,6 +6,7 @@ using SixLabors.ImageSharp.Advanced; |
|
|
|
using SixLabors.ImageSharp.Formats; |
|
|
|
using SixLabors.ImageSharp.Formats.Jpeg; |
|
|
|
using SixLabors.ImageSharp.Formats.Png; |
|
|
|
using SixLabors.ImageSharp.Memory; |
|
|
|
using SixLabors.ImageSharp.Metadata; |
|
|
|
using SixLabors.ImageSharp.PixelFormats; |
|
|
|
using SixLabors.ImageSharp.Tests.Memory; |
|
|
|
@ -35,6 +36,10 @@ public partial class ImageTests |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Width_Height_SizeNotRepresentable_ThrowsInvalidImageOperationException() |
|
|
|
=> Assert.Throws<InvalidMemoryOperationException>(() => new Image<Rgba32>(int.MaxValue, int.MaxValue)); |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Configuration_Width_Height() |
|
|
|
{ |
|
|
|
|
|
|
|
@ -107,6 +107,13 @@ public class UniformUnmanagedPoolMemoryAllocatorTests |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[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() |
|
|
|
{ |
|
|
|
|