Bug #74020 [NEW]: Foreach-by-reference may create unexpected key references
| From: | icarpenter at leadid dot com | Date: | Tue, 31 Jan 2017 03:08:05 +0000 |
| Subject: | Bug #74020 [NEW]: Foreach-by-reference may create unexpected key references | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-207053@lists.php.net to get a copy of this message | ||
From: icarpenter at leadid dot com
Operating system:
PHP version: 7.0.15
Package: Arrays related
Bug Type: Bug
Bug description:Foreach-by-reference may create unexpected key references
Description:
------------
After a foreach-by-reference, an array may create references to keys if
you create a copy of the original variable (either directly or
indirectly via foreach) and then on that new variable, create a copy of
a key to another key. The original key in that variable will then become
a reference to the same key in the original array.
In PHP 5.6 and 7.1, this can be avoided by either having the foreach run
in a different scope, or unsetting the reference variable after the
loop. PHP 7.0 exhibits this behavior regardless.
Test script:
---------------
https://3v4l.org/mHX7B (Triggers the bug in PHP 7.0)
https://3v4l.org/VLuhn (Triggers the bug in PHP 5.6, 7.0, and
7.1)
Expected result:
----------------
string(3) "foo"
Actual result:
--------------
string(3) "bar"
--
Edit bug report at https://bugs.php.net/bug.php?id=74020&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=74020&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=74020&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=74020&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=74020&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=74020&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=74020&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=74020&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=74020&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=74020&r=support
Expected behavior: https://bugs.php.net/fix.php?id=74020&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=74020&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=74020&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=74020&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=74020&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=74020&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=74020&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=74020&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=74020&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=74020&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=74020&r=mysqlcfg