ext/pcre: add PREG_THROW_ON_ERROR flag - #22797
#22797Draft
aldemeery wants to merge 2 commits into
php:masterphp/php-src:masterfrom
aldemeery:preg-throw-on-erroraldemeery/php-src:preg-throw-on-errorCopy head branch name to clipboard
Draft
ext/pcre: add PREG_THROW_ON_ERROR flag#22797aldemeery wants to merge 2 commits intophp:masterphp/php-src:masterfrom aldemeery:preg-throw-on-erroraldemeery/php-src:preg-throw-on-errorCopy head branch name to clipboard
aldemeery wants to merge 2 commits into
php:masterphp/php-src:masterfrom
aldemeery:preg-throw-on-erroraldemeery/php-src:preg-throw-on-errorCopy head branch name to clipboard
Conversation
aldemeery
force-pushed
the
preg-throw-on-error
branch
2 times, most recently
from
July 18, 2026 16:56
4f569f9 to
ac1aea9
Compare
aldemeery
force-pushed
the
preg-throw-on-error
branch
from
July 18, 2026 21:32
ac1aea9 to
6ee99c6
Compare
aldemeery
force-pushed
the
preg-throw-on-error
branch
from
July 22, 2026 04:19
65d19ae to
cc56e25
Compare
aldemeery
force-pushed
the
preg-throw-on-error
branch
from
July 28, 2026 00:51
cc56e25 to
cf51084
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds an opt-in
PREG_THROW_ON_ERRORflag to thepreg_*()functions.Pass it and any PCRE error the call records in
preg_last_error()is additionally thrown as a\PregException, so it can be caught instead of inspected by hand.Same idea as
JSON_THROW_ON_ERROR(7.3) andFILTER_THROW_ON_FAILURE(8.5).RFC: https://wiki.php.net/rfc/preg_throw_on_error
What it does
The flag only changes how an error is delivered. A call behaves exactly as it does without the flag, byte for byte, and additionally throws at the end if it recorded an error.
The exception carries the same code and message
preg_last_error()/preg_last_error_msg()report.There is one throw site and it reads the same state those functions read, so the two can never disagree.
Covered:
preg_*()functions that take$flags.preg_replace()andpreg_filter()gain an optional$flagsparameter, the only two that lacked one.\PregException extends \Exception(@strict-properties, not final), following the extension-exceptions naming guidance.Notes for review
PREG_INTERNAL_ERROR/ "Internal error" thatpreg_last_error()reports. The detailed "Compilation failed: ..." text stays on the warning, as today. Makingpreg_last_error_msg()carry that detail would change the non-flag path, so it is left for a separate change.preg_last_error(), throwing whatever error the same call leaves there. That is last-one-wins forpreg_replace()over an array and first-and-stop forpreg_grep(), matching each function's existing behavior. Whether thepreg_*()functions should fail fast on arrays is a separate question about those functions and is out of scope here.preg_*()inside apreg_replace_callback()callback does not make a successful outer call throw, and a callback's own exception always wins.$matches,$count) hold exactly what the same call without the flag would leave in them.ext/pcrekeep working without a recompile.Targets the next feature release after 8.6.
Tests assert the invariant directly, that the exception's code and message equal
preg_last_error()/preg_last_error_msg()byte for byte across compile and execution errors, and that a call without the flag behaves exactly as before.