Re: [xml] Iterating over a node set



On Thu, Jan 09, 2003 at 04:46:57PM +0100, Holger Rauch wrote:
Hi!

I'm trying to iterate over a node set using

xpath_items = xpath_obj->nodesetval->nodeTab

(xpath_obj is the one returned by xmlXPathCompiledEval(); xpath_items is an
xmlNodePtr). Now when I do

while ( *xpath_items ) {
...
}

  You did not indicate the way you increment or guard for xpath_items

on Linux, the loop terminates correctly (since xpath_items becomes NULL),
but on Solaris 2.8 (64 bit), the loop continues until an attribute is
accessed (the loop then terminates with a segfault). (I need to iterate over
the node set since it contains more than 1 node.) The libxml2 version
I'm using is 2.4.26. Two questions:

1. Is my way of iterating over a node set correct? If not, what am I doing
wrong?

  It's not correct. The number of elements in the set is indicated by
    xpath_obj->nodesetval->nodeNr
(assuming the xpath_obj is of the right type AND that xpath_obj->nodesetval
is not NULL).

struct _xmlNodeSet {
    int nodeNr;                 /* number of nodes in the set */
    int nodeMax;                /* size of the array as allocated */
    xmlNodePtr *nodeTab;        /* array of nodes in no particular order */
};

You're doing illegal memory accesses by accessing after the last element.
You're just lucky it doesn't crash on i386

Daniel

-- 
Daniel Veillard      | Red Hat Network https://rhn.redhat.com/
veillard redhat com  | libxml GNOME XML XSLT toolkit  http://xmlsoft.org/
http://veillard.com/ | Rpmfind RPM search engine http://rpmfind.net/



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]