[intl] Fix empty-needle grapheme_strpos offsets - #263
Open
iliaal wants to merge 1 commit into
Open
Conversation
grapheme_strpos_utf16() returned raw UTF-16 code-unit positions for an empty needle instead of grapheme counts, so multi-code-unit graphemes made strpos()/strrpos() over-report the offset. Convert the boundary position like the non-empty search path; sibling audit found the ASCII fast paths and the strstr() UTF-16 position consumer are unaffected. Signed-off-by: Ilia Alshanetsky <ilia@ilia.ws>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
grapheme_strpos_utf16() handled an empty needle by returning the raw UTF-16 code-unit position of the search start (or the full UTF-16 haystack length for strrpos with a non-negative offset), while the non-empty path converts the boundary position back to a grapheme count, so for haystacks containing multi-code-unit graphemes like strpos() on a string with an emoji the functions reported UTF-16 offsets instead of grapheme offsets. The fix converts both empty-needle return values through grapheme_count_graphemes() exactly like the non-empty search path does; the ASCII fast paths are unaffected because byte and grapheme offsets coincide there, and grapheme_strstr() consumes the separate raw UTF-16 position which remains correct.