vote up 3 vote down star
1

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.

flag

45% accept rate
Probably the compiler saw what you did there and thought oh boy, clever-time... leave me the hell alone... – ApoY2k Nov 23 at 12:30
1  
No doubt. Especially because PHP doesn't have a compiler. :p – Vilx- Nov 23 at 12:31
I don't think that the crash is an optimization. On the other hand, how should PHP handle something like that more gracefully? – Manni Nov 23 at 12:33
4  
You shouldn't expect PHP to give nice error messages when it sees something like this: lorienshaw.net/hasselhoff.html – schnaader Nov 23 at 12:36
Another reason not to use PHP – Yacoby Nov 23 at 12:43
show 2 more comments

4 Answers

vote up 5 vote down check

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

link|flag
Sweet. Can't wait until production upgrades to PHP 5.3 then. :P – Vilx- Nov 23 at 13:02
vote up 2 vote down

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.

link|flag
Good info about xdebug, but I'd contest your statement that recursive functions are a bad idea. Yes, they can be tricky to use, but for some situations (like tree traversal) they are the most efficient way to go. – dnagirl Nov 23 at 13:20
Actually its totally taken from somewhere else, and I thought about commenting on bad recursive functions. Dont get me wrong I know the power of recursion, I just didnt want to modify the quote. – Cem Kalyoncu Nov 23 at 13:47
vote up 2 vote down

"void 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
}
link|flag
2  
That's not the point. Sure I can code with many safeguards of my own. But bugs are inevitable, and sooner or later there will be a StackOverflow as well. Most likely the recursion won't be intended, like one function calling another which in turn calls the first one. Having a nondescript segfault in this case doesn't help at all. – Vilx- Nov 23 at 13:00
True, dont know what happens if a debug version of the php module is used. By default, any app that overflows will generate a segfault and quit... because its a segfault. Special (slow) debug code must be added to a file to handle stack overflows, thats probably why the php interpreter dosen't handle it. You may also try using xdebug and/or suhosin – Quamis Nov 26 at 9:50
vote up 0 vote down

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

link|flag
1  
Link isn't working. – MiffTheFox Nov 23 at 12:35
Interesting quote from the linked article: "avoid using recursive functions they are generally a bad idea anyway". – Manni Nov 23 at 12:36
Refreshing the page after loading will make it work - strange... – schnaader Nov 23 at 12:39

Your Answer

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.