What is the pcre.backtrack_limit "unit"?

I ran into a problem when preg_replace()with a complex regex it throws an error ( PREG_BACKTRACK_LIMIT_ERROR) due to the value too low pcre.backtrack_limit, which is set to by default 1,000,000. I installed it in 10,000,000and it works for this particular application.

My question is, what exactly is it backtracking limit, poorly defined, "unit"? Does the size match the 1,000,000size of the memory? If not, what does this mean? I am trying to understand what reasonable settings are for this in my environment.

Link to preg_replace: http://us3.php.net/manual/en/pcre.configuration.php#ini.pcre.backtrack-limit

Trackback Link: In regex, what is a backlink / backlink?

+4
source share
2 answers

From the PCRE source code, this error is returned when match () is called recursively more than 1,000,000 times:

/* First check that we haven't called match() too many times, or that we
haven't exceeded the recursive call limit. */

if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT);

This conversion to the error "PHP_PCRE_BACKTRACK_LIMIT_ERROR" here .

According to the pcreapi man page (see https://serverfault.com/a/408272/140833 ):

, PCRE , match(), ( ). , match_limit, , , , . , , .

, - " ". , 1 1 .

" " regex:

<?php

ini_set('pcre.backtrack_limit', 100);

for ($len = 1000; $len <= 1001; $len++) {

    $x = str_repeat("x", $len);
    $ret = preg_match("/x+x+y/", $x);

    echo "len = " . $len . "\n";
    echo "preg_match = " . $ret . "\n";
    echo "PREG_BACKTRACK_LIMIT_ERROR = " . PREG_BACKTRACK_LIMIT_ERROR . "\n";
    echo "preg_last_error = " . preg_last_error() . "\n";
    echo "\n";
}

: https://3v4l.org/EpaNC, :

len = 1000
preg_match = 0
PREG_BACKTRACK_LIMIT_ERROR = 2
preg_last_error = 0

len = 1001
preg_match = 
PREG_BACKTRACK_LIMIT_ERROR = 2
preg_last_error = 2
+1

, : pcre , pcre PCRE_ERROR_MATCHLIMIT. pcre, , , , , , .

, , , , ( ) - :   ini_set ('pcre.backtrack_limit', PHP_INT_MAX);

[] , pcre, , ( , ..)

0

Source: https://habr.com/ru/post/1539352/


All Articles