[GIT-PULLS] [php-src] PR #23107: Fix GH-23106: mb_strpos() reads past the end of a truncated UTF-8 haystack
[email protected] (lazerg)
| Newsgroups | php.git-pulls |
|---|---|
| Message-ID | <[email protected]> |
Pull Request: https://github.com/php/php-src/pull/23107
Author: lazerg
`offset_to_pointer_utf8()` walks the haystack with the UTF-8 mblen table. If the string ends in a truncated multi-byte sequence, the table length for the lead byte is larger than the bytes actually left, so the walk returns a pointer past the end of the string. `mb_strpos()` passes that pointer to `zend_memnstr()` as the start of the search, which fails the `end >= p` assertion on a debug build.
On a release build it is an out-of-bounds read instead: `mb_strpos("AA\xf0\x90", "x", 3)` returns a different bogus offset on every run, and with a long enough haystack it segfaults.
Clamping the walk to the end of the string makes the search start at the end and find nothing, which is what an offset equal to `mb_strlen()` should do.
Fixes GH-23106