|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68774 Calling a member variable as a function in a closure causes a crash
Submitted: 2015-01-09 09:11 UTC Modified: 2015-01-09 15:54 UTC
From: beerend dot lauwers at capgemini dot com Assigned:
Status: Wont fix Package: Class/Object related
PHP Version: 5.5.20 OS: Windows 7
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
4 + 16 = ?
Subscribe to this entry?

 [2015-01-09 09:11 UTC] beerend dot lauwers at capgemini dot com
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:

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.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-01-09 15:54 UTC]
-Status: Open +Status: Wont fix
 [2015-01-09 15:54 UTC]
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.

PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Sep 19 03:03:37 2021 UTC