Bug #68774 [Opn->Wfx]: Calling a member variable as a function in a closure causes a crash

From: Date: Fri, 09 Jan 2015 15:54:59 +0000
Subject: Bug #68774 [Opn->Wfx]: Calling a member variable as a function in a closure causes a crash
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-189845@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68774&edit=1 ID: 68774 Updated by: ab@php.net Reported by: beerend dot lauwers at capgemini dot com Summary: Calling a member variable as a function in a closure causes a crash -Status: Open +Status: Wont fix Type: Bug Package: Class/Object related Operating System: Windows 7 PHP Version: 5.5.20 Block user comment: N Private report: N New Comment: The issue in the first place is that the function happens to be recursive. This can be shown by this snippet: function a() {call_user_func('a');} a(); This leads to the stack overflow. You should ensure the function would terminate, or avoid passing the function to itself. Thanks. Previous Comments: ------------------------------------------------------------------------ [2015-01-09 09:11:21] beerend dot lauwers at capgemini dot com 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 this bug report at https://bugs.php.net/bug.php?id=68774&edit=1

« previous php.bugs (#189845) next »