Skip to content

CompareInfo.IndexOf / LastIndexOf early-exit code paths do not account for zero-weight characters #34828

Closed

Description

@GrabYourPitchforks

Repro code:

Console.WriteLine(CultureInfo.InvariantCulture.CompareInfo.IndexOf(string.Empty, "\u200c"));

This should print 0 on all platforms unless we're running under the invariant globalization mode, in which case it should return -1. The reason for this is an incorrect early-exit in CompareInfo.IndexOf, as shown below.

if (source.Length == 0)
{
if (value.Length == 0)
{
return 0;
}
return -1;
}

This early exit does not account for the fact that empty strings and non-empty strings may compare as equal under certain collations if the non-empty strings consist only of zero-weight code points.

Activity

  1. added this to the 5.0 milestone on Apr 10, 2020
  2. added
    untriaged
    on Apr 10, 2020
  3. removed
    untriaged
    on Apr 10, 2020
  4. tarekgh commented on Apr 10, 2020

    @tarekgh
    Member

    @GrabYourPitchforks thanks for filing it. just wondering if you'll get the fix? or expect someone else to do it?

  5. GrabYourPitchforks commented on Apr 10, 2020

    @GrabYourPitchforks
    MemberAuthor

    The fix is included in #1514. I'm hoping to get a new iteration of that out by the end of today.

  6. GrabYourPitchforks commented on Apr 21, 2020

    @GrabYourPitchforks
    MemberAuthor

    A quick note: there is a difference between NLS and ICU here.

    Consider a call to "hello".LastIndexOf("\u200d") (using a linguistic comparer). NLS's FindNLSStringEx will return an index of 5. ICU's usearch_last will return an index of 4. As far as I can tell this only happens with weightless code points. I'm not going to try to work around this in the .NET Core layer, but I'll update the unit tests to account for the target environment and to change the expectation.

  7. tarekgh commented on Apr 21, 2020

    @tarekgh
    Member

    @GrabYourPitchforks

    I'm not going to try to work around this in the .NET Core layer

    Any reason why we cannot do that?

  8. GrabYourPitchforks commented on Apr 21, 2020

    @GrabYourPitchforks
    MemberAuthor

    Any reason why we cannot do that?

    We're not doing it in other places. For example, on NLS "\u200dhello".IndexOf("hello") returns 1, but logically it should really return 0 (since "\u200dhello".StartsWith("hello") returns true). I think the risk of trying to capture all of these edge cases makes it more likely that we'll introduce bugs in our own code. It'll definitely slow down the common path.

  9. tarekgh commented on Apr 21, 2020

    @tarekgh
    Member

    ok. thanks!

  10. ghost locked as resolved and limited conversation to collaborators on Dec 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions