From edd43340f6de8bce0d4f705ef9a5bf61044d8c92 Mon Sep 17 00:00:00 2001 From: Vishnu Srivatsava Date: Sun, 1 Nov 2026 16:21:44 +0530 Subject: [PATCH] Fix GH-23106: mb_strpos() reads past the end of a haystack ending in a truncated UTF-8 sequence Backport of master commit cc22056f9d (PR #23107), requested in GH-25025. offset_to_pointer_utf8() can step past the end of the haystack when the forward offset walk lands on a truncated multi-byte sequence, and the returned pointer is then used by zend_memnstr() despite violating its end >= pointer invariant. Clamp the pointer to the end of the haystack. --- ext/mbstring/mbstring.c | 3 +++ ext/mbstring/tests/gh23106.phpt | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 ext/mbstring/tests/gh23106.phpt diff --git a/ext/mbstring/mbstring.c b/ext/mbstring/mbstring.c index 2ee6306bfb42..f240b86b4788 100644 --- a/ext/mbstring/mbstring.c +++ b/ext/mbstring/mbstring.c @@ -1910,6 +1910,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)