Skip to content

Comments

Fix integer overflow in ImmutableArray range validation#124042

Merged
eiriktsarpalis merged 5 commits intodotnet:mainfrom
prozolic:immutablearraycreate
Feb 9, 2026
Merged

Fix integer overflow in ImmutableArray range validation#124042
eiriktsarpalis merged 5 commits intodotnet:mainfrom
prozolic:immutablearraycreate

Conversation

@prozolic
Copy link
Contributor

@prozolic prozolic commented Feb 5, 2026

#124043
Prevent OutOfMemoryException by detecting integer overflow when validating start + length parameters. Previously, when start=1 and length=int.MaxValue, the addition would overflow to a negative value, incorrectly passing validation and causing OutOfMemoryException during array allocation.

Prevent OutOfMemoryException by detecting integer overflow when
validating start + length parameters. Previously, when start=1 and
length=int.MaxValue, the addition would overflow to a negative value,
incorrectly passing validation and causing OutOfMemoryException during
array allocation.
Copilot AI review requested due to automatic review settings February 5, 2026 13:47
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Feb 5, 2026
@dotnet-policy-service
Copy link
Contributor

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

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request fixes an integer overflow vulnerability in ImmutableArray range validation that could cause OutOfMemoryException. When start=1 and length=int.MaxValue, the addition start + length would overflow to a negative value, incorrectly passing validation and causing OutOfMemoryException during array allocation.

Changes:

  • Updated range validation in 4 methods to use unsigned arithmetic for overflow detection
  • Added comprehensive test coverage for the overflow scenario in all 4 affected methods

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray.cs Fixed integer overflow in range validation by casting start + length to uint for comparison in 4 methods: Create(T[], int, int), Create(ImmutableArray, int, int), CreateRange with selector, and CreateRange with selector and argument
src/libraries/System.Collections.Immutable/tests/ImmutableArrayTest.cs Added test cases for the overflow scenario (start=1, length=int.MaxValue) in 4 test methods corresponding to the fixed methods, each properly guarded with array.Length > 0 check

@prozolic prozolic marked this pull request as ready for review February 5, 2026 14:00
Copilot AI review requested due to automatic review settings February 5, 2026 14:00
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@eiriktsarpalis eiriktsarpalis merged commit 93df345 into dotnet:main Feb 9, 2026
91 of 93 checks passed
@prozolic prozolic deleted the immutablearraycreate branch February 9, 2026 22:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Collections community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants