Skip to content

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

@prozolic

Description

@prozolic

Description

In string.IndexOf, when comparisonType is StringComparison.OrdinalIgnoreCase and startIndex + count causes an integer overflow, an ArgumentOutOfRangeException is not thrown. Instead, it returns an incorrect value.
If comparisonType is anything other than StringComparison.OrdinalIgnoreCase, ArgumentOutOfRangeException is is thrown.

Reproduction Steps

// comparisonType is StringComparison.OrdinalIgnoreCase
var result = "Hello".IndexOf('o', 1, int.MaxValue, StringComparison.OrdinalIgnoreCase); // return 4 (should throw ArgumentOutOfRangeException)

// comparisonType is StringComparison.Ordinal
var result = "Hello".IndexOf('o', 1, int.MaxValue, StringComparison.Ordinal); // throw ArgumentOutOfRangeException

Expected behavior

ArgumentOutOfRangeException is thrown.

Actual behavior

incorrect value is returned.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

This is an edge case, I have submitted a #128191

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions