Skip to content

Resolve negative preg_match() offsets outside php_pcre_match_impl() - #25729

Open
marc-mabe wants to merge 1 commit into
php:masterfrom
marc-mabe:fix/preg-match-offset-int-min
Open

marc-mabe wants to merge 1 commit into
php:masterfrom
marc-mabe:fix/preg-match-offset-int-min

Conversation

@marc-mabe

Copy link
Copy Markdown
Contributor

php_pcre_match_impl() took the start offset as zend_off_t, a type meant for file offsets, and resolved negative offsets itself by negating them, which overflows for ZEND_LONG_MIN. To avoid that, preg_match() and preg_match_all() rejected PHP_INT_MIN with a ValueError (#16189).

Resolve negative offsets in preg_match() and preg_match_all() instead, where PHP_INT_MIN now starts at the beginning of the subject like any other negative offset beyond its length, and make php_pcre_match_impl() take a size_t offset. Callers with a constant offset of 0, such as the frameless preg_match() call and RegexIterator, no longer pay for the negative offset handling.

php_pcre_match_impl() took the start offset as zend_off_t, a type meant
for file offsets, and resolved negative offsets itself by negating them,
which overflows for ZEND_LONG_MIN. To avoid that, preg_match() and
preg_match_all() rejected PHP_INT_MIN with a ValueError (phpGH-16189).

Resolve negative offsets in preg_match() and preg_match_all() instead,
where PHP_INT_MIN now starts at the beginning of the subject like any
other negative offset beyond its length, and make php_pcre_match_impl()
take a size_t offset. Callers with a constant offset of 0, such as the
frameless preg_match() call and RegexIterator, no longer pay for the
negative offset handling.
@marc-mabe
marc-mabe force-pushed the fix/preg-match-offset-int-min branch 2 times, most recently from e10d1dc to fd62378 Compare November 25, 2026 07:07
@marc-mabe

Copy link
Copy Markdown
Contributor Author

Ping @devnexen as original author of #16189

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.

1 participant