Skip to content

Commit 8071bd2

Browse files
committed
Merge branch 'PHP-7.4' into PHP-8.0
* PHP-7.4: Fix #74264: grapheme_strrpos() broken for negative offsets
2 parents d86c25d + 28c9376 commit 8071bd2

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
@@ -20,6 +20,7 @@ PHP NEWS
2020
. Fixed bug #72809 (Locale::lookup() wrong result with canonicalize option).
2121
(cmb)
2222
. Fixed bug #68471 (IntlDateFormatter fails for "GMT+00:00" timezone). (cmb)
23+
. Fixed bug #74264 (grapheme_strrpos() broken for negative offsets). (cmb)
2324

2425
- OpenSSL:
2526
. 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
@@ -157,16 +157,29 @@ int32_t grapheme_strpos_utf16(char *haystack, size_t haystack_len, char *needle,
157157
goto finish;
158158
}
159159
status = U_ZERO_ERROR;
160-
usearch_setOffset(src, offset_pos, &status);
160+
usearch_setOffset(src, last ? 0 : offset_pos, &status);
161161
STRPOS_CHECK_STATUS(status, "Invalid search offset");
162162
}
163163

164164

165165
if(last) {
166-
char_pos = usearch_last(src, &status);
167-
if(char_pos < offset_pos) {
168-
/* last one is beyound our start offset */
169-
char_pos = USEARCH_DONE;
166+
if (offset >= 0) {
167+
char_pos = usearch_last(src, &status);
168+
if(char_pos < offset_pos) {
169+
/* last one is beyond our start offset */
170+
char_pos = USEARCH_DONE;
171+
}
172+
} else {
173+
/* searching backwards is broken, so we search forwards, albeit it's less efficient */
174+
int32_t prev_pos = USEARCH_DONE;
175+
do {
176+
char_pos = usearch_next(src, &status);
177+
if (char_pos == USEARCH_DONE || char_pos > offset_pos) {
178+
char_pos = prev_pos;
179+
break;
180+
}
181+
prev_pos = char_pos;
182+
} while(1);
170183
}
171184
} else {
172185
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)