Skip to content

Fix integer overflow in string.IndexOf range validation#128191

Merged
MihaZupan merged 1 commit into
dotnet:mainfrom
prozolic:stringindexof
May 14, 2026
Merged

Fix integer overflow in string.IndexOf range validation#128191
MihaZupan merged 1 commit into
dotnet:mainfrom
prozolic:stringindexof

Conversation

@prozolic
Copy link
Copy Markdown
Contributor

@prozolic prozolic commented May 14, 2026

Closes #128192
This PR change string.IndexOf range validation from count + startIndex > Length to count > Length - startIndex to avoid the overflow.
This ensures an ArgumentOutOfRangeException is thrown when startIndex + count exceeds int.MaxValue when comparisonType is StringComparison.OrdinalIgnoreCase.

Also applied the same fix to IndexOf(Rune, int, int, StringComparison) and updated it to use ArgumentOutOfRangeException.ThrowIfNegative and ArgumentOutOfRangeException.ThrowIfGreaterThan consistently with the char overload.

The validation `startIndex + count > Length` can overflow
when comparisonType is OrdinalIgnoreCase and
startIndex + count exceeds int.MaxValue. Change it to
`count > Length - startIndex` to avoid the overflow.

This ensures an ArgumentOutOfRangeException is thrown when
startIndex + count exceeds int.MaxValue.

Also update IndexOf(Rune, int, int, StringComparison) to
use ThrowIfNegative and ThrowIfGreaterThan consistently
with the char overload.
Copilot AI review requested due to automatic review settings May 14, 2026 12:27
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label May 14, 2026
Copy link
Copy Markdown
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

Note

Copilot was unable to run its full agentic suite in this review.

Updates string.IndexOf argument validation to avoid overflow and produce more accurate ArgumentOutOfRangeException parameter names, with added regression tests.

Changes:

  • Adjusted startIndex/count range checks to use count > Length - startIndex instead of startIndex + count > Length.
  • Added tests asserting correct ArgumentOutOfRangeException parameter names for IndexOf overloads with StringComparison.OrdinalIgnoreCase and Rune.

Reviewed changes

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

File Description
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/StringTests.cs Adds regression tests for argument validation/paramName behavior in IndexOf overloads.
src/libraries/System.Private.CoreLib/src/System/String.Searching.cs Fixes validation to be overflow-safe and to surface correct failing parameter names (startIndex vs count).

@dotnet-policy-service
Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Member

@MihaZupan MihaZupan left a comment

Choose a reason for hiding this comment

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

Thanks

Looks like tons of these methods also skip all arg validation if the string is empty for some reason.

Btw you can do Closes #123 in the PR description so that the issue gets automatically closed when a PR is merged

@MihaZupan MihaZupan enabled auto-merge (squash) May 14, 2026 13:04
@MihaZupan MihaZupan added this to the 11.0.0 milestone May 14, 2026
@prozolic
Copy link
Copy Markdown
Contributor Author

Btw you can do Closes #123 in the PR description so that the issue gets automatically closed when a PR is merged

@MihaZupan Thank you! I didn't realize that's how Closes works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Runtime 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.

string.IndexOf returns an incorrect value in some cases when comparisonType is StringComparison.OrdinalIgnoreCase

3 participants