From 28c93763069c5b184b81a153cb135ccd18f62d14 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Mon, 5 Jul 2021 16:04:14 +0200 Subject: [PATCH] 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. --- NEWS | 1 + ext/intl/grapheme/grapheme_util.c | 23 ++++++++++++++++++----- ext/intl/tests/bug74264.phpt | 26 ++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 ext/intl/tests/bug74264.phpt diff --git a/NEWS b/NEWS index f03ca5fc761..0001bdf6892 100644 --- a/NEWS +++ b/NEWS @@ -18,6 +18,7 @@ PHP NEWS . Fixed bug #72809 (Locale::lookup() wrong result with canonicalize option). (cmb) . Fixed bug #68471 (IntlDateFormatter fails for "GMT+00:00" timezone). (cmb) + . Fixed bug #74264 (grapheme_strrpos() broken for negative offsets). (cmb) - OpenSSL: . Fixed bug #52093 (openssl_csr_sign truncates $serial). (cmb) diff --git a/ext/intl/grapheme/grapheme_util.c b/ext/intl/grapheme/grapheme_util.c index 5bc23be2093..8a21fa51ee4 100644 --- a/ext/intl/grapheme/grapheme_util.c +++ b/ext/intl/grapheme/grapheme_util.c @@ -179,16 +179,29 @@ 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 */ + int32_t prev_pos = USEARCH_DONE; + 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); diff --git a/ext/intl/tests/bug74264.phpt b/ext/intl/tests/bug74264.phpt new file mode 100644 index 00000000000..13826cb4b09 --- /dev/null +++ b/ext/intl/tests/bug74264.phpt @@ -0,0 +1,26 @@ +--TEST-- +Bug #74264 (grapheme_sttrpos() broken for negative offsets) +--SKIPIF-- + +--FILE-- + +--EXPECT-- +bool(false) +bool(false) +int(3) +int(3) +int(4) +int(4) +int(5) +int(5) +int(6) +int(6)