Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 233
Vilx- Avatar asked Feb 28 '23 00:02

Vilx-


2 Answers

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/[email protected]/msg128905.html

like image 184
Lizard Avatar answered Mar 05 '23 17:03

Lizard


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
}
like image 20
Quamis Avatar answered Mar 05 '23 17:03

Quamis