Skip to content

CompareInfo.LastIndexOf does not handle zero-weight sequences correctly on ICU #44439

Closed

Description

@GrabYourPitchforks

Found by @gewarren, related to #34828.

Repro code on net5.0 on ICU:

var ci = CultureInfo.InvariantCulture.CompareInfo;
Console.WriteLine(ci.LastIndexOf("hello", "")); // Prints 5 (correct)
Console.WriteLine(ci.LastIndexOf("hello", "\0")); // Prints 4, but correct answer is 5
Console.WriteLine(ci.LastIndexOf("hello", "\u200D")); // Prints 4, but correct answer is 5

And net5.0 on NLS:

var ci = CultureInfo.InvariantCulture.CompareInfo;
Console.WriteLine(ci.LastIndexOf("hello", "")); // Prints 5 (correct)
Console.WriteLine(ci.LastIndexOf("hello", "\0")); // Prints -1 (correct, null is not weightless in NLS)
Console.WriteLine(ci.LastIndexOf("hello", "\u200D")); // Prints 5 (correct)

It's possible we have an off-by-one error somewhere in our ICU glue.

The reason all of these methods should return 5 is that "", "\0", and "\u200D" are all equivalent zero-weight strings, and zero-weight strings always trivially exist as a suffix of all possible substrings. (In NLS, the null character U+0000 is not weightless, hence the return value of -1 is correct in that scenario.)

Activity

  1. added
    untriaged
    on Nov 10, 2020
  2. removed
    untriaged
    on Nov 10, 2020
  3. added this to the 6.0.0 milestone on Nov 10, 2020
  4. ghost added
    in-pr
    on Jul 28, 2021
  5. ghost removed
    in-pr
    on Jul 31, 2021
  6. ghost locked as resolved and limited conversation to collaborators on Aug 30, 2021
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