[php-src] PR #25123: Fix GH-23106: mb_strpos() reads past the end of a haystack ending in a truncated UTF-8 sequence
| From: | VishnuSrivatsava | Date: | Sun, 01 Nov 2026 11:09:41 +0000 |
| Subject: | [php-src] PR #25123: Fix GH-23106: mb_strpos() reads past the end of a haystack ending in a truncated UTF-8 sequence | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-40175@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25123
Author: VishnuSrivatsava
Fixes the PHP-8.2 part of #25025 and GH-23106.
Backport of master commit cc22056f9d (PR #23107, credited to Lazizbek Ergashev). As analyzed in
#25025, on this branch the offset walk lives in
mbfl_find_offset_utf8() in libmbfl, so
the master patch had to be rewritten for the different function and const unsigned char
* pointers rather than applied directly.
The forward walk can land past the end of the haystack on a truncated multi-byte sequence, and the
returned pointer is then used by zend_memnstr() despite violating its end >=
p invariant. The clamp restores the invariant for the forward path and for the needle-offset
walk feeding zend_memnrstr() on the reverse path.
Changes:
- Clamp mbfl_find_offset_utf8() to the end of the haystack after the forward offset
walk.
- Port the regression test from PR #23107 as ext/mbstring/tests/gh23106.phpt.
Verification on this branch (macOS arm64, --enable-debug + AddressSanitizer):
- Pre-fix: mb_strpos("AA\xf0\x90", "xyz", 3) aborts with
Assertion failed: (end >= p), function zend_memnstr, file zend_operators.h, line
179.
- Post-fix: assertion gone, regression test passes, full ext/mbstring suite passes (327
passed, 0 failed).
Not touching NEWS, following the branch convention where release managers add entries at packaging
time. 8.2 is in security-only support until 2026-12-31; since this is an out-of-bounds read, it
should qualify for a security release, but that call belongs to release managers. The PHP-8.3
backport is in #25121.