Bug #51846 [Opn]: SimpleXMLElement iterator produces unexpected results

From: Date: Tue, 18 May 2010 00:56:11 +0000
Subject: Bug #51846 [Opn]: SimpleXMLElement iterator produces unexpected results
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-150529@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=51846&edit=1

 ID:               51846
 User updated by:  henning at glatter-gotz dot com
 Reported by:      henning at glatter-gotz dot com
 Summary:          SimpleXMLElement iterator produces unexpected results
 Status:           Open
 Type:             Bug
 Package:          SimpleXML related
-Operating System: windows xp
+Operating System: windows xp / Linux
 PHP Version:      5.3.2

 New Comment:

Added Linux (Ubuntu) to the OS field.


Previous Comments:
------------------------------------------------------------------------
[2010-05-18 02:53:34] 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:37:26] 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


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=51846&edit=1


Thread (8 messages)

« previous php.bugs (#150529) next »