php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40768 nested foreach break -- infinite loop - serious
Submitted: 2007-03-09 18:13 UTC Modified: 2007-05-20 12:14 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: abc at def dot ghi Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5.2.1 OS: winxp
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: abc at def dot ghi
New email:
PHP Version: OS:

 

 [2007-03-09 18:13 UTC] abc at def dot ghi
Description:
------------
i use just build i downloaded from site (5.2.1) no exts, no optimizers.
it seem the $a,$b keep same internal pointer, this is quite serious error cos $b=$a should create separate array copy and it does but without the inner pointer.
if you replace $b=$a with $b=array(1,2,3); it works as expected

this was reported and ignored by someone on solaris before as well-- #40608


Reproduce code:
---------------
$a=array(1,2,3);
$b=$a;
	
foreach($a as $A)
{
     foreach($b as $B)
      {
	echo "$A,$B\n";
	break;
      }
}

Expected result:
----------------
1,1
2,1
3,1

Actual result:
--------------
1,1
2,1
3,1
2,1
3,1
2,1
3,1
--infinite-

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-03-09 18:20 UTC] abc at def dot ghi
wonder if someone can check this on linux build if its win issue.
 [2007-03-09 18:30 UTC] abc at def dot ghi
me again,
i just tried latest snapshot and it works fine there so i guess new snapshot should be released as i walked around some other strange things in that current 5.2.1.
 [2007-03-19 19:56 UTC] ashnazg@php.net
Found this bug when looking at issue on PHP bug #40608.

Tested this example against the 5.2 snapshot (php5.2-win32-200703191630.zip) from php.net, and it works properly.  So, looks like it has been fixed on the 5.2 branch after 5.2.1 was released.
 [2007-05-20 12:14 UTC] cipri@php.net
A quick fix for those that can't update, is to access the key in the inner loop:

$a=array(1,2,3);
$b=$a;

foreach($a as $A)
{
     foreach($b as $B)
      {
        key($b);
        echo "$A,$B\n";
        break;
      }
}

Ouputs:
1,1
2,1
3,1
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Nov 24 22:01:33 2024 UTC