Skip to content

Commit 28c9376

Browse files
committed
Fix #74264: grapheme_strrpos() broken for negative offsets
We must not assume that `usearch_last()` gives the proper result for negative offsets. Instead we'd need to continue to search backwards (`usearch_previous`) until we find a proper match. However, apparently searching backwards is broken, so we work around by searching forward from the start of the string until we pass the `offset_pos`, and then use the previous result. Closes GH-7189.
1 parent b184073 commit 28c9376

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ PHP NEWS
1818
. Fixed bug #72809 (Locale::lookup() wrong result with canonicalize option).
1919
(cmb)
2020
. Fixed bug #68471 (IntlDateFormatter fails for "GMT+00:00" timezone). (cmb)
21+
. Fixed bug #74264 (grapheme_strrpos() broken for negative offsets). (cmb)
2122

2223
- OpenSSL:
2324
. Fixed bug #52093 (openssl_csr_sign truncates $serial). (cmb)

ext/intl/grapheme/grapheme_util.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,29 @@ int32_t grapheme_strpos_utf16(char *haystack, size_t haystack_len, char *needle,
179179
STRPOS_CHECK_STATUS(status, "Invalid search offset");
180180
}
181181
status = U_ZERO_ERROR;
182-
usearch_setOffset(src, offset_pos, &status);
182+
usearch_setOffset(src, last ? 0 : offset_pos, &status);
183183
STRPOS_CHECK_STATUS(status, "Invalid search offset");
184184
}
185185

186186

187187
if(last) {
188-
char_pos = usearch_last(src, &status);
189-
if(char_pos < offset_pos) {
190-
/* last one is beyound our start offset */
191-
char_pos = USEARCH_DONE;
188+
if (offset >= 0) {
189+
char_pos = usearch_last(src, &status);
190+
if(char_pos < offset_pos) {
191+
/* last one is beyond our start offset */
192+
char_pos = USEARCH_DONE;
193+
}
194+
} else {
195+
/* searching backwards is broken, so we search forwards, albeit it's less efficient */
196+
int32_t prev_pos = USEARCH_DONE;
197+
do {
198+
char_pos = usearch_next(src, &status);
199+
if (char_pos == USEARCH_DONE || char_pos > offset_pos) {
200+
char_pos = prev_pos;
201+
break;
202+
}
203+
prev_pos = char_pos;
204+
} while(1);
192205
}
193206
} else {
194207
char_pos = usearch_next(src, &status);

ext/intl/tests/bug74264.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Bug #74264 (grapheme_sttrpos() broken for negative offsets)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('intl')) die("skip intl extension not available");
6+
?>
7+
--FILE--
8+
<?php
9+
foreach (range(-5, -1) as $offset) {
10+
var_dump(
11+
grapheme_strrpos('déjàààà', 'à', $offset),
12+
grapheme_strripos('DÉJÀÀÀÀ', 'à', $offset)
13+
);
14+
}
15+
?>
16+
--EXPECT--
17+
bool(false)
18+
bool(false)
19+
int(3)
20+
int(3)
21+
int(4)
22+
int(4)
23+
int(5)
24+
int(5)
25+
int(6)
26+
int(6)

0 commit comments

Comments
 (0)