php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #14221 Segmentation fault when using class that accesses member functions & variables.
Submitted: 2001-11-26 02:22 UTC Modified: 2002-04-02 05:33 UTC
From: nickj at nickj dot org Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.2.0-dev OS: ANY
Private report: No CVE-ID: None
 [2001-11-26 02:22 UTC] nickj at nickj dot org
The problem: Segmentation fault when using class that accesses member functions & variables.

A short script that reproduces the problem:
[note, this is a command line script, but an equivalent web script doesn't work either]
========================================================
#!/usr/bin/php -q
<?php
        error_reporting (E_ALL);

        class test {
                var $currentField;
                function setCurrentField($field_name) {
                        $this->currentField = $field_name;
                }
                function getValue($field_name) {
                        return "getValue with arg - $field_name\n";
                }
                function getValue() {
                        return $this->getValue($this->currentField);
                }
        }


        $frm = new test;

        $frm->setCurrentField("blah");  
        print $frm->getValue();
        
?>
========================================================

What happens:
[root tmp]# ./file.php 
Segmentation fault (core dumped)
[root tmp]# 


What I would expect to happen:
[root tmp]# ./file.php 
getValue with arg - blah
[root tmp]# 


============================================
Extra info about the setup being used:

O/S is Linux Mandrake 7.2 with the shipped Linux 2.2.17 kernel, and the installed PHP uses updated RPMs that are available from the distribution's web site. 

Excerpts from phpinfo() shows:
PHP Version 4.0.4pl1 

Configure Command './configure' '--with-apxs=/usr/sbin/apxs' '--without-mysql' '--disable-static' '--disable-debug' '--enable-pic' '--enable-inline-optimization' '--prefix=/usr' '--with-zlib' '--with-config-file-path=/etc' '--enable-magic-quotes' '--enable-debugger' '--enable-track-vars' '--enable-safe-mode' '--with-exec-dir=/usr/bin' '--with-regex=system' '--with-versioning' '--enable-dba=shared' '--with-gdbm' '--with-db2' '--enable-sysvsem' '--enable-sysvshm' '--with-mod_charset' '--enable-force-cgi-redirect' '--with-mm' '--enable-trans-sid' '--with-dbase' '--with-filepro' '--enable-yp' '--enable-ftp' '--with-xml' '--with-gettext' 
Server API Apache 
Virtual Directory Support disabled 
Configuration File (php.ini) Path /etc 
ZEND_DEBUG disabled 
Thread Safety disabled 

This program makes use of the Zend scripting language engine:
Zend Engine v1.0.4, Copyright (c) 1998-2000 Zend Technologies
    with Zend Optimizer v1.1.0, Copyright (c) 1998-2000, by Zend Technologies

=============================================

Any extra info that I can provide that would help, please just let me know.

Kind Regards,
Nick.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-11-26 02:46 UTC] derick@php.net
Can you try the latest RC from : www.php.net/~zeev/php-4.1.0RC3.tar.gz ?

Derick
 [2001-11-26 03:05 UTC] mfischer@php.net
Your code is obviously bogus.

You're definig two methods with the same name. But I admit this shouldn't be possible, derick?

Anyway, because of this the second getValue() overwrites the former and therefore you have an infinite look; getValue calls getValue calls getValue calls ...

That's an ZE problem (ie. method with same name should give parse error or at least some warning).

(Correct if otherwise)

ps: php doesn't call funtions based on the parameters given
 [2001-11-26 04:01 UTC] nickj at nickj dot org
> php doesn't call functions based on the parameters given

Ah hah! OK, that changes everything - I was thinking like C++ classes, which, if my memory serves me correctly, know which member function to use.

Modified code that deals with this OK:
========================================================
#!/usr/bin/php -q
<?php
        error_reporting (E_ALL);

        class test {
                var $currentField;
                function setCurrentField($field_name) {
                    $this->currentField = $field_name;
                }
                function getValue($field_name = false) {
                    if ($field_name === false) $field_name = $this->currentField;
                    return "getValue with arg - $field_name\n";
                }
        }

        $frm = new test;
        $frm->setCurrentField("blah");  
        print $frm->getValue("real arg");
        print $frm->getValue();
                
?>
========================================================

> That's an ZE problem (ie. 
> method with same name should give parse error or at least some 
> warning).

Agreed.

Testing this with straight functions shows that the engine does do this for straight functions:
========================================================
#!/usr/bin/php -q
<?php
        error_reporting (E_ALL);

        function blah() {
                print "no arg\n";
        }

        function blah($field) {
                print "with arg, $field\n";
        }

        blah();
        blah("test");
?>
========================================================

Output gives:
[root tmp]# ./test.php
<br>
<b>Fatal error</b>:  Cannot redeclare blah() in <b>./test.php</b> on line <b>9</b><br>
[root tmp]# 

An equivalent "Cannot redeclare test class member function blah() in test.php on line 9" would be extremely helpful and is probably called for - I've always found the PHP syntax checking to extremely clear and spot-on previously.

Many thanks for all your help,

Kind Regards,
Nick.
 [2001-12-06 07:18 UTC] yohgaki@php.net
Make this report a duplicate of #13703
 [2002-04-01 19:16 UTC] nickj at nickj dot org
This looks to have been resolved here:
http://bugs.php.net/bug.php?id=16265
If this is correct, can this report be closed?
 [2002-04-01 21:39 UTC] yohgaki@php.net
http://bugs.php.net/bug.php?id=16265
should be reopenned. (I just did)

 [2002-04-01 21:51 UTC] yohgaki@php.net
It seems this isnot related to http://bugs.php.net/bug.php?id=16265

Since this segfaults
<?php
        error_reporting (E_ALL);

        class test {
                var $currentField;
                function setCurrentField($field_name) {
                        $this->currentField = $field_name;
                }
                function getValue() {
                        return $this->getValue($this->currentField);
                }
        }

        $frm = new test;

        $frm->setCurrentField("blah");  
        print $frm->getValue();
        
?>

 [2002-04-01 21:54 UTC] yohgaki@php.net
In fact, this bug is not realated to multiple method definition bug.

Stauts => Analyzed.


 [2002-04-01 22:09 UTC] nickj at nickj dot org
It is related, but that's certainly not immediately clear from the problem description.

Effectively what is happening is that there is infinite recursion in my code example given (which causes the segfault). The reason I ever believed that this was valid code was that I incorrectly believed that it was valid to have multiple member functions with the same name, but different parameters. The reason I believed this was because PHP previously happily accepted this - i.e. it allowed a class to have multiple member functions with the same name.

In essence, the code is invalid, but PHP's parsing behaviour before would make it very easy to believe that it was valid code.

The recent patch (I think it is from Derick) would definitely have prevented me from ever have believing that it was valid code, thus it would have prevented this problem from occurring.

It is for this reason that I am quite certain that (assuming the update from #16265 does what it says it does) that this problem is now resolved, and I am also very grateful for everyone's help.

Cheers,
Nick.
 [2002-04-02 05:33 UTC] yohgaki@php.net
If this bug report is related is not important.
Infinate recusive call is programmer's fault.

I think PHP should raise, error for
<?php
class foo {
  function bar() {}
  function bar() {}
}

There are some issues for preventing multiple method definition. Derick's patch does not work well, I've 
posted another patch, but it seems there is some problem. 
(i.e. parent class entry may not be defined under some condition)

Brad's proposal sounds nice.
See recent php-dev and zend-engine2 messages.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Oct 12 01:01:27 2024 UTC