Skip to content

Commit 9b24cc1

Browse files
Merge pull request #2545 from SixLabors/af/fix-memorygroup-overallocation
Disallow allocation attempts of unrepresentable sizes
2 parents 8c1113e + 2302ec3 commit 9b24cc1

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

src/ImageSharp/ImageSharp.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
</PropertyGroup>
2323

2424
<PropertyGroup>
25-
<!--Bump to V3 prior to tagged release.-->
26-
<MinVerMinimumMajorMinor>3.0</MinVerMinimumMajorMinor>
25+
<!--Bump to v3.1 prior to tagged release.-->
26+
<MinVerMinimumMajorMinor>3.1</MinVerMinimumMajorMinor>
2727
</PropertyGroup>
2828

2929
<Choose>

src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ internal override MemoryGroup<T> AllocateGroup<T>(
117117
AllocationOptions options = AllocationOptions.None)
118118
{
119119
long totalLengthInBytes = totalLength * Unsafe.SizeOf<T>();
120+
if (totalLengthInBytes < 0)
121+
{
122+
throw new InvalidMemoryOperationException("Attempted to allocate a MemoryGroup of a size that is not representable.");
123+
}
124+
120125
if (totalLengthInBytes <= this.sharedArrayPoolThresholdInBytes)
121126
{
122127
var buffer = new SharedArrayPoolBuffer<T>((int)totalLength);

tests/ImageSharp.Tests/Image/ImageTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using SixLabors.ImageSharp.Formats;
77
using SixLabors.ImageSharp.Formats.Jpeg;
88
using SixLabors.ImageSharp.Formats.Png;
9+
using SixLabors.ImageSharp.Memory;
910
using SixLabors.ImageSharp.Metadata;
1011
using SixLabors.ImageSharp.PixelFormats;
1112
using SixLabors.ImageSharp.Tests.Memory;
@@ -35,6 +36,10 @@ public void Width_Height()
3536
}
3637
}
3738

39+
[Fact]
40+
public void Width_Height_SizeNotRepresentable_ThrowsInvalidImageOperationException()
41+
=> Assert.Throws<InvalidMemoryOperationException>(() => new Image<Rgba32>(int.MaxValue, int.MaxValue));
42+
3843
[Fact]
3944
public void Configuration_Width_Height()
4045
{

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ public void AllocateGroup_MultipleTimes_ExceedPoolLimit()
107107
}
108108
}
109109

110+
[Fact]
111+
public void AllocateGroup_SizeInBytesOverLongMaxValue_ThrowsInvalidMemoryOperationException()
112+
{
113+
var allocator = new UniformUnmanagedMemoryPoolMemoryAllocator(null);
114+
Assert.Throws<InvalidMemoryOperationException>(() => allocator.AllocateGroup<S4>(int.MaxValue * (long)int.MaxValue, int.MaxValue));
115+
}
116+
110117
[Fact]
111118
public unsafe void Allocate_MemoryIsPinnableMultipleTimes()
112119
{

0 commit comments

Comments
 (0)