php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35123 here's how to seg fault php everytime by doing simple OOP
Submitted: 2005-11-06 09:42 UTC Modified: 2005-11-06 10:06 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: joe at popcast dot com Assigned:
Status: Not a bug Package: Reproducible crash
PHP Version: 5.0.5 OS: linux, XP, Mac
Private report: No CVE-ID: None
 [2005-11-06 09:42 UTC] joe at popcast dot com
Description:
------------
Using PHP from the standard Windows installer and running under Apache, the following code both produces unexpected output and crashes PHP.  The code is a simple OOP variable test.

A similar bug was posted (http://bugs.php.net/bug.php?id=26930) but did not crash PHP.

To reproduce the crash, uncomment the test method in class B or C and seg fault away.

The bug is reproducible on every OS I've tested, including Linux and Mac with standard PHP installs running under Apache.



Reproduce code:
---------------
class A {
	protected static $instance;
	public $test = "A";
	
	public function getInstance()
	{
		if (! self::$instance) self::$instance = new A;
		return self::$instance;
	}
	
	public function test()
	{
		echo $this->test;
	}
	
}


class B extends A
{
	protected static $instance;
	//protected $test = "B";
	
	public function getInstance()
	{
		if (! self::$instance) self::$instance = new B;
		self::$instance->test = "B";
		return self::$instance;
	}

	/*
	public function test()
	{
		echo $this->test();
	}
	*/

}


class C extends A
{
	protected static $instance;
	//protected $test = "C";

	public function getInstance()
	{
		if (! self::$instance) self::$instance = new C;
		self::$instance->test = "C";
		return self::$instance;
	}

	/*
	public function test()
	{
		echo $this->test();
	}
	*/

}


$a = new A;
$a->test();
echo "\n----------<br>\n";
$b = new B;
$a->test();
$b->test();
echo "\n----------<br>\n";
$c = new C;
$a->test();
$b->test();
$c->test();
echo "\n----------<br>\n";


Expected result:
----------------
A
----------
AB
----------
ABC
----------

Actual result:
--------------
A
----------
AA
----------
AAA
----------

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-11-06 09:52 UTC] johannes@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip
 [2005-11-06 09:54 UTC] joe at popcast dot com
I forgot to post the work around .... Too many hours programming today.

If you redeclare $test in the subclasses (see commented out code), you get the expected results of "ABC".  The var scope in classes A, B, and C should all be public or protected (the example code incorrectly has them mixed with public in A and protected in B and C; this is just a typo and not a factor in the test).

When reproducing the crash, setting $test in class A to either public or protected yields the same results.
 [2005-11-06 09:57 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

I should read the code: You're calling $this->test()  
inside your test method so you get endless recursion ->  
segfault.  
 [2005-11-06 10:06 UTC] joe at popcast dot com
Indeed... doh!  Sorry for the bad test example.  The crash is bogus.

But this is strange OOP behavior.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 14 21:01:33 2025 UTC