[PHP-DEV] PHP 4.0 Bug #3959: bogus references to $this From: alan <email protected>
Date: 03/29/00

From: alan <email protected>
Operating system: rh 6.0 2.2.11
PHP version: 4.0 Latest CVS (29/03/2000)
PHP Bug Type: Scripting Engine problem
Bug description: bogus references to $this

When creating nested objects thus;
$a=new person(
        "mamma",
        array(
                new person(
                        "b",
                        array(
                                new person(
                                        "c"
                                )
                        )
                )
        )
);
  
what ever $this is point to during the construction of the autovivfied objects doesnt seem to hang around. References made to $this during construction dont seem to be valid latter, resulting in a segfault due to the inability of Zend to resolve the reference (from a backtrace it apears to loop until it runs out of memory).

Here is some code that produces the segfault;

 
class person{
        function person($name="default_name", $kids=""){
                $this->name=$name;
 
                if(is_array($kids))foreach($kids as $val){
                        $kid->parent=&$this; // problem line here, but I dont
                                // want a copy...
                        $this->kids[$kid->name]=$kid;
                }
        }
 
        function whos_ya_mamma(){
                if(is_array($this->kids)){
                        foreach($this->kids as $val){
                                $this->whos_ya_mamma();
                        }
                }
                $par=$this->parent;
                echo $this->name." says '".$par->name." is my mamma'<br>";
        }
 
$a=new person(
        "mamma",
        array(
                new person(
                        "b",
                        array(
                                new person(
                                        "c"
                                )
                        )
                )
        )
);
 
$a->whos_ya_mamma();
?>

result in a 'document contained no data' browser error with the following
gdb message;
 
Program received signal SIGSEGV, Segmentation fault.
execute (op_array=0x815334c) at ./zend_execute.c:1216
1216 zend_fetch_var_address(&opline->result,
&opline->op1, &opline->op2, Ts, BP_VAR_R ELS_CC);

and the backtrace;

#0 execute (op_array=0x815334c) at ./zend_execute.c:1216
#1 0x40599af2 in execute (op_array=0x815334c) at ./zend_execute.c:1624
#2 0x40599af2 in execute (op_array=0x815334c) at ./zend_execute.c:1624
#3 0x40599af2 in execute (op_array=0x815334c) at ./zend_execute.c:1624
<snip>
#10977 0x40599af2 in execute (op_array=0x815334c) at ./zend_execute.c:1624
#10978 0x40599af2 in execute (op_array=0x814b54c) at ./zend_execute.c:1624
#10979 0x40583f77 in php_execute_script (primary_file=0xbffffb48)
    at main.c:1146
#10980 0x405b04b0 in apache_php_module_main (r=0x8125d30, fd=26,
    display_source_mode=0) at sapi_apache.c:88
#10981 0x405b0d74 in send_php (r=0x8125d30, display_source_mode=0,
    filename=0x8127848 "/home/httpd/htdocs/test.php")
    at mod_php4.c:491
#10982 0x405b0db0 in send_parsed_php (r=0x8125d30) at mod_php4.c:503
#10983 0x8053ffe in ap_invoke_handler ()
#10984 0x806218b in process_request_internal ()
#10985 0x80621e8 in ap_process_request ()
#10986 0x805c258 in child_main ()
#10987 0x805c3b7 in make_child ()
#10988 0x805c4b8 in startup_children ()

I have a userland hack workaround that _copies_ each object to a global array at the end of the constructor (with its own quirks) available on request.

-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: php-dev-unsubscribe <email protected>
For additional commands, e-mail: php-dev-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>