php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62639 XML structure broken
Submitted: 2012-07-23 11:16 UTC Modified: 2013-04-24 07:48 UTC
Votes:107
Avg. Score:4.8 ± 0.5
Reproduced:97 of 100 (97.0%)
Same Version:64 (66.0%)
Same OS:47 (48.5%)
From: alexshock at yandex dot ru Assigned: acurioso
Status: Assigned Package: SimpleXML related
PHP Version: 5.4.5 OS: debian 6.0.5
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2012-07-23 11:16 UTC] alexshock at yandex dot ru
Description:
------------
In PHP 5.4.0 and higher SimpleXMLElement works strange when trying to obtain children nodes (see test.php source for details).
Works fine in 5.3.x, I found that this issue occurs after this commit in php git repo: 1e3b32c777829f61fa9a18278e0647e9112d96ea

Test script:
---------------
https://www.dropbox.com/s/j0i47y7q4n4g0g6/test.php


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-07-27 21:23 UTC] willfitch@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: willfitch
 [2012-07-27 21:27 UTC] willfitch@php.net
Confirmed in 5.4.5.  In the process of verifying the change(s) that introduced 
this issue:

--------------------------------------------------------------------------------
object(A)#2 (1) {
  ["@attributes"]=>
  array(1) {
    ["attr"]=>
    string(9) "Some Attr"
  }
}
--------------------------------------------------------------------------------
object(A)#3 (2) {
  ["@attributes"]=>
  array(1) {
    ["attr"]=>
    string(9) "Some Attr"
  }
  [0]=>
  string(10) "Some Value"
}
--------------------------------------------------------------------------------
 [2012-07-29 23:16 UTC] willfitch@php.net
Andrew -

Would you mind taking a look at this? I reverted 
1e3b32c777829f61fa9a18278e0647e9112d96ea locally, and array typecasting works 
fine without the patch.  Also, this patch causes the last child in an element to 
be skipped in the case pointed out in this 
report.  I wanted to get your input before I move forward with any changes.
 [2012-07-29 23:16 UTC] willfitch@php.net
-Assigned To: willfitch +Assigned To: acurioso
 [2012-07-29 23:31 UTC] acurioso@php.net
-Summary: XML structure broken +Summary: Cannot call constructor -Status: Assigned +Status: Open -Package: SimpleXML related +Package: SPL related -Operating System: debian 6.0.5 +Operating System: Windows XP -PHP Version: 5.4.5 +PHP Version: 5.3.6
 [2012-07-29 23:44 UTC] acurioso@php.net
After reverting locally do all the unit test cases for SimpleXML still pass? This was not the case prior to the patch (one test was failing).

I'll work on reverting the change locally on my end tonight and see if I can reproduce the bug and fix.
 [2012-07-29 23:47 UTC] acurioso@php.net
-Summary: Cannot call constructor +Summary: XML structure broken -Package: SPL related +Package: SimpleXML related -Operating System: Windows XP +Operating System: debian 6.0.5 -PHP Version: 5.3.6 +PHP Version: 5.4.5
 [2012-07-30 00:58 UTC] acurioso@php.net
I can confirm that reverting that patch does fix the bug; however, it causes the original test to fail again:

=====================================================================
FAILED TEST SUMMARY
---------------------------------------------------------------------
SimpleXML: cast to array [ext/simplexml/tests/034.phpt]
Bug #51615 (PHP crash with wrong HTML in SimpleXML) [ext/simplexml/tests/bug51615.phpt]
=====================================================================

See: ext/simplexml/tests/034.phpt for more information.

Ignore bug51615.phpt for now. That is just a side effect of test 34.

I would actually prefer if I could take the first stab at this bug myself since it was clearly introduced in my code. I'd be hesitate to just revert the changes since that would break test 34.
 [2012-07-30 01:57 UTC] willfitch@php.net
I think that would make more sense.  Thanks, Andrew.
 [2013-04-24 07:48 UTC] laruence@php.net
I think we should revert the previous fix at now, since this new bug is more 
critical than the previous one.

and I think the key problem there is that we can not tell iterating from 
convert_to_array in the sxe_get_prop_hash

without that, I don't think we can get a good fix for the 034.phpt
 [2013-06-14 00:21 UTC] info at fuktommy dot com
It causes not only wrong var_dump and print_r, but also wrong boolean cast.

Test script:
------------
$xml = <<<XML
<?xml version="1.0"?>
<a>
    <b>Some String</b>
</a>
XML;

$b = simplexml_load_string($xml)->xpath('/a/b');
var_dump((string)$b[0]);
var_dump((bool)$b[0]);

Expected result:
----------------
string(11) "Some String"
bool(true)

Actual result:
--------------
string(11) "Some String"
bool(false)
 [2013-08-16 01:59 UTC] sergey at shymko dot net
The following workaround seems to fix both dumping and type casting:

<?php

class SimpleXMLElement_CloneFix extends SimpleXMLElement
{
    public function xpath($path)
    {
        $result = parent::xpath($path) ?: array();
        foreach ($result as $node) {
            $this->_fixNode($node);
        }
        return $result;
    }

    protected function _fixNode(SimpleXMLElement $node)
    {
        if (!$node && (string)$node) {
            $dom = dom_import_simplexml($node);
            $dom->parentNode->replaceChild(clone $dom, $dom);
        }
    }
}

Unfortunately, the same approach does not work for children() method, because of mysterious nature of its returned value.
Despite returned object manifests itself as an instance of SimpleXMLElement, it does not behave as such.
For instance, it is incompatible with dom_import_simplexml().
So, what kind of SimpleXMLElement is that?
 [2013-12-29 20:34 UTC] knyttl at goout dot cz
Is there something happening with this issue? It is now in stable debian and others :/
 [2014-02-11 11:33 UTC] electroglyph at gmail dot com
???
 [2014-03-29 20:47 UTC] knyttl at goout dot cz
Why you develop new features, when the old ones are unusable?
 [2014-04-09 10:42 UTC] pavel at icewarp dot com
So any update? this issue is hanging here for almost two years ? Will you fix it ?
 [2014-04-09 17:29 UTC] knyttl at goout dot cz
Thanks for your replies.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Fri Apr 18 03:02:48 2014 UTC