php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51846 SimpleXMLElement iterator produces unexpected results
Submitted: 2010-05-18 02:37 UTC Modified: 2013-10-24 08:01 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: henning at glatter-gotz dot com Assigned: yohgaki (profile)
Status: Closed Package: SimpleXML related
PHP Version: 5.3.2 OS: windows xp / Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: henning at glatter-gotz dot com
New email:
PHP Version: OS:

 

 [2010-05-18 02:37 UTC] henning at glatter-gotz dot com
Description:
------------
When loading an xml document with simplexml_load_string() that contains 2500 or 
more child elements, iterating over these 
with a foreach loop and pushing results into an array results in 4998 array 
entries, when only 2500 are expected. See sample 
code for the exact conditions under which this occurs.

There are two foreach loops, the first behaves as expected and results in an 
array of exactly 2500 objects of type A, 
whereas the second foreach loop that instantiates an object of type B in each 
iteration ends up being of size 4998.

Note that the object B takes two parameters in the constructor. This behavior is 
only exhibited if both parameters are 
arrays. If one is changed to an int for example the code runs as expected.

Also, if the xml files contains 2499 elements or less, the code also works as 
expected.
Executed on the command line in the following environments:

1) Windows XP, PHP 5.3.1 (cli) (built: Nov 20 2009 17:26:32) -> FAIL
2) Windows XP, PHP 5.3.2 (cli) (built: Mar  3 2010 19:40:13) -> FAIL
3) PHP 5.2.10-2ubuntu6.4 with Suhosin-Patch 0.9.7 (cli) (built: Jan  6 2010 
22:56:44) -> SUCCESS

Setups 1 and 3 are production environments and have slightly modified php.ini 
files. Setup 2 is an "out of the box" download 
of VC6 x86 Thread Safe (2010-Mar-04 20:11:08) from 
http://windows.php.net/download/. No changes were made to it.

Test script:
---------------
class A
{}

class B
{
   protected $v1;
   protected $v2;
   
   public function __construct($v1, $v2) {
      $this->v1 = $v1;
      $this->v2 = $v2;
   }
}

$xml_string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><rows>";

for ($i = 0; $i < 2500; ++$i) {
   $xml_string .= "<row>bla</row>";
}

$xml_string .= "</rows>";
$xml = simplexml_load_string($xml_string);
$a1 = array();
$a2 = array();

foreach ($xml->row as $r) {
   $a1[] = new A();
}

foreach ($xml->row as $r) {
   $val1 = array();
   $val2 = array();
   $a2[] = new B($val1, $val2);
}

echo 'count(a1) = '.count($a1).PHP_EOL;
echo 'count(a2) = '.count($a2).PHP_EOL;

Expected result:
----------------
count(a1) = 2500
count(a2) = 2500

Actual result:
--------------
count(a1) = 2500
count(a2) = 4998

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-05-18 02:53 UTC] henning at glatter-gotz dot com
Now also tested on PHP 5.3.2-1ubuntu4.1 with Suhosin-Patch (cli) (built: May  4 2010 06:56:22). It fails with the same result as on Windows.
 [2010-05-18 02:56 UTC] henning at glatter-gotz dot com
-Operating System: windows xp +Operating System: windows xp / Linux
 [2010-05-18 02:56 UTC] henning at glatter-gotz dot com
Added Linux (Ubuntu) to the OS field.
 [2010-05-18 02:58 UTC] felipe@php.net
-Status: Open +Status: Feedback
 [2010-05-18 02:58 UTC] felipe@php.net
Please try using this snapshot:

  http://snaps.php.net/php5.3-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/


 [2010-05-18 03:15 UTC] henning at glatter-gotz dot com
-Status: Feedback +Status: Open
 [2010-05-18 03:15 UTC] henning at glatter-gotz dot com
For windows I downloaded the latest available VC6 x86 Thread Safe (2010-Mar-04 
20:11:08) binaries, and tried it (see original report).
I cannot build from Source on Linux, I do not have sufficient access to a system 
to do that. Sorry.
But I did test on two different OS' and 3 different versions of PHP.
 [2010-05-18 09:39 UTC] mike@php.net
Probably related to Bug #50670
 [2010-05-18 17:40 UTC] henning at glatter-gotz dot com
I agree, it is possible that this is indeed related or even the same
problem as 50670. I ran some more test on documents with more than
10k elements and there both of my test cases fail, even the one that
works for smaller sets.

Did not think to look for issues related to the script Engine, so I
did not find that bug.

Maybe you can mark this one as a dupe and I will vote on the 50670.

Thanks!
 [2013-10-24 08:01 UTC] yohgaki@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: yohgaki
 [2013-10-24 08:01 UTC] yohgaki@php.net
It seems fixed already at least on my 5.5.4/linux.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC