php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38287 static variables mess up global vars
Submitted: 2006-08-01 18:57 UTC Modified: 2006-08-07 15:15 UTC
From: steemann at globalpark dot de Assigned: dmitry (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.2.0RC1 OS: Linux 2.6.12-10-686
Private report: No CVE-ID: None
 [2006-08-01 18:57 UTC] steemann at globalpark dot de
Description:
------------
Variables in the global scope seem to keep corrupted when using static  
properties/variables.  
  
The script below has a global variable $not_there which is supposed to be NULL  
as it is not set. It is NULL when var_dumping it.  
  
Although the variable is not set by the script, the variable value changes during  
execution so the script evaluates   
if ($not_there["invalid_var"]))  
to true obviously. When printing a hash of the variable ("use_authmodule" which  
does not exist), the variable seems to be an array but it should be NULL.  
  
btw: magic_quotes_gpc is turned on. 

Reproduce code:
---------------
something::do_something((int) $argv[1]);

// $not_there is really NULL
var_dump($not_there);

// error occurs here: execution should never get inside the if condition because $not_there is NULL
if ($not_there["invalid_var"])
{
  // will print NULL (which is ok, but execution should never get here if the value is NULL)
  var_dump($not_there["use_authmodule"]);
  // will print "PATH:Array"
  print "PATH:".$not_there["use_authmodule"];
}

class something
{
  protected static $static_var=array();

  public static function get_object()
  {
    static $object=NULL;
    if ($object===NULL)
      $object=new something;
    return $object;
  }

  public static function do_something($version=0)
  {
    if ($version==0)
    {
      // $argv[1]==0: this will cause the error
      foreach (array("g"=>&$_GET,"p"=>&$_POST,"r"=>&$_REQUEST) as $var_name=>$super_global)
        self::get_object()->static_var[]=$var_name;
    } // end of version 0

    if ($version==1)
    {
      // $argv[1]==1: this does the same but will not cause the error
      $obj=self::get_object();
      foreach (array("g"=>&$_GET,"p"=>&$_POST,"r"=>&$_REQUEST) as $var_name=>$super_global)
        $obj->static_var[]=$var_name;
    } // end of version 1
  } // end of do_something method
} // end of class something

Expected result:
----------------
expected result for $argv[1]==0:  
NULL   
  
  
result for $argv[1]==1:  
NULL  
 

Actual result:
--------------
Call the script above on the command line with $argv[1]=0.  
Script execution gets into the "if ($not_there...)" part as the global variable 
$not_there gets corrupted. 
 
result is:  
NULL 
NULL   
PATH:Array   
  
 
result for $argv[1]=1:  
NULL  
 
 
So the problem does not occur if the script is called with $argv[1] = 1. 
    
The $argv interpretation and actual difference in the code is in method    
do_something() of class "something". 
    
I know the static property $static_var of class "something" should not be    
accessed via -> because it is static, but the code worked fine in PHP 5.1   
and also works fine when using version 1 ($argv[1]=1). 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-08-01 19:12 UTC] tony2001@php.net
Dmitry, could you please take a look at it?
There are also several leaks, which seem to be related to the issue.
 [2006-08-07 15:15 UTC] dmitry@php.net
Fixed in CVS HEAD and PHP_5_2.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 23 07:01:29 2024 UTC