preg_match_all会导致孩子死亡

时间:2015-12-09 17:44:48

标签: php regex apache

我有一个接受图像复制/粘贴的TinyMCE表单。当您完成评论后,按“发布”并提交表单,解析内容并显示在屏幕对话中。

我正在尝试使用preg_match_all从POSTed变量中提取base64编码数据。我的大多数模式都有效,但是这个模式导致Apache崩溃。

<img src="data:image/(png|PNG|gif|GIF|jpg|JPG|jpeg|JPEG);base64,([a-zA-Z0-9+/=])*

崩溃是沉默的,我从Apache / PHP获得的唯一提示是error.log文件中的一行:

  

[错误]孩子死于信号11

我已经将它缩小到preg_match_all中的这个模式,并且事实上我在第二个组之后有一个*,它包含一个类定义,旨在跟随base64字符到它们的引号终止。

这里有样本图像(文本文件,base64编码): https://cloud.highpoweredhelp.com/index.php/s/hnIaFmK9vTCOmcU

我唯一能想到的是“*”过于贪婪并且消耗太多内存。但是,有两个问题:

  1. 我将php.ini中的memory_limit从128M增加到256M而没有结果,
  2. 文件大小仅为198K。
  3. 系统: Debian Wheezy 7.9上的Apache v2.2.22 PHP版本:5.6.16使用以下配置从源代码编译:

    ./configure --with-config-file-path=/etc/php5/apache2 \
    --with-pear=/usr/share/php \
    --with-bz2 \
    --with-curl \
    --with-gd \
    --enable-calendar \
    --enable-mbstring \
    --enable-bcmath \
    --enable-sockets \
    --with-libxml-dir \
    --with-mysqli \
    --with-mysql \
    --with-openssl \
    --with-regex=php \
    --with-readline \
    --with-zlib \
    --with-apxs2=/usr/bin/apxs2 \
    --enable-soap \
    --with-freetype-dir=/usr/include/freetype2/ \
    --with-freetype \
    --with-mcrypt=/usr/src/mcrypt-2.6.8 \
    --with-jpeg-dir=/usr/lib/x86_64-linux-gnu/ \
    --with-png-dir=/usr/lib/x86_64-linux-gnu/
    

1 个答案:

答案 0 :(得分:2)

您真的打算([a-zA-Z0-9+/=]*)而不是([a-zA-Z0-9+/=])*

后一种模式只能获得base64序列的最终字符,但似乎很快就会导致分段错误。我能够使用该模式使用相对较短的字符串来崩溃命令行应用程序:

#this segfaulted for me, a shorter string did succeed
$str='<img src="data:image/png;base64,'.str_repeat('0123456789',1000);
if (preg_match('{<img src="data:image/(png|PNG|gif|GIF|jpg|JPG|jpeg|JPEG);base64,([a-zA-Z0-9+/=])*}', $str, $match)){
        print "matched";
}

([a-zA-Z0-9+/=]*)模式将捕获每个角色,并且不会崩溃!

相关问题