php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22237 PHP crashes when class references property using variable variable
Submitted: 2003-02-15 19:06 UTC Modified: 2003-04-19 20:06 UTC
Votes:5
Avg. Score:4.8 ± 0.4
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:1 (33.3%)
From: peter at globalvision dot com dot au Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5CVS-2003-02-15 (dev) OS: All
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: peter at globalvision dot com dot au
New email:
PHP Version: OS:

 

 [2003-02-15 19:06 UTC] peter at globalvision dot com dot au
<?php
class myClass
{
    var $foo = "hi";
    var $bar = "foo";

    function test()
    {
      echo $this->$bar;      //line 1
      //echo $this->foo;     //line 2
    }
}
?>


commenting out line 1 (and optionally including line 2) cause it to run normally. The crash occurs as soon as you
include $this->$<something>.


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

From a dos command line on Windows XP Pro SP1 (build 2600):

>php c:\temp\t.php

I get

   PHP Script Interpreter has encountered a problem and
   needs to close (pop up).

I'm using the windows build from the snaps page. I've had this error over the past few days of snaps too.

>php -m
[PHP Modules]
bcmath
calendar
com
ctype
ftp
mysql
odbc
pcre
rpc
session
standard
tokenizer
wddx
xml
zlib

[Zend Modules]


The crash details are:

AppName: php.exe
AppVer: 5.0.0.0
ModName: php4ts.dll
ModVer: 5.0.0.0
Offset: 000b3bdc

Exception Information:
Code: 0xc0000005  Flags: 0x00000000
Record: 0x000000000000000    Address: 0x000000000100b3bdc


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-02-15 19:15 UTC] sniper@php.net
No crash with PHP 4.3.1-dev, does crash with PHP 5-dev.

 [2003-03-23 06:46 UTC] peter at globalvision dot com dot au
Well... I have not had the time to solve it, but I now know pretty well why it is caused.

**can someone please have a crack at fixing this**?

In zend_language_parser.y, the rule for object_property is either an object_dim_list (which can be a T_STRING) or it is a "variable_without_ibjects".

object_dim_list works for $this->foo, but $this->$foo is matched by variable_without_objects.

HOWEVER, variable_without_objects is the match for vanilla variables, so it consumes one '$' before it dereferences.

in other words, $this->$foo sends the $foo part off to the standard variable compiler area which does not recognise that $foo is a reference.

I experimented by placing a couple of rules in the lex scanner as follows:

<ST_LOOKING_FOR_PROPERTY>"$" {

fprintf(stderr,"xxx");

  yy_push_state(ST_TEST)
  return '$';
}

and

<ST_TEST>";" {

fprintf(stderr,"yyy");

  yy_pop_state(TSRMLS_C);
  return ';';
}
<ST_TEST>{LABEL} {

fprintf(stderr,"zzz");

   zendlval->value.str.val = (char *)estrndup(yytext,yyleng);
   zendlval->value.str.len = yyleng;
   zendlval->type = IS_STRING;
   return T_VARIABLE;
}

and suddenly, it all seems to parse, however I'm clearly missing something - it still does not display the forrect output for

  echo $this->$foo;

What am I doing wrong?
 [2003-04-18 18:14 UTC] thekid at thekid dot de
No crash here.

In line 1, I assume you're expecting "hi" as an output. This will of course not work as $bar is not set.

Modify line 1 to read:
  echo $this->{$this->bar}

and you will have your expected output. Note that with error_reporting(E_ALL) you would notice such kinds of mistakes. Also, var_dump() helps a lot, you can actually see type information instead of just nothing appearing:)

thekid@friebes:~ > cat | php5
<?php
class myClass
{
    var $foo = "hi";
    var $bar = "foo";

    function test()
    {
      var_dump($this->{$this->bar});
      var_dump($this->foo);
    }
}

$c= new myClass();
$c->test();
?>
string(2) "hi"
string(2) "hi"
 [2003-09-29 07:31 UTC] rep at devdomain dot com
Not closed! Using September's 28 PHP5 CVS:

PHP Fatal error:  Method name must be a string in /www/var/template/%%-16/%%-1606537564/Contact.en.tpl.php

And the php file goes like this: 

<?php echo $this->_plugins['function']['mailto'][0](array('address' => "here@email.com",'encode' => 'javascript'), $this) ; ?>

(Smarty template.)
With this postfilter it works fine:

function dev_smarty_php5b1_fix_postfilter ($sTpl, &$oSmarty) {

  return preg_replace (
    '/echo (\$this-\>_plugins\[\'\w+\'\]\[\'\w+\'\]\[\d+\])/',
    '$dev_php5b1_fix = "{\1}"; echo $dev_php5b1_fix', $sTpl);
}
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Sep 19 21:01:26 2024 UTC