Jun 28, 2010

Posted by in PHP | 0 comments

pcre backtrack_limit and recursion_limit bug

Have you ever encountered the bug pcre.backtrack_limit too restrictive OR pcre.recursion_limit too restrictive

This is a rare bug and mostly comes when we use excessive regular expressions. Mostly we encounter this bug when we use the PCRE functions in php such as

  • preg_filter — Perform a regular expression search and replace
  • preg_grep — Return array entries that match the pattern
  • preg_last_error — Returns the error code of the last PCRE regex execution
  • preg_match_all — Perform a global regular expression match
  • preg_match — Perform a regular expression match
  • preg_quote — Quote regular expression characters
  • preg_replace_callback — Perform a regular expression search and replace using a callback
  • preg_replace — Perform a regular expression search and replace
  • preg_split — Split string by a regular expression

The default value for pcre.backtrack_limit and pcre.recursion_limit are 100000 (less than 100K). Which is okay when we are using less regular expression. But, at times we need to increase there limit to use excessive regular expressions.

We can increase there limit in php.ini file else using the following code in our php file.

if ( @ini_get('pcre.backtrack_limit') <= 950000 )
@ini_set('pcre.backtrack_limit', 950000);
if ( @ini_get('pcre.recursion_limit') <= 350000 )
@ini_set('pcre.recursion_limit', 350000);

Be careful while increasing its limit, as this can cause slower performance.

Feel free to share your thoughts and ideas if you have any on these two things

Leave a Reply