php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39949 Returned reference cloned if stored in variable
Submitted: 2006-12-26 04:29 UTC Modified: 2006-12-26 13:16 UTC
Votes:2
Avg. Score:3.5 ± 1.5
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: ap at tellusion dot com Assigned: dmitry (profile)
Status: Not a bug Package: Variables related
PHP Version: 5CVS-2006-12-26 (snap) OS: Linux Red Hat EL
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ap at tellusion dot com
New email:
PHP Version: OS:

 

 [2006-12-26 04:29 UTC] ap at tellusion dot com
Description:
------------
If a function (or object method) returns a reference (as a return value or by writing to a reference supplied as a parameter), and the caller saves that reference in a variable, the object/array/variable that is referenced is incorrectly cloned.

Interestingly, if the return value is passed directly to 2nd function (or object method) the bug does not occur, that is, the variable is not cloned and the reference is passed to 2nd function in-tact.


Reproduce code:
---------------
I have provided two scripts to reproduce the bug. One is a procedural example (reftest-array2.php) and the other is an OO example (reftest-object2.php).

They can be downloaded from http://www.tellusion.com/reftest2.tar

ISOLATION

This is potentially related but still separate to bug #39944 (which was fixed). I have modified two of the scripts I submitted previosuly to clearly demonstrate the presence of this bug only.

The bug is present in PHP 5.0.4 and all versions of PHP 5.1 & 5.2 including the latest version of 5.2-STABLE.

I haven?t tested any older versions of PHP.

Expected result:
----------------
### reftest-array2.php ###

arrTheArray = Array
(
    [0] => xx1
    [1] => xx2
    [2] => xx3
)

### reftest-object2.php ###

RefTest::addToArray(): arr = Array
(
    [0] => xx1
)

RefTest::addToArray(): arr = Array
(
    [0] => xx1
    [1] => xx2
)

RefTest::addToArray(): arr = Array
(
    [0] => xx1
    [1] => xx2
    [2] => xx3
)

objRefTest->getArray() = Array
(
    [0] => xx1
    [1] => xx2
    [2] => xx3
)

Actual result:
--------------
### reftest-array2.php ###

arrTheArray = Array
(
    [0] => xx3
)

### reftest-object2.php ###

RefTest::addToArray(): arr = Array
(
    [0] => xx1
)

RefTest::addToArray(): arr = Array
(
    [0] => xx2
)

RefTest::addToArray(): arr = Array
(
    [0] => xx3
)

objRefTest->getArray() = Array
(
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-12-26 13:16 UTC] dmitry@php.net
$a = getTheArray1() doesn't make $a to be a reference to $arrTheArray. Use "=&" instead of "=".

$a =& getTheArray1();

getTheArray2($b) doesn't make $a to be a reference to $arrTheArray. It does the same as $b = $arrTheArray and it assign a value to $b (not a reference). And it is not possible to assign by reference to argument passed by reference.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 03 18:01:34 2024 UTC