Voting

: max(nine, six)?
(Example: nine)

The Note You're Voting On

stamster at gmail dot com
10 years ago
Be careful with this pattern match and large input buffer on preg_match_* functions.

<?php
$pattern = '/\{(?:[^{}]|(?R))*\}/';

preg_match_all($pattern, $buffer, $matches); 
?>

if $buffer is 80+ KB in size, you'll end up with segfault! 

[89396.588854] php[4384]: segfault at 7ffd6e2bdeb0 ip 00007fa20c8d67ed sp 00007ffd6e2bde70 error 6 in libpcre.so.3.13.1[7fa20c8c3000+3c000]

This is due to the PCRE recursion. This is a known bug in PHP since 2008, but it's source is not PHP itself but PCRE library. 

Rasmus Lerdorf has the answer: https://bugs.php.net/bug.php?id=45735#1365812629

"The problem here is that there is no way to detect run-away regular expressions 
here without huge performance and memory penalties. Yes, we could build PCRE in a 
way that it wouldn't segfault and we could crank up the default backtrack limit 
to something huge, but it would slow every regex call down by a lot. If PCRE 
provided a way to handle this in a more graceful manner without the performance 
hit we would of course use it."

<< Back to user notes page

To Top