Skip to content

Fix GH-25024: backport mb_ereg_replace() \k<name> OOB-read clamp to PHP-8.2/8.3 - #25143

Open
dhrupo wants to merge 1 commit into
php:PHP-8.2from
dhrupo:fix/gh-25024-mbereg-kname-oob
Open

dhrupo wants to merge 1 commit into
php:PHP-8.2from
dhrupo:fix/gh-25024-mbereg-kname-oob

Conversation

@dhrupo

@dhrupo dhrupo commented Nov 2, 2026

Copy link
Copy Markdown

Backports the mb_ereg_replace() \k<name> out-of-bounds read fix (509e27b, Closes GH-23499; fix by Ilia Alshanetsky) to the still-supported PHP-8.2 and PHP-8.3 branches, as requested in GH-25024.

When a \k<name> backref in the replacement string has no closing delimiter, mb_regex_substitute() advances p past eos at two sites and the following smart_str_appendl(pbuf, sp, p - sp) copies the overshoot (a NUL terminator, or heap bytes past the string) into the returned result. Both sites are clamped to eos, identical to PHP-8.4+.

Opened against PHP-8.2 so it merges up into 8.3 (8.4 already has the fix). If 8.2 is considered too close to EOL to warrant it, happy to retarget to PHP-8.3.

Testing (built from source, NTS, --enable-mbstring):

  • ext/mbstring/tests/mb_ereg_replace_kname_unterminated_nul.phpt (the upstream test): FAILS on clean 8.2 and 8.3 (output carries a trailing NUL / overshoot), PASSES after the clamps.
  • Full ext/mbstring/tests: 8.2 = 406 passed / 0 failed (2 skipped), 8.3 = 423 passed / 0 failed (2 skipped).

Drafted with an LLM; I reviewed and tested it myself.

Backport of 509e27b (Closes phpGH-23499; fix by Ilia Alshanetsky).
When a \k<name> backref in the replacement lacks its closing delimiter,
two error paths in mb_regex_substitute() advance p past eos and the
following smart_str_appendl() copies the overshoot (a NUL terminator, or
heap bytes past the string) into the result. Clamp both sites to eos,
identical to PHP-8.4+. The fix shipped in 8.4.26 but was never backported
to the still-supported 8.2 and 8.3 branches.
@alexdowad

Copy link
Copy Markdown
Contributor

@dhrupo, are you connected somehow to the gentleman who sent this PR earlier today? #25121

Your PR relates to a different issue, but the style, etc. seems very similar to his.

@dhrupo

dhrupo commented Nov 2, 2026

Copy link
Copy Markdown
Author

No, don't know them at all. We probably read alike because both PRs leaned on an LLM for an mbstring OOB backport — and "a fix that landed in 8.4 but never got backported" is an obvious thing to go looking for, so same idea the same day isn't that surprising.

I did build and test this one for real though: 8.2 and 8.3 from source, the upstream test fails before the clamps and passes after, full ext/mbstring/tests green on both. If you'd rather not take LLM-assisted backports, say so and I'll close it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants