dongliuxia9495 2018-10-17 15:31
浏览 50

preg_replace_callback导致PHP死亡

I have a legacy text filter, which uses preg_replace_callback for parsing. However, when the parsed text is too complex, the code dies with no error message given, except for this PHP log entry WARNING: [pool www] child ... exited on signal 11.

To illustrate the problem, run the following code with values 10 and 20 in the $rows variable:

<?php

function my_callback(&$matches) {
    return $matches[0];
}

$regex = '#^\{\|(.*?)(?:^\|\+(.*?))?(^(?:((?R))|.)*?)^\|}#msi';

$columns = '';
$rows = 10; // 20 causes WSOD with no error given

for ($i=0; $i<20; $i++) {
  $columns .= "| style=\"width:6em\" | $i
";
}
$body = "{|
" . implode("|-
", array_fill(0, $rows, $columns)) . "
|}";

print preg_replace_callback($regex, 'my_callback', $body);

How do I increase the correcponding resource limit (assuming there is some) the code is hitting? Any help highly appreciated!

  • 写回答

1条回答 默认 最新

  • duandi6531 2018-10-18 08:59
    关注

    After deep digging it turned out, that this is a variant of an insufficient size of PCRE stack. To debug, it was required to run the program on the cli PHP, when it was possible to use the preg_last_error() on its last line, to obtain the error message (6 in this case).

    The solution to this is either to recompile PCRE to use bigger stack, or to rewrite the REGEX to use less memory (effectively to give up the recursion).

    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法