Bug #68774 [NEW]: Calling a member variable as a function in a closure causes a crash

From: Date: Fri, 09 Jan 2015 09:11:22 +0000
Subject: Bug #68774 [NEW]: Calling a member variable as a function in a closure causes a crash
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-189827@lists.php.net to get a copy of this message
From: beerend dot lauwers at capgemini dot com Operating system: Windows 7 PHP version: 5.5.20 Package: Class/Object related Bug Type: Bug Bug description:Calling a member variable as a function in a closure causes a crash Description: ------------ Let's say we have a member variable $foo, which will hold something callable. We then assign to $foo a closure, say, function($x){ return $x; }: $this->foo = function($x) { return $x; }; Then, we assign to $foo a new closure, which brings into a scope another closure: $another_f = function($x) { return $x; }; $this->foo = function() use ($another_f) { $args = func_get_args(); call_user_func_array( $this->foo, $args ); // Crash occurs here call_user_func_array( $another_f, $args ); }; The PHP process will crash on the line where it calls $this->foo as a function. Probably recurses into an infinite loop. Test script: --------------- https://gist.github.com/BeerendLauwers/c28585ce4868ab42d2f9 Expected result: ---------------- The value returned from $t->apply() should return TRUE. Actual result: -------------- PHP process crashes. As a workaround, you can assign $this->foo to a placeholder variable and bring it into scope into the closure via use, which will bind it correctly: $placeholder = $this->foo; $another_f = function($x) { return $x; }; $this->foo = function() use ($another_f,$placeholder) { $args = func_get_args(); call_user_func_array( $placeholder, $args ); call_user_func_array( $another_f, $args ); }; Perhaps make it illegal to call a member variable as a function while assigning it to itself, because the value of $this->foo is not captured correctly by the closure. -- Edit bug report at https://bugs.php.net/bug.php?id=68774&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=68774&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=68774&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=68774&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=68774&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=68774&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=68774&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=68774&r=needscript Try newer version: https://bugs.php.net/fix.php?id=68774&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=68774&r=support Expected behavior: https://bugs.php.net/fix.php?id=68774&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=68774&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=68774&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=68774&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=68774&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=68774&r=dst IIS Stability: https://bugs.php.net/fix.php?id=68774&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=68774&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=68774&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=68774&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=68774&r=mysqlcfg

« previous php.bugs (#189827) next »