From ae9db77325abc0b4169b6bd0ac102c0e263cf3b7 Mon Sep 17 00:00:00 2001 From: lazerg Date: Fri, 7 Aug 2026 23:01:47 +0500 Subject: [PATCH] Fix GH-23106: mb_strpos() reads past the end of a truncated UTF-8 haystack --- NEWS | 2 ++ ext/mbstring/mbstring.c | 3 +++ ext/mbstring/tests/gh23106.phpt | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 ext/mbstring/tests/gh23106.phpt diff --git a/NEWS b/NEWS index ba832ef05888..84ef871ff953 100644 --- a/NEWS +++ b/NEWS @@ -17,6 +17,8 @@ PHP NEWS offset in a non-UTF-8 encoding). (Eyüp Can Akman) . Fixed bug GH-21036 (mb_ereg_search_getregs() crashes after mb_eregi() invalidates the regex cache). (Matthias Goergens) + . Fixed bug GH-23106 (mb_strpos() reads past the end of a haystack ending in + a truncated UTF-8 sequence). (Lazizbek Ergashev) - Opcache: . Fixed bug GH-22857 (Function JIT emits wrong code for FETCH_OBJ_FUNC_ARG on a diff --git a/ext/mbstring/mbstring.c b/ext/mbstring/mbstring.c index 4893390a8264..6fbeb43fc1b4 100644 --- a/ext/mbstring/mbstring.c +++ b/ext/mbstring/mbstring.c @@ -1891,6 +1891,9 @@ static unsigned char* offset_to_pointer_utf8(unsigned char *str, unsigned char * } pos += u8_tbl[*pos]; } + if (pos > end) { + pos = end; + } return pos; } } diff --git a/ext/mbstring/tests/gh23106.phpt b/ext/mbstring/tests/gh23106.phpt new file mode 100644 index 000000000000..f55df0f2f081 --- /dev/null +++ b/ext/mbstring/tests/gh23106.phpt @@ -0,0 +1,22 @@ +--TEST-- +GH-23106 (mb_strpos() reads past the end of a haystack ending in a truncated UTF-8 sequence) +--EXTENSIONS-- +mbstring +--FILE-- +getMessage(), "\n"; +} +?> +--EXPECT-- +bool(false) +bool(false) +bool(false) +int(1) +mb_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)