diff --git a/ext/mbstring/php_mbregex.c b/ext/mbstring/php_mbregex.c index aeb1a9b84f11..07fd67d5c218 100644 --- a/ext/mbstring/php_mbregex.c +++ b/ext/mbstring/php_mbregex.c @@ -737,8 +737,9 @@ static inline void mb_regex_substitute( clen = (int) php_mb_mbchar_bytes(p, enc); if (clen != 1 || p == eos || p[0] != '\\') { /* skip anything that's not an ascii backslash */ - smart_str_appendl(pbuf, p, clen); - p += clen; + sp = p; + p = MIN(p + clen, eos); + smart_str_appendl(pbuf, sp, p - sp); continue; } sp = p; /* save position */ diff --git a/ext/mbstring/tests/gh24769.phpt b/ext/mbstring/tests/gh24769.phpt new file mode 100644 index 000000000000..833fef9af6e3 --- /dev/null +++ b/ext/mbstring/tests/gh24769.phpt @@ -0,0 +1,19 @@ +--TEST-- +GH-24769 (mb_ereg_replace() reads past the replacement on a truncated trailing multibyte character) +--EXTENSIONS-- +mbstring +--SKIPIF-- + +--FILE-- + +--EXPECT-- +string(4) "78f0" +string(6) "78e282" +string(8) "62f09f62"