php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27395 reference to an array index makes the array to be passed by reference always
Submitted: 2004-02-25 12:02 UTC Modified: 2004-02-25 19:58 UTC
From: jamesn at tocquigny dot com Assigned: zeev (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4CVS, 5CVS (2004-02-25 OS: *
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jamesn at tocquigny dot com
New email:
PHP Version: OS:

 

 [2004-02-25 12:02 UTC] jamesn at tocquigny dot com
Description:
------------
There appears to be a problem with php that doesn't allow arrays to be passed by value.  The fix appears to be something similar to: http://bugs.php.net/bug.php?id=6417
if (PZVAL_IS_REF(*p))
{
   SEPARATE_ZVAL(p);
} else {
   zval_add_ref(p);
}

It would appear that that logic is missing in one place or another.  I could not track it down myself.  Code similar to this appears to be copy/pasted in various places.

Reproduce code:
---------------
$array = array(1);

// This line makes the call to theFunction() act as if passed by ref.?
$reference =& $array[0];

echo $array[0], '<br>';
theFunction($array);

echo $array[0], '<br>';

function theFunction($array) {
    $array[0] = 2;
}

Expected result:
----------------
you should get 1 and 1

Actual result:
--------------
instead you get 1 and 2!

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-02-25 13:39 UTC] sniper@php.net
Wrong test, this was the one:

<?php

function theFunction($arg) {
    $arg[0] = 2;
}

// Reference on array index
$arr1 = array (1);
$reference1 =& $arr1[0];
    
var_dump($reference1);
var_dump($arr1);
theFunction($arr1);
var_dump($reference1);
var_dump($arr1);

echo "--------\n";

// Reference on array
$arr2 = array (1);
$reference2 =& $arr2;
    
var_dump($reference2);
var_dump($arr2);
theFunction($arr2);
var_dump($reference2);
var_dump($arr2);

?>

 [2004-02-25 19:58 UTC] sniper@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.

See bug #20993 (where you seem to have got the example too..)


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 16:01:31 2024 UTC