Urgend: __get and foreach / Segfaults

php.internals

Mike Bretz

20 years ago
Hello, we currently monitor segmentation faults on all of our production and development servers running php 5.0.5 and 5.1.2 which leads to a complete application failure. We will need a quick solution the reason why I write this to the list instead of to bugs.php.net. We traced down the problem to be associated with a foreach loop over an array return by __get method within a class. The segmentation fault occurs randomly either while shutting down php module (while cleaning up memory) or just within the application after the foreach loop. Using the current snapshot the problem can be reproduced visually (without segfaults) as follows: ----------------- <? class Foo { private $data = array("errors" => array(1, 2, 3)); function __get($x) { return $this->data[$x]; } } $t = new Foo(); var_dump($t); function X(Foo $t) { // when using the foreach within a function the error gets triggered // following 2 lines have to be enabled to see the bug. $bar = $t->errors; // not using this line makes the script working. foreach($t->errors as $foo); // not using this line also makes the script working. } X($t); var_dump($t); ?> --------- Output expected: object(Foo)#4 (1) { ["data:private"]=> array(1) { ["errors"]=> array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } } } object(Foo)#4 (1) { ["data:private"]=> array(1) { ["errors"]=> array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } } } Actual Output: object(Foo)#4 (1) { ["data:private"]=> array(1) { ["errors"]=> array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } } } object(Foo)#4 (1) { ["data:private"]=> array(1) { ["errors"]=> NULL } } ------- I think the foreach within the X function in conjunction with the $bar variable is freeing $t->errors array, which in fact is wrong behaviour. Using $bar var as input for the foreach makes the application working, too. Note that this small application does not segfault, but when executing some (a lot) just before and after that code sniplet it almost always leads to a crash here. Who is able to look at that and provide a patch? Thank you, Mike
-- mike peter bretz metropolis ag / entwicklung email: m.bretz@metropolis-ag.de heinestraße 72 phone: +49-7121-348-120 d-72762 reutlingen fax: +49-7121-348-111 http://www.metropolis-ag.de/ metropolis ag. creating social internetworks.

Rasmus Lerdorf

20 years ago
Mike Bretz wrote:
> Note that this small application does not segfault, but when executing > some (a lot) just before and after that code sniplet it almost always > leads to a crash here. > > Who is able to look at that and provide a patch?
Please file a proper bug report at bugs.php.net and make sure you include your operating system type and version along with a backtrace of an actual crash. -Rasmus