Re: [xml] libxml2 API crazy?



this gives out an error:

#include <libxml/parser.h>
#include <libxml/tree.h>
#include <stdio.h>

int
main (int argc, char** argv)
{
  xmlDocPtr doc = xmlReadMemory("<doc/>", 6, "egg.xml", NULL, 0);

                  /* XmlNode */            /* XmlDoc */
  printf("%s\n", (doc->children->parent == doc) ? "true" : "false");
  return 0;
}

and this crashes on my machine (as xmlDoc has different fields than
xmlName):

#include <libxml/parser.h>
#include <libxml/tree.h>
#include <stdio.h>

int
main (int argc, char** argv)
{
  xmlDocPtr doc = xmlReadMemory("<doc/>", 6, "egg.xml", NULL, 0);
         
  if (doc->children->parent->ns && doc->children->parent->ns->href)
    printf("%s\n", doc->children->parent->ns->href);

  return 0;
}

On Sat, 2013-05-04 at 10:08 +1000, Callum Gibson wrote:
On 03May13 22:44, Nikita Churaev wrote:
}> What do you want to achieve ? I think I understand but it is better if
}> stated.
}
}Make so you can use libxml2 and be sure that you use it correctly rather
}than just blindly guess.
}
}The main problem here is casting quirks, libxml2 tree seems to cast
}xmlDoc*, xmlAttr* and even xmlNs* into xmlNode*, so not only the fields
}after the common node parts are unsafe to access without checking, even
}ones in the common part are, except xmlElementType type. The
}documentation provides no answer whatsoever on what structures
}correspond to each xmlElementType. Casting xmlNs* to xmlNode* is the
}worst one.

You seem to be talking about the internal implementation, rather than
use of the API? Can you provide specific examples in the code of how one
might be using the API in a valid fashion (ie. as documented and per the
examples) and yet be potentially using the wrong struct type?

If you are worried about "polymorphic" aspects of using certain structures
this is perfectly standard behaviour and, as I mentioned earlier, has been
well-known since K&R 2nd edition. It's also used extensively in the X11 API
and associated widget toolkits.

}I want to add:
}
}safe casting functions, for example:
}
}xmlNodePtr xmlDocToNode(xmlDocPtr node); 
}
}xmlDocPtr xmlNodeToDoc(xmlNodePtr node); // returns NULL if 
}                                         // node is not a doc

Why would an application being using docs as nodes and vice versa? Can you
give examples where this happens?

regards,
    Callum





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