Skip to content

ImmutableArray.Create throws OutOfMemoryException instead of ArgumentOutOfRangeException on integer overflow #124043

@prozolic

Description

@prozolic

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.MaxValueOutOfMemoryException
  • After: start=1, length=int.MaxValueArgumentOutOfRangeException

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions