php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27268 Bad references accentuated by clone
Submitted: 2004-02-15 21:06 UTC Modified: 2005-06-23 13:11 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: lingwitt at bellsouth dot net Assigned: dmitry
Status: Closed Package: Scripting Engine problem
PHP Version: 5CVS-2005-06-19 OS: *
Private report: No CVE-ID:
 [2004-02-15 21:06 UTC] lingwitt at bellsouth dot net
Description:
------------
When an object's method calls upon another one of its 
methods such that the second method returns a reference 
that is stored in the the first method as a local 
variable, then the reference persists in some way so as 
to make cloning problematic.

As a result, a modification to the referenced variable 
in the clone cuases a modification to the same variable 
in original.

Reproduce code:
---------------
class A
{
    var $a = array();
    
    public function makeAReference()
    {
        $array = $this->getA();
    }
    
    public function &getA()
    {
        return $this->a;
    }
}

$A = new A;
$A->a = array(1);
$A->makeAReference();
$clone = clone $A;
$clone->a = array();

print_r($A);

Expected result:
----------------
This is gotten when $A->makeAReference() is removed.

A Object
(
    [a] => Array
        (
            [0] => 1
        )

)

Actual result:
--------------
Obviously the modification made it back to the original.

A Object
(
    [a] => Array
        (
        )

)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-02-15 23:47 UTC] lingwitt at bellsouth dot net
In fact, the reference doesn't need to be made in a 
method:

class A
{
    var $a = array();
    
    public function &getA()
    {
        return $this->a;
    }
}

$A = new A;
$A->a = array(1);
$array = $A->getA();
$clone = clone $A;
$clone->a = array();

print_r($A);
 [2005-05-25 12:32 UTC] ericvanblokland at gmail dot com
Isn't this related to

http://bugs.php.net/bug.php?id=24485

when copies become references? (Clone should copy variables from an object, but because of the bug mentioned above, variables can no longer be copied, just referenced)
 [2005-06-19 21:18 UTC] sniper@php.net
Still happens..

 [2005-06-23 10:25 UTC] dmitry@php.net
Fixed in cvs HEAD (not in PHP_5_0).

The simular patch (http://cvs.php.net/diff.php/ZendEngine2/zend_execute.c?r1=1.707&r2=1.708&ty=u) probably can be applyed to PHP_5_0 too.
 [2005-06-23 13:11 UTC] sniper@php.net
Forget PHP_5_0, HEAD is enough. :)

 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Thu Apr 17 18:02:13 2014 UTC