Edit report at https://bugs.php.net/bug.php?id=48601&edit=1

 ID:                 48601
 Updated by:         [email protected]
 Reported by:        theultramage at gmail dot com
 Summary:            xpath() returns FALSE for legitimate query
-Status:             Closed
+Status:             Re-Opened
 Type:               Bug
 Package:            SimpleXML related
 Operating System:   Windows Vista
-PHP Version:        6CVS-2009-06-19 (snap)
+PHP Version:        5.4SVN
 Assigned To:        rrichards
 Block user comment: N
 Private report:     N

 New Comment:

Looks like it still happens for me - the unit test fails and the function 
returns 
false.


Previous Comments:
------------------------------------------------------------------------
[2010-05-05 13:42:16] [email protected]

This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.



------------------------------------------------------------------------
[2010-05-05 13:40:13] [email protected]

Automatic comment from SVN on behalf of rrichards
Revision: http://svn.php.net/viewvc/?view=revision&revision=299016
Log: fix bug #48601 (xpath() returns FALSE for legitimate query)
add test

------------------------------------------------------------------------
[2009-06-24 16:13:21] theultramage at gmail dot com

>From the mail of the main libxml developer 
>(http://mail.gnome.org/archives/xml/2009-June/msg00040.html):

> A NULL object or an empty node set should be treated in the same way.
Also
>There is no iterator API.

So for one part, the current SimpleXML code has a bug because it does not 
handle the NULL value correctly (should return empty array, not FALSE). And 
second, there's currently no other solution.

------------------------------------------------------------------------
[2009-06-19 10:57:16] theultramage at gmail dot com

Description:
------------
SimpleXML's xpath() implementation contains code that returns FALSE for some 
queries where it should be returning an empty container object.

In revision 1.79 of simplexml.c 
(http://cvs.php.net/viewvc.cgi/php-src/ext/simplexml/simplexml.c?r1=1.78&r2=1.79)
 the call to xmlXPathEval() was hotfixed to avoid crashing if the query 
errored. However, the author also added a check against "retval->nodesetval", 
treating NULL as an error situation. I believe this was an incorrect decision.

There is a scenario where 'nodesetval' will be NULL - when querying for the 
attribute of a non-matching element ('/foo/@attr' when there are no 'foo' 
nodes); in all other 'bad' queries that I tried, 'nodesetval' came initialized 
and containing zero nodes. So, we can see that for certain queries, simplexml 
will give an incorrect result. This was already pointed out on 18-dec-2005 by 
an user comment on the php simplexml::xpath documentation page.

I have sent an inquiry to the xml mailing list 
(http://mail.gnome.org/archives/xml/2009-June/msg00019.html) asking for 
clarification, however I believe that the current logic is incorrect. I checked 
php's DOM module src and libxml's xpath1.c code example, and they do not 
consider it a problem (DOM initializes empty DOMNodeList, example sets count to 
0).

Alternatively, the code should not be messing with what is apparently internal 
structures, and instead use the API for processing, if one is available.

Reproduce code:
---------------
<?php
        $xml = '<?xml version="1.0" encoding="utf-8"?><dummy/>';
        $db = new SimpleXMLElement($xml);
        if( FALSE !== $db->xpath("/foo/@attr") )
                echo "OK";
        else
                echo "Error!";
?>

Expected result:
----------------
xpath() should return an empty array.

Actual result:
--------------
xpath() returns FALSE.


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



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

Reply via email to