Last active
August 29, 2015 14:23
-
-
Save dkrnl/041415fe8f065fc6069f to your computer and use it in GitHub Desktop.
PCRE limit
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
<?php | |
// This test script is designed to be run from the command line. | |
// It measures the subject string length that results in a | |
// PREG_RECURSION_LIMIT_ERROR error in the preg_match() function. | |
echo("Entering TEST.PHP...\n"); | |
// Set and display pcre.recursion_limit. (set to stacksize / 500). | |
// Under Win32 httpd.exe has a stack = 256KB and 8MB for php.exe. | |
//ini_set("pcre.recursion_limit", "524"); // Stacksize = 256KB. | |
ini_set("pcre.recursion_limit", "16777"); // Stacksize = 8MB. | |
echo(sprintf("PCRE pcre.recursion_limit is set to %s\n", | |
ini_get("pcre.recursion_limit"))); | |
function parseAPIResults($results){ | |
$pattern = "/\[(.|\n)+\]/"; | |
$resultsArray = preg_match($pattern, $results, $matches); | |
if ($resultsArray === 1) { | |
$msg = 'Successful match.'; | |
} else { | |
// Either an unsuccessful match, or a PCRE error occurred. | |
$pcre_err = preg_last_error(); // PHP 5.2 and above. | |
if ($pcre_err === PREG_NO_ERROR) { | |
$msg = 'Successful non-match.'; | |
} else { | |
// preg_match error! | |
switch ($pcre_err) { | |
case PREG_INTERNAL_ERROR: | |
$msg = 'PREG_INTERNAL_ERROR'; | |
break; | |
case PREG_BACKTRACK_LIMIT_ERROR: | |
$msg = 'PREG_BACKTRACK_LIMIT_ERROR'; | |
break; | |
case PREG_RECURSION_LIMIT_ERROR: | |
$msg = 'PREG_RECURSION_LIMIT_ERROR'; | |
break; | |
case PREG_BAD_UTF8_ERROR: | |
$msg = 'PREG_BAD_UTF8_ERROR'; | |
break; | |
case PREG_BAD_UTF8_OFFSET_ERROR: | |
$msg = 'PREG_BAD_UTF8_OFFSET_ERROR'; | |
break; | |
default: | |
$msg = 'Unrecognized PREG error'; | |
break; | |
} | |
} | |
} | |
return($msg); | |
} | |
// Build a matching test string of increasing size. | |
function buildTestString() { | |
static $content = ""; | |
$content .= "A"; | |
return '['. $content .']'; | |
} | |
// Find subject string length that results in error. | |
for (;;) { // Infinite loop. Break out. | |
$str = buildTestString(); | |
$msg = parseAPIResults($str); | |
printf("Length =%10d\r", strlen($str)); | |
if ($msg !== 'Successful match.') break; | |
} | |
echo(sprintf("\nPCRE_ERROR = \"%s\" at subject string length = %d\n", | |
$msg, strlen($str))); | |
echo("Exiting TEST.PHP..."); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment