Skip to content

Stop GetRange's range check overflowing for large counts - #68

Merged
matt-edmondson merged 1 commit into
mainfrom
claude/containers-66-getrange-overflow
Sep 26, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
claude/containers-66-getrange-overflow

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #66

Problem

GetRange in ContiguousCollection, InsertionOrderCollection and OrderedCollection checked startIndex + count > Count. For a large count, the sum overflows to a negative number and the guard passes:

  • ContiguousCollection allocated an int.MaxValue array and threw OutOfMemoryException.
  • The List<T>-backed collections threw ArgumentException from List<T>.GetRange, not the documented ArgumentOutOfRangeException.

Fix

All three collections now check count > Count - startIndex. This comparison cannot overflow because both values are already known to be non-negative. GetRange(Count, 0) still returns an empty collection (#59).

Tests

  • GetRange_CountOverflowingStartIndex_ThrowsArgumentOutOfRangeException is new in all three test classes. It calls GetRange(1, int.MaxValue).
  • GetRange_ZeroCountAtEnd_ReturnsEmpty is added for ContiguousCollection and InsertionOrderCollection. OrderedCollection already had it.

With the fix reverted, the 3 overflow tests fail. With it, the full suite passes (378/378).

This PR is independent of #63, #64 and #67; all of them branch from main.

🤖 Generated with Claude Code

https://claude.ai/code/session_012Hhb8knddbu4LRSs2uu9jR


Generated by Claude Code

`startIndex + count` overflowed to a negative number when count was
large, so the guard passed. ContiguousCollection then tried to allocate
an int.MaxValue array, and the List-backed collections threw
ArgumentException instead of the documented ArgumentOutOfRangeException.
Compare count against `Count - startIndex`, which cannot overflow.

Fixes #66

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012Hhb8knddbu4LRSs2uu9jR
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 8eeb8aa into main Sep 26, 2026
12 checks passed
@matt-edmondson
matt-edmondson deleted the claude/containers-66-getrange-overflow branch September 26, 2026 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GetRange range check overflows for large counts: ContiguousCollection tries to allocate an int.MaxValue array

2 participants