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: 2015-05-27 17:48 UTC
Votes:123
Avg. Score:4.8 ± 0.5
Reproduced:112 of 115 (97.4%)
Same Version:68 (60.7%)
Same OS:51 (45.5%)
From: alexshock at yandex dot ru Assigned: cmb (profile)
Status: Duplicate Package: SimpleXML related
PHP Version: 5.4.5 OS: debian 6.0.5
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
29 + 31 = ?
Subscribe to this entry?

 
 [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.
 [2014-04-27 18:41 UTC] knyttl at goout dot cz
PHP - the reliable language of the future.
 [2014-10-09 09:42 UTC] info at berany dot com
Will this ever be fixed?
 [2014-12-09 14:32 UTC] homer dot dong at gmail dot com
This bug still exists in any PHP versions higher than PHP 5.4.0. Tried 5.6.3, 5.4.35 and 5.5.19 and all return the same wrong result.
 [2015-05-26 12:16 UTC] cmb@php.net
Confirmed: <http://3v4l.org/t2RHn>.

This issue seems to be related to bug #67116, bug #67572, bug #68946, bug #69169 and bug #69491.
 [2015-05-27 17:48 UTC] cmb@php.net
-Status: Assigned +Status: Duplicate -Assigned To: acurioso +Assigned To: cmb
 [2015-05-27 17:48 UTC] cmb@php.net
After further investigation it turned out that this is a duplicate
of bug #66084.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 08:01:32 2024 UTC