Skip to content

Conversation

@prozolic
Copy link
Contributor

#121673

LengthBuckets.CreateLengthBucketsArrayIfAppropriate caused an integer overflow when calculating bucket array size for very large strings, resulting in a negative value being passed to ArrayPool.Shared.Rent. This triggered an ArgumentOutOfRangeException when creating a FrozenDictionary with large strings approaching Array.MaxLength.

Add an overflow check to prevent creating buckets when the calculation would overflow.

LengthBuckets.CreateLengthBucketsArrayIfAppropriate caused an
integer overflow when calculating bucket array size for very large
strings, resulting in a negative value being passed to
ArrayPool<T>.Shared.Rent. This triggered an ArgumentOutOfRangeException
when creating a FrozenDictionary with large strings approaching Array.MaxLength.

Add an overflow check to prevent creating buckets when the calculation
would overflow.
Copilot AI review requested due to automatic review settings November 16, 2025 00:21
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Nov 16, 2025
@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.

Copilot finished reviewing on behalf of prozolic November 16, 2025 00:34
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 PR fixes an integer overflow bug in LengthBuckets.CreateLengthBucketsArrayIfAppropriate that occurred when creating FrozenDictionaries with very large strings. The overflow happened during calculation of the bucket array size, causing a negative value to be passed to ArrayPool<T>.Shared.Rent, which resulted in an ArgumentOutOfRangeException.

Key Changes:

  • Changed intermediate array size calculation from int to long to prevent overflow
  • Added explicit cast to long for the multiplication operation before bounds checking
  • Safely cast back to int only after validating the result is within acceptable bounds

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.

1 participant