Req #76124 [Opn]: Better way to detect PCRE errors such as invalid patterns
| From: | cmb@php.net | Date: | Wed, 21 Mar 2018 16:30:35 +0000 |
| Subject: | Req #76124 [Opn]: Better way to detect PCRE errors such as invalid patterns | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-214437@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=76124&edit=1
ID: 76124
Updated by: cmb@php.net
Reported by: graefrath at femu dot rwth-aachen dot de
Summary: Better way to detect PCRE errors such as invalid
patterns
Status: Open
Type: Feature/Change Request
Package: PCRE related
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
> Additionally, the last error is not cleared when a PCRE function
is called [â¦]
In my opinion, this is a bug.
Previous Comments:
------------------------------------------------------------------------
[2018-03-21 11:53:48] graefrath at femu dot rwth-aachen dot de
Description:
------------
preg_match returns false in case of an error. However, not all errors that can occur have
corresponding error codes returned by preg_last_error. Additionally, the last error is not cleared
when a PCRE function is called and there is no function to manually clear it. This makes it very
hard to reliably detect errors such as invalid patterns. Consider the following example:
@preg_match("/./u", "\xff"); // returns false and sets preg_last_error to
PREG_BAD_UTF8_ERROR
@preg_match("invalidpattern", ""); // returns false, but preg_last_error is
still PREG_BAD_UTF8_ERROR
The only way to detect the invalid pattern error is to catch the PHP warning that is being raised.
This seems really inconsistent to me. Since PHP < 7 does not have error_clear_last yet, it is
also not trivial to figure out whether you have to look at the last PHP error or the last PCRE
error, since a PHP error message starting with preg_ could also be from a previous call.
Expected result:
----------------
I expect the return value of preg_last_error to reflect all kinds of errors correctly, not just some
of them.
Actual result:
--------------
preg_last_error returns a valid error code only for some errors, while other are indicated by PHP
warnings.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=76124&edit=1