php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68299 empty foreach modifies array content
Submitted: 2014-10-24 12:19 UTC Modified: 2014-10-24 17:51 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: eric dot prevoteau at ac-strasbourg dot fr Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.5.18 OS: Linux
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: eric dot prevoteau at ac-strasbourg dot fr
New email:
PHP Version: OS:

 

 [2014-10-24 12:19 UTC] eric dot prevoteau at ac-strasbourg dot fr
Description:
------------
This bug occurs at least on PHP 5.5.9, 5.5.16 & 5.6.0 on both gentoo and ubuntu 14.04.

When 2 foreach iterate over the same array with the same variable to store the iteration value and the 1st foreach uses reference on the iteration variable but not the 2nd foreach, at the end of the 2nd foreach, the last value of the array is modified.

the problem occurs:
* when array values are objects or integers
* only if the 1st foreach uses '&' but not the 2nd foreach
* only if the 2 foreach use the same variable to store the iteration value
* on both apache2 module and cli

adding print_r inside the 2nd foreach shows array changes. At each iteration, the last value of the array is modified.

My configuration for PHP 5.6.0 (cli) is the following:
'./configure'  '--prefix=/usr' '--build=x86_64-pc-linux-gnu' '--host=x86_64-pc-linux-gnu' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--datadir=/usr/share' '--sysconfdir=/etc' '--localstatedir=/var/lib' '--prefix=/usr/lib64/php5.6' '--mandir=/usr/lib64/php5.6/man' '--infodir=/usr/lib64/php5.6/info' '--libdir=/usr/lib64/php5.6/lib' '--with-libdir=lib64' '--without-pear' '--enable-maintainer-zts' '--disable-bcmath' '--without-bz2' '--disable-calendar' '--enable-ctype' '--with-curl=/usr' '--enable-dom' '--without-enchant' '--disable-exif' '--enable-fileinfo' '--enable-filter' '--disable-ftp' '--with-gettext=/usr' '--without-gmp' '--enable-hash' '--without-mhash' '--with-iconv' '--disable-intl' '--disable-ipv6' '--enable-json' '--without-kerberos' '--enable-libxml' '--with-libxml-dir=/usr' '--enable-mbstring' '--with-mcrypt=/usr' '--without-mssql' '--with-onig=/usr' '--without-openssl' '--without-openssl-dir' '--enable-pcntl' '--disable-phar' '--enable-pdo' '--disable-opcache' '--without-pgsql' '--enable-posix' '--without-pspell' '--without-recode' '--enable-simplexml' '--disable-shmop' '--without-snmp' '--disable-soap' '--enable-sockets' '--without-sqlite3' '--without-sybase-ct' '--disable-sysvmsg' '--disable-sysvsem' '--disable-sysvshm' '--without-fpm-systemd' '--without-tidy' '--enable-tokenizer' '--disable-wddx' '--enable-xml' '--disable-xmlreader' '--enable-xmlwriter' '--without-xmlrpc' '--without-xsl' '--enable-zip' '--with-zlib=/usr' '--disable-debug' '--without-cdb' '--without-db4' '--disable-flatfile' '--without-gdbm' '--disable-inifile' '--without-qdbm' '--with-freetype-dir=/usr' '--with-t1lib=/usr' '--disable-gd-jis-conv' '--with-jpeg-dir=/usr' '--with-png-dir=/usr' '--without-xpm-dir' '--without-vpx-dir' '--with-gd' '--with-ldap=/usr' '--without-ldap-sasl' '--with-mysql=mysqlnd' '--with-mysqli=mysqlnd' '--with-mysql-sock=/var/run/mysqld/mysqld.sock' '--without-pdo-dblib' '--with-pdo-mysql=mysqlnd' '--without-pdo-pgsql' '--without-pdo-sqlite' '--without-pdo-odbc' '--without-readline' '--without-libedit' '--without-mm' '--with-pic' '--with-pcre-regex=/usr' '--with-pcre-dir=/usr' '--with-config-file-path=/etc/php/cli-php5.6' '--with-config-file-scan-dir=/etc/php/cli-php5.6/ext-active' '--disable-embed' '--enable-cli' '--disable-cgi' '--disable-fpm' '--without-apxs2' 'build_alias=x86_64-pc-linux-gnu' 'host_alias=x86_64-pc-linux-gnu' 'CFLAGS=-march=native '-O2' '-pipe'' 'LDFLAGS=-Wl,-O1 '-Wl,--as-needed'' 'CPPFLAGS=' 'CXXFLAGS=-march=native '-O2' '-pipe''

No loaded modules.

No changes made on php.ini

Test script:
---------------
$a = array();

$a[1] = 1;
$a[2] = 3;
$a[3] = 5;
$a[4] = 7;

foreach($a as &$v)
{
}

foreach($a as $v)
{
}

print_r($a);



Expected result:
----------------
Array
(
    [1] => 1
    [2] => 3
    [3] => 5
    [4] => 7
)


Actual result:
--------------
Array
(
    [1] => 1
    [2] => 3
    [3] => 5
    [4] => 5
)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-10-24 17:51 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-10-24 17:51 UTC] requinix@php.net
After the first foreach ends $v will still be a reference (to the last element in the array). When the second foreach starts updating $v with values it will also update the last element in the array; if you dump $a in the loop you'll see it change like
  [1, 3, 5, 1]
  [1, 3, 5, 3]
  [1, 3, 5, 5]
  [1, 3, 5, 5]

Either don't use references or unset($v) after the first loop.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jun 02 07:01:30 2024 UTC