문제

How can PHP cause memory leaks, buffer overflows, stack overflows and any other errors of such kind? Can PHP even cause such errors?

도움이 되었습니까?

해결책

By causing some kinda infinite recursion, you can cause a PHP crash.

For example, a file that recursively requires itself should cause a stack overflow:

require __FILE__;  

Or a recursion in the magic __sleep() method that is supposed to unserialize an object, but calls serialize() instead:

class sleepCrasher
{
    public function __sleep()
    {
        serialize($this);
    }
}

serialize(new sleepCrasher());

Or a class destructor that creates new instances:

class destructorCrasher
{
    public function __destruct()
    {
        new destructorCrasher();
    }
}

// Calling __destruct() manually is just for the sake of example, 
// In real scenarios, PHP's garbage collector will crash PHP for you.
(new destructorCrasher())->__destruct();

As well as a recursive __toString():

class toStringCrasher
{
    public function __tostring()
    {
        return strval($this);
    }
}

strval(new toStringCrasher());

There are other recursion scenarios that PHP is protected against. For example, calling a recursive function without an exit condition or a recursive self-yielding generator. These ones do not cause a crash, but a Allowed memory size of ... fatal error.

For more examples, you might want to see:

다른 팁

You can either do the stuff that would cause overflows in any language (like recursively calling the current function, mindlessly eating memory, etc.) or rely on the good old PHP interpreter to do that job. Just have a look at how many memory leaks were fixed in PHP5 (My favorite: In 5.2.6 they fixed bug #44069: 'Huge memory usage with concatenation using . instead of .=').

All in all PHP is ok (at most) if you just want to serve a single http request. But you can't really do sophisticated stuff with it (I once tried implementing a Peer2Peer client, the server died of memory shortage after just 10 minutes - could be a bug on my behalf of course, but I had spent several days finding leaks in my own code - to no avail).

PHP is a interpreted language, so all php scripts are protected from memory leaks, buffer overflow and stack overflow.

How ever you will encounter problems as such:

Integer overflow, if you assign a number too large it will overflow, and no exception will occur.

Out of memory, using more memory than memory size configured in your php.ini

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top