From: Operating system: windows xp PHP version: 5.3.2 Package: SimpleXML related Bug Type: Bug Bug description:SimpleXMLElement iterator produces unexpected results
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 bug report at http://bugs.php.net/bug.php?id=51846&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=51846&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=51846&r=trysnapshot53 Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=51846&r=trysnapshot60 Fixed in SVN: http://bugs.php.net/fix.php?id=51846&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=51846&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=51846&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=51846&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=51846&r=needscript Try newer version: http://bugs.php.net/fix.php?id=51846&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=51846&r=support Expected behavior: http://bugs.php.net/fix.php?id=51846&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=51846&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=51846&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=51846&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=51846&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=51846&r=dst IIS Stability: http://bugs.php.net/fix.php?id=51846&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=51846&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=51846&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=51846&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=51846&r=mysqlcfg