Re: [xml] Iterating over a node set

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

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

  It's not correct. The number of elements in the set is indicated by
(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 Veillard      | Red Hat Network
veillard redhat com  | libxml GNOME XML XSLT toolkit | Rpmfind RPM search engine

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