runtime icon indicating copy to clipboard operation
runtime copied to clipboard

ImmutableArray.Create throws OutOfMemoryException instead of ArgumentOutOfRangeException on integer overflow

Open prozolic opened this issue 9 months ago • 2 comments

Description

In ImmutableArray.Create and ImmutableArray.CreateRange, there were cases where an OutOfMemoryException was thrown when validating start + length. For example, when start=1 and length=int.MaxValue, an overflow occurred and passed the range validation, resulting in an OutOfMemoryException during array allocation.

Reproduction Steps

int[] array = [1, 2, 3, 4, 5];
var immutableArray = ImmutableArray.Create(array, 1, int.MaxValue);

Expected behavior

ArgumentOutOfRangeException is thrown.

Actual behavior

OutOfMemoryException is thrown.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

A fix has been submitted in #124042. This fix is a potential Bucket 3 breaking change per the breaking change guidelines.

Behavior Change:

  • Before: start=1, length=int.MaxValue → OutOfMemoryException
  • After: start=1, length=int.MaxValue → ArgumentOutOfRangeException

prozolic avatar Feb 05 '26 13:02 prozolic

Tagging subscribers to this area: @dotnet/area-system-collections See info in area-owners.md if you want to be subscribed.

dotnet-policy-service avatar Feb 05 '26 15:02 dotnet-policy-service

Fixed by #124042.

martincostello avatar Feb 16 '26 16:02 martincostello