note 88587 added to function.preg-match

From: Date: Fri, 30 Jan 2009 11:05:41 +0000
Subject: note 88587 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-149972@lists.php.net to get a copy of this message
Bugs of preg_match (PHP-version 5.2.5) In most cases, the following example will show one of two PHP-bugs discovered with preg_match depending on your PHP-version and configuration. <?php $text = "test="; // creates a rather long text for ($i = 0; $i++ < 100000;) $text .= "%AB"; // a typical URL_query validity-checker (the pattern's function does not matter for this example) $pattern = '/^(?:[;\/?:@&=+$,]|(?:[^\W_]|[-_.!~*\()\[\] ])|(?:%[\da-fA-F]{2}))*$/'; var_dump( preg_match( $pattern, $text ) ); ?> Possible bug (1): ============= On one of our Linux-Servers the above example crashes PHP-execution with a C(?) Segmentation Fault(!). This seems to be a known bug (see http://bugs.php.net/bug.php?id=40909), but I don't know if it has been fixed, yet. If you are looking for a work-around, the following code-snippet is what I found helpful. It wraps the possibly crashing preg_match call by decreasing the PCRE recursion limit in order to result in a Reg-Exp error instead of a PHP-crash. <?php [...] // decrease the PCRE recursion limit for the (possibly dangerous) preg_match call $former_recursion_limit = ini_set( "pcre.recursion_limit", 10000 ); // the wrapped preg_match call $result = preg_match( $pattern, $text ); // reset the PCRE recursion limit to its original value ini_set( "pcre.recursion_limit", $former_recursion_limit ); // if the reg-exp fails due to the decreased recursion limit we may not make any statement, but PHP-execution continues if ( PREG_RECURSION_LIMIT_ERROR === preg_last_error() ) { // react on the failed regular expression here $result = [...]; // do logging or email-sending here [...] } //if ?> Possible bug (2): ============= On one of our Windows-Servers the above example does not crash PHP, but (directly) hits the recursion-limit. Here, the problem is that preg_match does not return boolean(false) as expected by the description / manual of above. In short, preg_match seems to return an int(0) instead of the expected boolean(false) if the regular expression could not be executed due to the PCRE recursion-limit. So, if preg_match results in int(0) you seem to have to check preg_last_error() if maybe an error occurred. ---- Server IP: 69.147.83.197 Probable Submitter: 87.159.96.188 ---- Manual Page -- http://www.php.net/manual/en/function.preg-match.php Edit -- https://master.php.net/note/edit/88587 Del: integrated -- https://master.php.net/note/delete/88587/integrated Del: useless -- https://master.php.net/note/delete/88587/useless Del: bad code -- https://master.php.net/note/delete/88587/bad+code Del: spam -- https://master.php.net/note/delete/88587/spam Del: non-english -- https://master.php.net/note/delete/88587/non-english Del: in docs -- https://master.php.net/note/delete/88587/in+docs Del: other reasons-- https://master.php.net/note/delete/88587 Reject -- https://master.php.net/note/reject/88587 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#149972) next »