Bug #79264 [NEW]: Value assigned by reference in foreach is do not overwrite to removed variable
| From: | juanparati at gmail dot com | Date: | Wed, 12 Feb 2020 10:18:39 +0000 |
| Subject: | Bug #79264 [NEW]: Value assigned by reference in foreach is do not overwrite to removed variable | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-225523@lists.php.net to get a copy of this message | ||
From: juanparati at gmail dot com
Operating system: Linux
PHP version: 7.3.14
Package: Scripting Engine problem
Bug Type: Bug
Bug description:Value assigned by reference in foreach is do not overwrite to removed variable
Description:
------------
It's very well documented that behaviour of variables passed by
reference into a foreach can have unexpected behaviours when the
variables were previosly used.
From PHP documentation:
"Warning
Reference of a $value and the last array element remain even after the
foreach loop. It is recommended to destroy it by unset(). Otherwise you
will experience the following behavior"
However I found that in some cases even when the variable used as
reference is previously removed using "unset" the issue persist.
I can reproduce this issue from PHP 5.6 TO PHP 7.4 with the same
result.
It seems that if variables previously are removed (unset) and used again
as reference into the foreach can have a wrong behaviour.
This issue is probably due the way that PHP keep the internal
references.
Test script:
---------------
See the following code
https://3v4l.org/h6d0G
The array subnodes "needle" and "haystack" were automatically appended
for the first element of the array even if the variable used as
reference was previously removed (unset).
In the following example the result is the expected one when the
variables were not previously used:
https://3v4l.org/4kRmH
Expected result:
----------------
array(2) {
["18427e904f7a227e8c3b80dd2858a70e"]=>
array(2) {
["needle"]=>
array(1) {
[0]=>
array(3) {
["starts_at"]=>
int(11821)
["ends_at"]=>
int(14377)
["length"]=>
int(2556)
}
}
["haystack"]=>
array(1) {
[0]=>
array(4) {
["starts_at"]=>
int(12227)
["ends_at"]=>
int(14783)
["length"]=>
int(2556)
["original"]=>
string(3) "foo"
}
}
}
["dd0c26f35ebf55de25eabea3edbfba32"]=>
array(2) {
["needle"]=>
array(1) {
[0]=>
array(3) {
["starts_at"]=>
int(17255)
["ends_at"]=>
int(32431)
["length"]=>
int(15176)
}
}
["haystack"]=>
array(1) {
[0]=>
array(4) {
["starts_at"]=>
int(17776)
["ends_at"]=>
int(33032)
["length"]=>
int(15256)
["original"]=>
string(3) "foo"
}
}
}
}
Actual result:
--------------
array(2) {
["18427e904f7a227e8c3b80dd2858a70e"]=>
array(2) {
["needle"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(4) {
["starts_at"]=>
int(17255)
["ends_at"]=>
int(32431)
["length"]=>
int(15176)
["original"]=>
string(3) "foo"
}
}
}
["haystack"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(4) {
["starts_at"]=>
int(17776)
["ends_at"]=>
int(33032)
["length"]=>
int(15256)
["original"]=>
string(3) "foo"
}
}
}
}
["dd0c26f35ebf55de25eabea3edbfba32"]=>
array(2) {
["needle"]=>
array(1) {
[0]=>
array(3) {
["starts_at"]=>
int(17255)
["ends_at"]=>
int(32431)
["length"]=>
int(15176)
}
}
["haystack"]=>
array(1) {
[0]=>
array(3) {
["starts_at"]=>
int(17776)
["ends_at"]=>
int(33032)
["length"]=>
int(15256)
}
}
}
}
--
Edit bug report at https://bugs.php.net/bug.php?id=79264&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=79264&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=79264&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=79264&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=79264&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=79264&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=79264&r=support
Expected behavior: https://bugs.php.net/fix.php?id=79264&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=79264&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=79264&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=79264&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=79264&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=79264&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=79264&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=79264&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=79264&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=79264&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=79264&r=mysqlcfg