#27268 [Com]: Bad references accentuated by clone

From: Date: Tue, 09 Mar 2004 12:42:52 +0000
Subject: #27268 [Com]: Bad references accentuated by clone
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-56155@lists.php.net to get a copy of this message
 ID:               27268
 Comment by:       splab at bach-mortensen dot dk
 Reported By:      lingwitt at bellsouth dot net
 Status:           Open
 Bug Type:         Zend Engine 2 problem
 Operating System: *
 PHP Version:      5CVS-2004-02-18
 New Comment:

Clone doesnt seem to act as expected on domdocument - we want a copy of
the entire document but get what seems to be root node - and segfault
if we try to work on it:
$dom = new domdocument();
$dom->load("somexml.xml");
print $dom->getElementsByTagName("body")->length . "\n";//1
$dom2 = clone $dom;
$dom2->getElementsByTagName("body")->length; //Segfault

somexml.xml:
<body>
  <paragraf>
    test test test
  </paragraf>
</body>


Previous Comments:
------------------------------------------------------------------------

[2004-02-15 23:47:13] 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);

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

[2004-02-15 21:06:03] 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
        (
        )

)


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


-- 
Edit this bug report at http://bugs.php.net/?id=27268&edit=1


Thread (10 messages)

« previous php.bugs (#56155) next »