[php-src] PHP-8.4: ext/intl: Fix overlapping grapheme matches with negative search offsets (#24028)

From: Date: Fri, 02 Oct 2026 16:10:40 +0000
Subject: [php-src] PHP-8.4: ext/intl: Fix overlapping grapheme matches with negative search offsets (#24028)
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-141884@lists.php.net to get a copy of this message
Author: Weilin Du (LamentXU123) Committer: GitHub (web-flow) Pusher: LamentXU123 Date: 2026-10-03T00:10:37+08:00 Commit: https://github.com/php/php-src/commit/be5442d56fba89e682d639110e1202fec93e7ade Raw diff: https://github.com/php/php-src/commit/be5442d56fba89e682d639110e1202fec93e7ade.diff ext/intl: Fix overlapping grapheme matches with negative search offsets (#24028) Changed paths: A ext/intl/tests/grapheme_strrpos_negative_offset_overlap.phpt M NEWS M ext/intl/grapheme/grapheme_util.c Diff: diff --git a/NEWS b/NEWS index 99df5910c8b5..fb0f05f502b5 100644 --- a/NEWS +++ b/NEWS @@ -74,6 +74,8 @@ PHP NEWS . Fixed grapheme_strstr() and grapheme_strpos() missing canonically equivalent matches, and grapheme searches reporting ICU errors for empty haystacks. (Weilin Du) + . Fixed grapheme_strrpos() and grapheme_strripos() skipping overlapping matches + when using a negative offset. (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 dbe80d962765..cd337055d45d 100644 --- a/ext/intl/grapheme/grapheme_util.c +++ b/ext/intl/grapheme/grapheme_util.c @@ -184,6 +184,12 @@ int32_t grapheme_strpos_utf16(char *haystack, size_t haystack_len, char *needle, break; } prev_pos = char_pos; + int32_t next_pos = ubrk_following(bi, char_pos); + if (next_pos == UBRK_DONE) { + break; + } + usearch_setOffset(src, next_pos, &status); + STRPOS_CHECK_STATUS(status, "Invalid search offset"); } while(1); } } else { diff --git a/ext/intl/tests/grapheme_strrpos_negative_offset_overlap.phpt b/ext/intl/tests/grapheme_strrpos_negative_offset_overlap.phpt new file mode 100644 index 000000000000..aa7fb349ba4d --- /dev/null +++ b/ext/intl/tests/grapheme_strrpos_negative_offset_overlap.phpt @@ -0,0 +1,32 @@ +--TEST-- +grapheme_strrpos() and grapheme_strripos() find overlapping matches with negative offsets +--EXTENSIONS-- +intl +--FILE-- +<?php +foreach (['a', "\u{00E9}", "e\u{0301}", "\u{1F600}"] as $grapheme) { + foreach (['grapheme_strrpos', 'grapheme_strripos'] as $function) { + $positions = []; + foreach ([0, 1, -1, -2, -3] as $offset) { + $positions[] = $function(str_repeat($grapheme, 3), str_repeat($grapheme, 2), $offset); + } + echo $function, ': ', implode(', ', $positions), "\n"; + } +} + +var_dump(grapheme_strripos("\u{00C9}\u{00E9}\u{00C9}", "\u{00E9}\u{00E9}", -1)); +var_dump(grapheme_strrpos("\u{00E9}\u{00E9}x\u{00E9}\u{00E9}", "\u{00E9}\u{00E9}", -1)); +var_dump(grapheme_strrpos("\u{00E9}\u{00E9}x\u{00E9}\u{00E9}", "\u{00E9}\u{00E9}", -3)); +?> +--EXPECT-- +grapheme_strrpos: 1, 1, 1, 1, 0 +grapheme_strripos: 1, 1, 1, 1, 0 +grapheme_strrpos: 1, 1, 1, 1, 0 +grapheme_strripos: 1, 1, 1, 1, 0 +grapheme_strrpos: 1, 1, 1, 1, 0 +grapheme_strripos: 1, 1, 1, 1, 0 +grapheme_strrpos: 1, 1, 1, 1, 0 +grapheme_strripos: 1, 1, 1, 1, 0 +int(1) +int(3) +int(0)

« previous php.cvs (#141884) next »