English 中文(简体)
PHP doesn t handle stack overflow?
原标题:

I was surprised when I just tried the following PHP code:

function foo()
{
    foo();
}
foo();

I expected to get "500: Internal server error". Instead the connection was closed immediately (no bytes received), and the log files show that apache segfaulted. WTF? Is this a known bug in PHP? Are there some configuration options that I m missing? Because a crashed process for every accidental stack overflow is, well... pretty unacceptable, I think.

最佳回答

PHP is not able to deal with this, it will just go into an infinite loop and produce a segmentation fault.

http://bugs.php.net/bug.php?id=49823

also

http://www.mail-archive.com/php-bugs@lists.php.net/msg128905.html

问题回答

avoid using recursive functions they are generally a bad idea" 0 riiight:))) they were invented because its a bad ideea:))...

I recomment setting a hrd upper-limit on the number of times the functions is called. DO NOT use global variables (you may actually need to call more recursive functions, why pollute the globals like this?). You may use extra parameters for a function

function a($param1, $param2, $depth=100){
  $depth--;
  if(!$depth==0) return error
}

Taken from iBlog - Ilia Alshanetsky

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.

I think this is a known bug. See the list Top 10 ways to crash PHP.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签