[php-src] PHP-8.4: ext/intl: Fix grapheme_strstr() and grapheme_stristr() match offsets after supplementary characters (#24012)

From: Date: Wed, 30 Sep 2026 16:28:44 +0000
Subject: [php-src] PHP-8.4: ext/intl: Fix grapheme_strstr() and grapheme_stristr() match offsets after supplementary characters (#24012)
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-141804@lists.php.net to get a copy of this message
Author: Weilin Du (LamentXU123) Committer: GitHub (web-flow) Pusher: LamentXU123 Date: 2026-10-01T00:28:40+08:00 Commit: https://github.com/php/php-src/commit/f2a59c14b951e41759aafb4844829e645b9b9edd Raw diff: https://github.com/php/php-src/commit/f2a59c14b951e41759aafb4844829e645b9b9edd.diff ext/intl: Fix grapheme_strstr() and grapheme_stristr() match offsets after supplementary characters (#24012) Convert the UTF-16 match offset to a code point count before using it to locate the UTF-8 substring in grapheme_strstr() and grapheme_stristr(). Add regression tests for supplementary characters and combining sequences, covering both functions and both beforeNeedle modes. Changed paths: A ext/intl/tests/grapheme_strstr_supplementary_prefix.phpt M NEWS M ext/intl/grapheme/grapheme_util.c Diff: diff --git a/NEWS b/NEWS index 2aa1831288a1..4b9cd689113d 100644 --- a/NEWS +++ b/NEWS @@ -65,6 +65,8 @@ PHP NEWS (Ilia Alshanetsky) . Fixed Collator attribute and strength methods not rejecting an unconstructed Collator. (Ilia Alshanetsky) + . Fixed grapheme_strstr() and grapheme_stristr() returning incorrect substrings + when supplementary characters precede the match. (Weilin Du) - Lexbor: . Merge patches lexbor/lexbor@8a14bc0 and lexbor/lexbor@f67ce4b, fixing a diff --git a/ext/intl/grapheme/grapheme_util.c b/ext/intl/grapheme/grapheme_util.c index f0c79785483f..66c3876a5bd2 100644 --- a/ext/intl/grapheme/grapheme_util.c +++ b/ext/intl/grapheme/grapheme_util.c @@ -190,7 +190,7 @@ int32_t grapheme_strpos_utf16(char *haystack, size_t haystack_len, char *needle, if(char_pos != USEARCH_DONE && ubrk_isBoundary(bi, char_pos)) { ret_pos = grapheme_count_graphemes(bi, uhaystack,char_pos); if(puchar_pos) { - *puchar_pos = char_pos; + *puchar_pos = u_countChar32(uhaystack, char_pos); } } else { ret_pos = -1; diff --git a/ext/intl/tests/grapheme_strstr_supplementary_prefix.phpt b/ext/intl/tests/grapheme_strstr_supplementary_prefix.phpt new file mode 100644 index 000000000000..f9f85880604a --- /dev/null +++ b/ext/intl/tests/grapheme_strstr_supplementary_prefix.phpt @@ -0,0 +1,27 @@ +--TEST-- +grapheme_strstr() and grapheme_stristr() preserve match boundaries after supplementary characters +--EXTENSIONS-- +intl +--FILE-- +<?php +foreach (["\u{1F600}", "\u{1F600}\u{1F600}", "\u{1F600}e\u{0301}"] as $prefix) { + $haystack = $prefix . 'abc'; + var_dump(grapheme_strstr($haystack, 'a')); + var_dump(grapheme_strstr($haystack, 'a', true)); + var_dump(grapheme_stristr($haystack, 'A')); + var_dump(grapheme_stristr($haystack, 'A', true)); +} +?> +--EXPECT-- +string(3) "abc" +string(4) "šŸ˜€" +string(3) "abc" +string(4) "šŸ˜€" +string(3) "abc" +string(8) "šŸ˜€šŸ˜€" +string(3) "abc" +string(8) "šŸ˜€šŸ˜€" +string(3) "abc" +string(7) "šŸ˜€é" +string(3) "abc" +string(7) "šŸ˜€é"

« previous php.cvs (#141804) next »