Exceptional command-line PHP
(Yes, I know, I’ve done no blogging in far too long. I’ve got a stack of stuff to blog about, but it’s all rather heavy. In the mean time, here’s something light.)
When executing code at the command line using php -r and PHP 5.2.5, be sure not to extend the Exception class. It will cause a segmentation fault.
For example, the following causes no trouble at all:
Samurai:~ pmjones$ php -r "throw new Exception();"
PHP Fatal error: Uncaught exception 'Exception' in Command line code:1
Stack trace:
#0 {main}
thrown in Command line code on line 1
But the next example gives a segmentation fault following a long … pause … after the stack trace output:
Samurai:~ pmjones$ php -r "class Foo extends Exception {} throw new Exception();"
Fatal error: Uncaught exception 'Exception' in Command line code:1
Stack trace:
#0 {main}
thrown in Command line code on line 1
Segmentation fault
Note that we didn’t even throw the extended Foo exception; we threw the native PHP exception. The mere presence of the extended class is enough to cause the segfault.
It took me two evenings to track this down; what you see here is the simplified generic case. I’ve entered a bug with the PHP guys here.
Update: I thought I was running 5.2.6, but I was wrong; this was occurring on PHP 5.2.5. Note to self: check to make sure you’re running the latest version. :-)
Update (2008-08-12): These guys found the problem earlier, too: https://bugs.launchpad.net/ubuntu/+source/php5/+bug/198246.
August 8th, 2008 at 01:13
PHP 5.2.4-2ubuntu5.1 with Suhosin-Patch 0.9.6.2 (cli) (built: May 9 2008 16:34:16)
php -r "class Foo extends Exception {} throw new Exception();"
Exception: in Command line code on line 1
Call Stack:
0.0110 61752 1. {main}() Command line code:0
ALERT - canary mismatch on efree() - heap overflow detected (attacker 'REMOTE_ADDR not set', file 'unknown')
Perhaps the last line is the problem – but only suhosin display error?
August 8th, 2008 at 09:06
@Marcin: I’ve read that “heap corruption” is exactly the problem. It has been fixed on PHP 5.2.6, but at least 2 earlier versions (5.2.4 and 5.2.5) appear to be affected, maybe more.