Skip to content

Commit 148f322

Browse files
committed
Checking for maxint allocationid
1 parent bfb09f5 commit 148f322

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

UnmanagedStringPool.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ public sealed class UnmanagedStringPool : IDisposable
5050
private int totalFreeBlocks; // Total number of free blocks in the pool
5151
private int totalFreeBytes; // Running total of free bytes to avoid recalculation
5252

53-
// Index free blocks by size for faster allocation
53+
// Index free blocks by size for faster allocation, keyed by block size
5454
private readonly SortedList<int, List<FreeBlock>> freeBlocksBySize = new(DefaultCollectionSize);
5555

56-
// Central registry of allocated strings
56+
// Central registry of allocated , keyed by allocation ID
5757
private readonly Dictionary<int, AllocationInfo> allocations = new(DefaultCollectionSize);
5858

5959
/// <summary>
@@ -387,6 +387,11 @@ private static int AlignSize(int sizeBytes)
387387
[MethodImpl(MethodImplOptions.AggressiveInlining)]
388388
private PooledString RegisterAllocation(IntPtr ptr, int lengthChars, int offset)
389389
{
390+
if (lastAllocationId == int.MaxValue) {
391+
// in the unlikely event we reach MaxValue (2 billion+ allocations) we cannot allocate more
392+
throw new OutOfMemoryException("Allocation overflow");
393+
}
394+
390395
++lastAllocationId;
391396
allocations[lastAllocationId] = new(ptr, lengthChars, offset);
392397
return new(this, lastAllocationId);

0 commit comments

Comments
 (0)