From:             theultramage at gmail dot com
Operating system: Windows Vista
PHP version:      6CVS-2009-06-19 (snap)
PHP Bug Type:     SimpleXML related
Bug description:  xpath() returns FALSE for legitimate query

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

Reply via email to