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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With