当我尝试下面的PHP代码时,感到很惊讶:
function foo()
{
foo();
}
foo();
我本来期望会收到“500:内部服务器错误”的提示。但实际上,连接立即被关闭(没有接收到任何字节),而日志文件显示Apache崩溃了。这是怎么回事?PHP中是否存在已知的bug?是否有一些配置选项我没注意到?因为每次意外堆栈溢出都导致进程崩溃,这是相当不可接受的。
当我尝试下面的PHP代码时,感到很惊讶:
function foo()
{
foo();
}
foo();
我本来期望会收到“500:内部服务器错误”的提示。但实际上,连接立即被关闭(没有接收到任何字节),而日志文件显示Apache崩溃了。这是怎么回事?PHP中是否存在已知的bug?是否有一些配置选项我没注意到?因为每次意外堆栈溢出都导致进程崩溃,这是相当不可接受的。
PHP无法处理这个问题,它会陷入无限循环并产生分段错误。
http://bugs.php.net/bug.php?id=49823
同时
http://www.mail-archive.com/php-bugs@lists.php.net/msg128905.html
function a($param1, $param2, $depth=100){
$depth--;
if(!$depth==0) return error
}
Stack overflow. PHP does not have any internal stack protection choosing to rely upon the system stack without any protection. This means that if you have a recursive function or a method PHP will eventually crash.
function a() { a(); } a();
There are 2 solutions to this problem, 1 avoid using recursive functions they are generally a bad idea anyway, and if you MUST use them implement some counter using a global variable that would prevent the function from iterating itself more then X amount of time for values of X between 500 to 1000. The other solution involves using the xdebug extension that implements protection against stack overflows by defining a limit on how deep can recursive functions go via a php.ini value. This is a better solution in hosting environments where you have no control over the scripts that are being ran on the server.
我认为这是一个已知的 bug。请参阅列表 Top 10 ways to crash PHP。