Skip to content

Fix #74264: grapheme_sttrpos() broken for negative offsets #7189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions ext/intl/grapheme/grapheme_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void grapheme_substr_ascii(char *str, size_t str_len, int32_t f, int32_t l, char
int32_t grapheme_strpos_utf16(char *haystack, size_t haystack_len, char *needle, size_t needle_len, int32_t offset, int32_t *puchar_pos, int f_ignore_case, int last)
{
UChar *uhaystack = NULL, *uneedle = NULL;
int32_t uhaystack_len = 0, uneedle_len = 0, char_pos, ret_pos, offset_pos = 0;
int32_t uhaystack_len = 0, uneedle_len = 0, char_pos, ret_pos, offset_pos = 0, prev_pos = USEARCH_DONE;
unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE];
UBreakIterator* bi = NULL;
UErrorCode status;
Expand Down Expand Up @@ -179,16 +179,28 @@ int32_t grapheme_strpos_utf16(char *haystack, size_t haystack_len, char *needle,
STRPOS_CHECK_STATUS(status, "Invalid search offset");
}
status = U_ZERO_ERROR;
usearch_setOffset(src, offset_pos, &status);
usearch_setOffset(src, last ? 0 : offset_pos, &status);
STRPOS_CHECK_STATUS(status, "Invalid search offset");
}


if(last) {
char_pos = usearch_last(src, &status);
if(char_pos < offset_pos) {
/* last one is beyound our start offset */
char_pos = USEARCH_DONE;
if (offset >= 0) {
char_pos = usearch_last(src, &status);
if(char_pos < offset_pos) {
/* last one is beyond our start offset */
char_pos = USEARCH_DONE;
}
} else {
/* searching backwards is broken, so we search forwards, albeit it's less efficient */
do {
char_pos = usearch_next(src, &status);
if (char_pos == USEARCH_DONE || char_pos > offset_pos) {
char_pos = prev_pos;
break;
}
prev_pos = char_pos;
} while(1);
}
} else {
char_pos = usearch_next(src, &status);
Expand Down
26 changes: 26 additions & 0 deletions ext/intl/tests/bug74264.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Bug #74264 (grapheme_sttrpos() broken for negative offsets)
--SKIPIF--
<?php
if (!extension_loaded('intl')) die("skip intl extension not available");
?>
--FILE--
<?php
foreach (range(-5, -1) as $offset) {
var_dump(
grapheme_strrpos('déjàààà', 'à', $offset),
grapheme_strripos('DÉJÀÀÀÀ', 'à', $offset)
);
}
?>
--EXPECT--
bool(false)
bool(false)
int(3)
int(3)
int(4)
int(4)
int(5)
int(5)
int(6)
int(6)