Bug #50380 [Ver->Csd]: non static class calls inside of objects will cause a fake __call and crashes

From: Date: Sun, 17 Oct 2010 15:48:20 +0000
Subject: Bug #50380 [Ver->Csd]: non static class calls inside of objects will cause a fake __call and crashes
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-155273@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=50380&edit=1

 ID:                 50380
 Updated by:         felipe@php.net
 Reported by:        dkr at mindwerk dot de
 Summary:            non static class calls inside of objects will cause
                     a fake __call and crashes
-Status:             Verified
+Status:             Closed
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   linux 2.6.21
 PHP Version:        5.2.11
-Assigned To:        
+Assigned To:        felipe
 Block user comment: N

 New Comment:

It looks already fixed.


Previous Comments:
------------------------------------------------------------------------
[2010-01-11 08:53:32] David dot Gausmann at measx dot com

On Windows XP SP3 I've got the following output (I've switched error
reporting to E_ALL):
Notice: Use of undefined constant self - assumed 'self' in
C:\xampp\htdocs\test3.php on line 49
foobar

------------------------------------------------------------------------
[2009-12-14 14:48:49] dkr at mindwerk dot de

Hm, i just think that this is an endless loop that should throw any
error like "max function nesting reached" or something. The following
codes are 2 easier examples that will fault:

Reproduce code #1:
------------------
<?php
class Foo {
	static function __call($f,$a) {
		static $call = 0;
		test();
	}
}
function test() {
	call_user_func_array(array('Foo','__call'),array(0,array()));
}
test();
?>

Reproduce code #2:
------------------
<?php
function Bar() {
	Foo();
}
function Foo() {
	Bar();
}
Foo();
?>

Expected result:
----------------
Fatal Error: Maximum calls of nested functions reached in ... on line
...

Actual result:
--------------
Segmentation fault

------------------------------------------------------------------------
[2009-12-08 16:59:46] felipe@php.net

__call activated on class: Foo

Warning: String is not zero-terminated (ZZZZZZ&#65533;&#65533;&#65533;)
(source: /home/felipe/dev/php5_2/Zend/zend_execute_API.c:414) in
/home/felipe/dev/bug.php on line 15
[Tue Dec  8 14:59:22 2009]  Script:  '../bug.php'
---------------------------------------
/home/felipe/dev/php5_2/Zend/zend_execute_API.c(414) : Block 0x08b757f0
status:
/home/felipe/dev/php5_2/Zend/zend_variables.c(35) : Actual location
(location was relayed)
Beginning:  	Cached
Freed (invalid)
    Start:	OK
      End:	OK
---------------------------------------
Foo: foobar
[Tue Dec  8 14:59:22 2009]  Script:  '../bug.php'
/home/felipe/dev/php5_2/Zend/zend_object_handlers.c(767) :  Freeing
0x08B75B80 (44 bytes), script=../bug.php
[Tue Dec  8 14:59:22 2009]  Script:  '../bug.php'
/home/felipe/dev/php5_2/Zend/zend_object_handlers.c(775) :  Freeing
0x08B75E7C (6 bytes), script=../bug.php
=== Total 2 memory leaks detected ===


------------------------------------------------------------------------
[2009-12-08 16:33:13] dkr at mindwerk dot de

No, that is NOT BOGUS i think, there is something went wrong, and it
took me quite a while finding the error in the app (using strace etc),
and quite a while yet to reproduce this behavior with a small code
snippet that will SEGFAULT. 

Note: Currently only tested on 5.2.11 and 5.2.0-8+etch15

Reproduce code:
---------------

<?php
class Foo
{
	function __call($f,$a)
	{
		echo("__call activated on class: ".__CLASS__.PHP_EOL);
	}
	function write($text) {
		echo( 'Foo: '.$text.PHP_EOL );
	}
	function debug($text) {
		return call_user_func_array(
			array(self,'write'),
			array($text,1)
		);
	}
}

class Bar {
	function __construct()
	{
		Foo::debug("foobar");
	}
}

class Baz {
	function __construct()
	{
		Foo::debug("foobar");
	}
	// here he dies...
	function __call($f,$a)
	{
		echo("__call activated on class: ".__CLASS__.PHP_EOL);
	}
}

$bar = new Bar();
$baz = new Baz(); // will cause php to die
?>

Expected result:
----------------
1. Calling non-static function Foo::debug() in Bar::__construct() 
a) should throw an function not found error while "self::" is used, as
the engine does a lookup in Bar and does not find any function.
b) should not make the usage of Foo::__call() as it does not interact in
the scope of Class Foo finally?

2. Calling Foo::debug() in Baz::__construct() in Baz::__construct()
a) should use __call as it is defined in Baz, and should rely on the
scope of this class and not the non static called class Foo.. Uncomment
Foo::__call to let the whole thing get more weird, as the text
"Baz::foobar" is normally echoed if Baz::__call is only defined there
(only defined!)
b) should never make the usage of Foo::call() as it does not interact in
the scope of Class Foo finally?

Actual result:
--------------
dkr@*:~$ php test.php
__call activated on class: Foo
Segmentation fault
dkr@*:~$

Actual result without Foo::__call():
------------------------------------
dkr@*:~$ php phptest.php
PHP Warning:  call_user_func_array(): Unable to call self::write() in *
on line *
Foo: foobar
dkr@*:~$

------------------------------------------------------------------------
[2009-12-04 19:02:39] dkr at mindwerk dot de

If i uncomment Bar::__call, i get the text "foobar" and not "__called!"
or any other error. That is how 5.2.11 (not tried another version)
handles it here currently.. I will try others later.

So, if self:: does rely on the scope of the object we came from (because
we have NOT defined the function as static), Foo::__call should never be
called if self:: or $this is used in Foo, or am i wrong?

It is something like PHP uses Bar::__call if it exists and if not
Foo::__call(), hence we are not extending the class...

Output if Bar::__call commented: "foobar"
Output if Bar::__call uncommented: "__called!"

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    http://bugs.php.net/bug.php?id=50380


-- 
Edit this bug report at http://bugs.php.net/bug.php?id=50380&edit=1


Thread (8 messages)

« previous php.bugs (#155273) next »