Skip to content

[intl] Fix empty-needle grapheme_strpos offsets - #263

Open
iliaal wants to merge 1 commit into
PHP-8.4from
fix/grapheme-empty-offset-84
Open

[intl] Fix empty-needle grapheme_strpos offsets#263
iliaal wants to merge 1 commit into
PHP-8.4from
fix/grapheme-empty-offset-84

Conversation

@iliaal

@iliaal iliaal commented Aug 24, 2026

Copy link
Copy Markdown
Owner

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.

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>
@iliaal iliaal closed this Aug 24, 2026
@iliaal iliaal reopened this Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant