summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathan Straz <nstraz@redhat.com>2009-06-23 14:26:32 -0400
committerNathan Straz <nstraz@redhat.com>2009-06-23 14:26:32 -0400
commit84ab2045844cb0c22a839b5a7605217c5398ab2e (patch)
treee333cd5a0b4a2d93b0855f215a69a10ff10a3d7a
parent58704a5ec28143401690fa94f7f9d3d495df409f (diff)
downloadgxpp-84ab2045844cb0c22a839b5a7605217c5398ab2e.tar.gz
gxpp-84ab2045844cb0c22a839b5a7605217c5398ab2e.tar.xz
gxpp-84ab2045844cb0c22a839b5a7605217c5398ab2e.zip
Fix a segfault if no nodes were found
The check for nodeset or null nodeset would short circuit causing us to deference the null nodeset. Return early instead so we can process the rest of the files. Include the filename in the output.
-rw-r--r--gxpd.c16
1 files changed, 8 insertions, 8 deletions
diff --git a/gxpd.c b/gxpd.c
index 70cd2bf..d6a2601 100644
--- a/gxpd.c
+++ b/gxpd.c
@@ -100,15 +100,15 @@ gxpd(char *fn, const char *xpath_query, const char *prefix)
xpath_query, fn);
exit(1);
}
- if (obj->type != XPATH_NODESET || obj->nodesetval == NULL) {
- fprintf(stderr, "XPath expression '%s' did not match a nodeset.\n", xpath_query);
- exit(1);
+ /* return early if there were no nodes to remove */
+ if (obj->type != XPATH_NODESET) return 1;
+ if (obj->nodesetval == NULL) return 1;
+
+ for (i = 0; i < obj->nodesetval->nodeNr; i++) {
+ xmlUnlinkNode(obj->nodesetval->nodeTab[i]);
+ xmlFreeNode(obj->nodesetval->nodeTab[i]);
}
- for (i = 0; i < obj->nodesetval->nodeNr; i++) {
- xmlUnlinkNode(obj->nodesetval->nodeTab[i]);
- xmlFreeNode(obj->nodesetval->nodeTab[i]);
- }
- printf("Removed %d nodes\n", i);
+ printf("%d nodes removed from %s\n", i, fn);
xmlXPathFreeObject(obj);
xmlSaveFile(fn, doc);