Add USEARCH_DONE check to StartsWith in System.Globalization.Native
authorstephentoub <stoub@microsoft.com>
Sun, 15 Nov 2015 13:03:26 +0000 (08:03 -0500)
committerstephentoub <stoub@microsoft.com>
Sun, 15 Nov 2015 13:03:26 +0000 (08:03 -0500)
commiteb470c92fcd71beb7e34ceaa199758cb44a5a4e4
treeec43fae968ea5f407464c7489a0746c2d40af6f0
parentf8b864158f4e674791706feda765bb90c9003b22
Add USEARCH_DONE check to StartsWith in System.Globalization.Native

The StartsWith ICU wrapper was not checking the result of usearch_first
to see if it was USEARCH_DONE, indicating no match found.  This has two
ramifications:
1. When there isn't a match, USEARCH_DONE (-1) gets passed in as the
textLength argument to ucol_openElements, which treats -1 as meaning
the string isn't null-terminated, and thus ends up walking the string
looking for non-ignorable collation elements.  Our tests have been passing
because they've been using strings containing only non-ignorable
elements, and thus the first character checked causes us to bail and correctly
return false.  If nothing else, this is an unnecessary perf overhead.
2. But on top of that if there are only ignorable collation elements
before the first null character in the string (e.g. if the string begins
with a null character), then because we told ICU that the string ended
at the first null character, it'll stop walking the string and return
a match. e.g. "\0bug".StartsWith("test") returns true incorrectly.

This commit simply adds a check for USEARCH_DONE to StartsWith.
EndsWith already has such a check.
src/corefx/System.Globalization.Native/collation.cpp