Uh oh!
There was an error while loading. Please reload this page.
Fix IndexOf upper-bound guard in ImmutableArray/List - #124967
Conversation
Change the startIndex upper-bound validation in IndexOf from '< Count' to '<= Count' for ImmutableArray<T>, ImmutableArray<T>.Builder, and add upper-bound validation in ImmutableList<T>.Node. This aligns the behavior with Array.IndexOf, which permits index == Count provided count is 0.
Tagging subscribers to this area: @dotnet/area-system-collections |
There was a problem hiding this comment.
Pull request overview
This PR fixes the IndexOf upper-bound validation in ImmutableArray<T>, ImmutableArray<T>.Builder, and ImmutableList<T>.Node to align with Array.IndexOf behavior, which allows startIndex == Count when count == 0. The PR is a follow-up to #124560 which fixed similar issues in LastIndexOf.
Changes:
- Modified upper-bound validation for
startIndex/indexparameter from< Countto<= Countin IndexOf methods - Added upper-bound validation to
ImmutableList<T>.Node.IndexOfwhich was previously missing - Enhanced tests to validate parameter names in thrown exceptions and added consistency tests with
Array.IndexOf
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| ImmutableArray_1.cs | Changed startIndex upper-bound validation from < Length to <= Length in IndexOf |
| ImmutableArray_1.Builder.cs | Changed startIndex upper-bound validation from < Count to <= Count in IndexOf |
| ImmutableList_1.Node.cs | Added upper-bound check to index validation (from single >= 0 check to >= 0 && <= Count), added redundant count upper-bound validation |
| IndexOfTests.cs | Added indexParameterName parameter to test signature, enhanced tests to validate exception parameter names and added edge case tests |
| ImmutableArrayTest.cs | Added IndexOfConsistentWithArray test and updated test calls to pass "startIndex" parameter |
| ImmutableArrayBuilderTest.cs | Added IndexOfConsistentWithArray test and updated test calls to pass "startIndex" parameter |
| ImmutableListTest.cs | Added IndexOfConsistentWithArray test and updated test calls to pass "index" parameter |
| ImmutableListBuilderTest.cs | Updated test calls to pass "index" parameter |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
prozolic
commented
Apr 10, 2026
@MihaZupan I've addressed your feedback. Could you please take another look when you get a chance? |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
PR dotnet#124967 accidentally committed two lines with CRLF endings into a file that uses LF, causing git to perpetually report the file as modified on Windows (core.autocrlf=true). Renormalize to consistent LF endings. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
PR #124967 accidentally committed two lines with CRLF endings into a file that uses LF, causing git to perpetually report the file as modified on Windows (core.autocrlf=true). Renormalize to consistent LF endings. cc: @MihaZupan Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Change the
startIndexupper-bound validation inIndexOffrom '< Count' to '<= Count' forImmutableArray<T>,ImmutableArray<T>.Builder, and add upper-bound validation inImmutableList<T>.Node.This aligns the behavior with
Array.IndexOf, which permitsindex == Countprovided count is 0.Change:
startIndexupper-bound validation inIndexOffrom '< Count' to '<= Count' for ImmutableArray,ImmutableArray<T>.Builder.ImmutableList<T>.Nodeto match the behavior ofImmutableArray<T>.Fixes#124968