Fix LINQ Append/Prepend GetCount overflow to throw OverflowException#123821
Merged
stephentoub merged 3 commits intomainfrom Jan 31, 2026
Merged
Fix LINQ Append/Prepend GetCount overflow to throw OverflowException#123821stephentoub merged 3 commits intomainfrom
stephentoub merged 3 commits intomainfrom
Conversation
…flow tests Co-authored-by: vcsjones <361677+vcsjones@users.noreply.github.com>
vcsjones
reviewed
Jan 30, 2026
…arrays Co-authored-by: vcsjones <361677+vcsjones@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix overflow in LINQ Append and Prepend GetCount optimization
Fix LINQ Append/Prepend GetCount overflow to throw OverflowException
Jan 30, 2026
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-linq |
This was referenced Jan 31, 2026
stephentoub
reviewed
Jan 31, 2026
stephentoub
approved these changes
Jan 31, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a silent integer overflow bug in LINQ's Append and Prepend operations where counting sequences with int.MaxValue elements would return negative counts instead of throwing OverflowException, which is the expected behavior consistent with Concat.
Changes:
- Added
checkedarithmetic toGetCountmethods in bothAppendPrepend1IteratorandAppendPrependNclasses to throwOverflowExceptionon overflow - Added 6 comprehensive overflow test cases covering both iterator types and different source types (Iterator vs ICollection)
- Introduced
MockCollection<T>test helper to test overflow scenarios without allocating memory
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/System.Linq/src/System/Linq/AppendPrepend.SpeedOpt.cs | Added checked arithmetic to 4 count calculations in GetCount methods to ensure overflow throws OverflowException |
| src/libraries/System.Linq/tests/AppendPrependTests.cs | Added MockCollection<T> helper class and 6 overflow test cases covering all overflow scenarios in both AppendPrepend1Iterator and AppendPrependN |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
AppendandPrependspeed optimizations silently overflow when counting sequences withint.MaxValueelements, returning negative counts instead of throwingOverflowException. This diverges fromConcat, which correctly throws on overflow.Note: This issue only affects the synchronous
System.Linqlibrary.System.Linq.AsyncEnumerabledoes not have equivalentGetCountoptimizations and already handles overflow correctly inCountAsync.Changes
checkedarithmetic toGetCountin bothAppendPrepend1IteratorandAppendPrependNMockCollection<T>to avoid memory allocationTesting
All 28 tests in
AppendPrependTestspass, including the 6 new overflow tests.Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.