Bug #80826 [NEW]: Array member reference affecting pass by value assignment
| From: | tshumbeo at mailhouse dot biz | Date: | Wed, 03 Mar 2021 19:46:30 +0000 |
| Subject: | Bug #80826 [NEW]: Array member reference affecting pass by value assignment | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-232521@lists.php.net to get a copy of this message | ||
From: tshumbeo at mailhouse dot biz
Operating system: Windows & Linux
PHP version: 7.4.15
Package: *General Issues
Bug Type: Bug
Bug description:Array member reference affecting pass by value assignment
Description:
------------
See the test script. When (1) is not present (leaving the member
reference in the outside scope when f() is called), the assignment
inside f() is able to modify the passed array even though it doesn't
accept it by reference (note "&array(1)" in Actual Result). Removing the
reference explicitly with (1) avoids this problem.
Test script:
---------------
<?php
$a = [1, 2, 3];
foreach ($a as &$reference) {
$reference = (object) ['x' => $reference];
}
// (1)
//unset($reference);
f($a);
var_dump($a);
function f($a) {
foreach ($a as $k => $v) {
$a[$k] = [$v];
}
}
Expected result:
----------------
array(3) {
[0]=>
object(stdClass)#1 (1) {
["x"]=>
int(1)
}
[1]=>
object(stdClass)#2 (1) {
["x"]=>
int(2)
}
[2]=>
object(stdClass)#3 (1) {
["x"]=>
int(3)
}
}
Actual result:
--------------
array(3) {
[0]=>
object(stdClass)#1 (1) {
["x"]=>
int(1)
}
[1]=>
object(stdClass)#2 (1) {
["x"]=>
int(2)
}
[2]=>
&array(1) { // !!!
[0]=>
object(stdClass)#3 (1) {
["x"]=>
int(3)
}
}
}
--
Edit bug report at https://bugs.php.net/bug.php?id=80826&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=80826&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=80826&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=80826&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=80826&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=80826&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=80826&r=support
Expected behavior: https://bugs.php.net/fix.php?id=80826&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=80826&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=80826&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=80826&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=80826&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=80826&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=80826&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=80826&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=80826&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=80826&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=80826&r=mysqlcfg