ImmutableArray.Create throws OutOfMemoryException instead of ArgumentOutOfRangeException on integer overflow
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
Tagging subscribers to this area: @dotnet/area-system-collections See info in area-owners.md if you want to be subscribed.
Fixed by #124042.