Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <string.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
void
main()
{
xmlDocPtr doc;
xmlNodePtr cur;
xmlXPathContextPtr ctx;
xmlXPathObjectPtr xpath;
xmlNodeSetPtr set;
xmlChar *xml = "<foo></foo>";
xmlChar *query = "//namespace::*";
int i;
xmlInitParser();
xmlXPathInit();
doc = xmlReadMemory(xml, strlen(xml), NULL, NULL, XML_PARSE_RECOVER | XML_PARSE_NOERROR);
ctx = xmlXPathNewContext(doc);
ctx->node = doc->children;
xpath = xmlXPathEvalExpression(query, ctx);
set = xpath->nodesetval;
/* libxml expects the following to happen:
* xmlXPathFreeNodeSet(set);
* xmlFreeDoc(doc);
*/
/* We cannot guarantee the order of the above and the following can
* cause a segfault:
* xmlFreeDoc(doc);
* xmlXPathFreeNodeSet(set);
*/
/* Nokogiri currently does the following, but valgrind complains:
*/
xmlFreeDoc(doc);
for (i = 0; i < set->nodeNr; i++) {
cur = set->nodeTab[i];
if (cur && cur->type == XML_NAMESPACE_DECL)
xmlXPathNodeSetFreeNs(cur);
}
xmlXPathFreeNodeSetList(xpath);
xmlXPathFreeContext(ctx);
xmlFree(set->nodeTab);
xmlFree(set);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.