Adding various checks on node type though the API
Specifially checking against namespace nodes before accessing node
pointers
diff --git a/tree.c b/tree.c
index 8169d6d..e48fd8a 100644
--- a/tree.c
+++ b/tree.c
@@ -2779,7 +2779,7 @@
xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
xmlAttrPtr prop;
- if (tree == NULL)
+ if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
return;
if (tree->doc != doc) {
if(tree->type == XML_ELEMENT_NODE) {
@@ -2807,7 +2807,7 @@
xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
xmlNodePtr cur;
- if (list == NULL)
+ if ((list == NULL) || (list->type == XML_NAMESPACE_DECL))
return;
cur = list;
while (cur != NULL) {
@@ -2914,7 +2914,9 @@
xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) {
xmlAttrPtr attr;
- if (cur->type != XML_ATTRIBUTE_NODE)
+ if ((cur == NULL) || (cur->type != XML_ATTRIBUTE_NODE) ||
+ (prop == NULL) || (prop->type != XML_ATTRIBUTE_NODE) ||
+ ((prev != NULL) && (prev->type != XML_ATTRIBUTE_NODE)))
return(NULL);
/* check if an attribute with the same name exists */
@@ -2962,14 +2964,14 @@
*/
xmlNodePtr
xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
- if (cur == NULL) {
+ if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
#ifdef DEBUG_TREE
xmlGenericError(xmlGenericErrorContext,
"xmlAddNextSibling : cur == NULL\n");
#endif
return(NULL);
}
- if (elem == NULL) {
+ if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
#ifdef DEBUG_TREE
xmlGenericError(xmlGenericErrorContext,
"xmlAddNextSibling : elem == NULL\n");
@@ -3040,14 +3042,14 @@
*/
xmlNodePtr
xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
- if (cur == NULL) {
+ if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
#ifdef DEBUG_TREE
xmlGenericError(xmlGenericErrorContext,
"xmlAddPrevSibling : cur == NULL\n");
#endif
return(NULL);
}
- if (elem == NULL) {
+ if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
#ifdef DEBUG_TREE
xmlGenericError(xmlGenericErrorContext,
"xmlAddPrevSibling : elem == NULL\n");
@@ -3118,7 +3120,7 @@
xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
xmlNodePtr parent;
- if (cur == NULL) {
+ if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
#ifdef DEBUG_TREE
xmlGenericError(xmlGenericErrorContext,
"xmlAddSibling : cur == NULL\n");
@@ -3126,7 +3128,7 @@
return(NULL);
}
- if (elem == NULL) {
+ if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
#ifdef DEBUG_TREE
xmlGenericError(xmlGenericErrorContext,
"xmlAddSibling : elem == NULL\n");
@@ -3194,7 +3196,7 @@
xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
xmlNodePtr prev;
- if (parent == NULL) {
+ if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
#ifdef DEBUG_TREE
xmlGenericError(xmlGenericErrorContext,
"xmlAddChildList : parent == NULL\n");
@@ -3202,7 +3204,7 @@
return(NULL);
}
- if (cur == NULL) {
+ if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
#ifdef DEBUG_TREE
xmlGenericError(xmlGenericErrorContext,
"xmlAddChildList : child == NULL\n");
@@ -3280,7 +3282,7 @@
xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
xmlNodePtr prev;
- if (parent == NULL) {
+ if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
#ifdef DEBUG_TREE
xmlGenericError(xmlGenericErrorContext,
"xmlAddChild : parent == NULL\n");
@@ -3288,7 +3290,7 @@
return(NULL);
}
- if (cur == NULL) {
+ if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
#ifdef DEBUG_TREE
xmlGenericError(xmlGenericErrorContext,
"xmlAddChild : child == NULL\n");
@@ -3402,7 +3404,7 @@
*/
xmlNodePtr
xmlGetLastChild(xmlNodePtr parent) {
- if (parent == NULL) {
+ if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
#ifdef DEBUG_TREE
xmlGenericError(xmlGenericErrorContext,
"xmlGetLastChild : parent == NULL\n");
@@ -3836,14 +3838,15 @@
xmlNodePtr
xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
if (old == cur) return(NULL);
- if ((old == NULL) || (old->parent == NULL)) {
+ if ((old == NULL) || (old->type == XML_NAMESPACE_DECL) ||
+ (old->parent == NULL)) {
#ifdef DEBUG_TREE
xmlGenericError(xmlGenericErrorContext,
"xmlReplaceNode : old == NULL or without parent\n");
#endif
return(NULL);
}
- if (cur == NULL) {
+ if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
xmlUnlinkNode(old);
return(old);
}
@@ -3957,6 +3960,8 @@
xmlAttrPtr ret;
if (cur == NULL) return(NULL);
+ if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
+ return(NULL);
if (target != NULL)
ret = xmlNewDocProp(target->doc, cur->name, NULL);
else if (doc != NULL)
@@ -4076,6 +4081,8 @@
xmlAttrPtr ret = NULL;
xmlAttrPtr p = NULL,q;
+ if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
+ return(NULL);
while (cur != NULL) {
q = xmlCopyProp(target, cur);
if (q == NULL)
@@ -4576,7 +4583,7 @@
char nametemp[100];
int occur = 0, generic;
- if (node == NULL)
+ if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
return (NULL);
buf_len = 500;
@@ -4841,7 +4848,7 @@
xmlNodePtr old = NULL;
if (doc == NULL) return(NULL);
- if (root == NULL)
+ if ((root == NULL) || (root->type == XML_NAMESPACE_DECL))
return(NULL);
xmlUnlinkNode(root);
xmlSetTreeDoc(root, doc);
@@ -4928,6 +4935,8 @@
xmlNodeGetLang(xmlNodePtr cur) {
xmlChar *lang;
+ if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
+ return(NULL);
while (cur != NULL) {
lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
if (lang != NULL)
@@ -5007,6 +5016,8 @@
xmlNodeGetSpacePreserve(xmlNodePtr cur) {
xmlChar *space;
+ if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
+ return(-1);
while (cur != NULL) {
space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
if (space != NULL) {
@@ -5173,6 +5184,8 @@
if ((cur == NULL) && (doc == NULL))
return(NULL);
+ if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
+ return(NULL);
if (doc == NULL) doc = cur->doc;
if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
cur = doc->children;
@@ -5807,6 +5820,9 @@
int maxns = 10;
int i;
+ if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
+ return(NULL);
+
while (node != NULL) {
if (node->type == XML_ELEMENT_NODE) {
cur = node->nsDef;
@@ -5905,7 +5921,7 @@
xmlNsPtr cur;
xmlNodePtr orig = node;
- if (node == NULL) return(NULL);
+ if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) return(NULL);
if ((nameSpace != NULL) &&
(xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
@@ -6035,7 +6051,7 @@
xmlNodePtr orig = node;
int is_attr;
- if ((node == NULL) || (href == NULL))
+ if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || (href == NULL))
return (NULL);
if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
/*
@@ -6126,7 +6142,7 @@
xmlChar prefix[50];
int counter = 1;
- if (tree == NULL) {
+ if ((tree == NULL) || (tree->type != XML_ELEMENT_NODE)) {
#ifdef DEBUG_TREE
xmlGenericError(xmlGenericErrorContext,
"xmlNewReconciliedNs : tree == NULL\n");
@@ -7983,6 +7999,8 @@
if ((map == NULL) || (*map != NULL))
return (-1);
+ if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
+ return (-1);
/*
* Get in-scope ns-decls of @parent.
*/
@@ -8250,6 +8268,8 @@
if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
return (-1);
+ if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
+ return(-1);
*retNs = NULL;
if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
@@ -8348,8 +8368,8 @@
xmlNodePtr cur;
xmlNsPtr ns;
- if ((doc == NULL) || (node == NULL))
- return (-1);
+ if ((doc == NULL) || (node == NULL) || (node->type == XML_NAMESPACE_DECL))
+ return(-1);
if (retNs)
*retNs = NULL;
@@ -8417,6 +8437,9 @@
char buf[50];
const xmlChar *pref;
int counter = 0;
+
+ if ((doc == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
+ return(NULL);
/*
* Create a ns-decl on @anchor.
*/
@@ -8933,6 +8956,9 @@
parnsdone = 0;
cur = node;
+ if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
+ goto internal_error;
+
while (cur != NULL) {
/*
* Paranoid source-doc sanity check.
@@ -9312,6 +9338,9 @@
*resNode = NULL;
cur = node;
+ if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
+ return(-1);
+
while (cur != NULL) {
if (cur->doc != sourceDoc) {
/*
@@ -9829,6 +9858,8 @@
if (attr->children == NULL)
return (0);
cur = attr->children;
+ if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
+ goto internal_error;
while (cur != NULL) {
cur->doc = destDoc;
switch (cur->type) {
@@ -9913,7 +9944,8 @@
xmlNodePtr destParent,
int options)
{
- if ((node == NULL) || (destDoc == NULL) ||
+ if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
+ (destDoc == NULL) ||
((destParent != NULL) && (destParent->doc != destDoc)))
return(-1);
/*
diff --git a/valid.c b/valid.c
index 5de491d..6d87a85 100644
--- a/valid.c
+++ b/valid.c
@@ -5236,7 +5236,7 @@
xmlElementContentPtr cont;
const xmlChar *name;
- if (elemDecl == NULL)
+ if ((elemDecl == NULL) || (parent == NULL))
return(-1);
cont = elemDecl->content;
name = elemDecl->name;
@@ -5517,7 +5517,8 @@
int ret = 1;
xmlNodePtr cur, child;
- if ((ctxt == NULL) || (doc == NULL) || (elem == NULL))
+ if ((ctxt == NULL) || (doc == NULL) || (elem == NULL) ||
+ (elem->type != XML_ELEMENT_NODE))
return(0);
child = elem->children;
@@ -6379,7 +6380,8 @@
* they don't really mean anything validation wise.
*/
if ((elem->type == XML_XINCLUDE_START) ||
- (elem->type == XML_XINCLUDE_END))
+ (elem->type == XML_XINCLUDE_END) ||
+ (elem->type == XML_NAMESPACE_DECL))
return(1);
CHECK_DTD;
diff --git a/xinclude.c b/xinclude.c
index 0463959..a42793e 100644
--- a/xinclude.c
+++ b/xinclude.c
@@ -875,8 +875,8 @@
static xmlNodePtr
xmlXIncludeGetNthChild(xmlNodePtr cur, int no) {
int i;
- if (cur == NULL)
- return(cur);
+ if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
+ return(NULL);
cur = cur->children;
for (i = 0;i <= no;cur = cur->next) {
if (cur == NULL)
@@ -923,11 +923,13 @@
return(NULL);
start = (xmlNodePtr) range->user;
- if (start == NULL)
+ if ((start == NULL) || (start->type == XML_NAMESPACE_DECL))
return(NULL);
end = range->user2;
if (end == NULL)
return(xmlDocCopyNode(start, target, 1));
+ if (end->type == XML_NAMESPACE_DECL)
+ return(NULL);
cur = start;
index1 = range->index;
@@ -1948,8 +1950,9 @@
xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) {
xmlXIncludeCtxtPtr newctxt;
int ret = 0;
-
- if ((fallback == NULL) || (ctxt == NULL))
+
+ if ((fallback == NULL) || (fallback->type == XML_NAMESPACE_DECL) ||
+ (ctxt == NULL))
return(-1);
if (fallback->children != NULL) {
/*
@@ -2175,7 +2178,7 @@
if ((nr < 0) || (nr >= ctxt->incNr))
return(-1);
cur = ctxt->incTab[nr]->ref;
- if (cur == NULL)
+ if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
return(-1);
/*
@@ -2350,7 +2353,7 @@
int ret = 0;
int i, start;
- if ((doc == NULL) || (tree == NULL))
+ if ((doc == NULL) || (tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
return(-1);
if (ctxt == NULL)
return(-1);
@@ -2464,7 +2467,8 @@
xmlXIncludeCtxtPtr ctxt;
int ret = 0;
- if ((tree == NULL) || (tree->doc == NULL))
+ if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL) ||
+ (tree->doc == NULL))
return(-1);
ctxt = xmlXIncludeNewContext(tree->doc);
@@ -2549,7 +2553,8 @@
xmlXIncludeCtxtPtr ctxt;
int ret = 0;
- if ((tree == NULL) || (tree->doc == NULL))
+ if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL) ||
+ (tree->doc == NULL))
return(-1);
ctxt = xmlXIncludeNewContext(tree->doc);
if (ctxt == NULL)
@@ -2593,7 +2598,8 @@
xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
int ret = 0;
- if ((node == NULL) || (node->doc == NULL) || (ctxt == NULL))
+ if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
+ (node->doc == NULL) || (ctxt == NULL))
return(-1);
ret = xmlXIncludeDoProcess(ctxt, node->doc, node);
if ((ret >= 0) && (ctxt->nbErrors > 0))
diff --git a/xmlreader.c b/xmlreader.c
index 767bd05..f9c7fa1 100644
--- a/xmlreader.c
+++ b/xmlreader.c
@@ -1228,6 +1228,9 @@
xmlBufferPtr buffer;
xmlChar *ret;
+ if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
+ return(NULL);
+
buffer = xmlBufferCreate();
if (buffer == NULL)
return NULL;
diff --git a/xmlsave.c b/xmlsave.c
index f8d4052..76027f6 100644
--- a/xmlsave.c
+++ b/xmlsave.c
@@ -1465,6 +1465,10 @@
return;
if (cur->type == XML_XINCLUDE_END)
return;
+ if (cur->type == XML_NAMESPACE_DECL) {
+ xmlNsDumpOutputCtxt(ctxt, (xmlNsPtr) cur);
+ return;
+ }
if (cur->type == XML_DTD_NODE) {
xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
return;
diff --git a/xpath.c b/xpath.c
index ec02a16..6161671 100644
--- a/xpath.c
+++ b/xpath.c
@@ -7798,6 +7798,8 @@
return(ctxt->context->node->children);
}
+ if (cur->type == XML_NAMESPACE_DECL)
+ return(NULL);
if (cur->children != NULL) {
/*
* Do not descend on entities declarations
@@ -8180,6 +8182,10 @@
static int
xmlXPathIsAncestor(xmlNodePtr ancestor, xmlNodePtr node) {
if ((ancestor == NULL) || (node == NULL)) return(0);
+ if (node->type == XML_NAMESPACE_DECL)
+ return(0);
+ if (ancestor->type == XML_NAMESPACE_DECL)
+ return(0);
/* nodes need to be in the same document */
if (ancestor->doc != node->doc) return(0);
/* avoid searching if ancestor or node is the root node */
@@ -8217,7 +8223,7 @@
if (cur->type == XML_ATTRIBUTE_NODE)
return(cur->parent);
}
- if (cur == NULL)
+ if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
return (NULL);
if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE))
cur = cur->prev;
@@ -8264,6 +8270,8 @@
return (NULL);
ctxt->ancestor = cur->parent;
}
+ if (cur->type == XML_NAMESPACE_DECL)
+ return(NULL);
if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE))
cur = cur->prev;
while (cur->prev == NULL) {
@@ -8471,7 +8479,7 @@
int i = 0;
tmp = cur->nodesetval->nodeTab[0];
- if (tmp != NULL) {
+ if ((tmp != NULL) && (tmp->type != XML_NAMESPACE_DECL)) {
tmp = tmp->children;
while (tmp != NULL) {
tmp = tmp->next;
@@ -14308,6 +14316,7 @@
}
scan_children:
+ if (cur->type == XML_NAMESPACE_DECL) break;
if ((cur->children != NULL) && (depth < max_depth)) {
/*
* Do not descend on entities declarations
diff --git a/xpointer.c b/xpointer.c
index 0b463dd..ce771bd 100644
--- a/xpointer.c
+++ b/xpointer.c
@@ -134,7 +134,7 @@
static int
xmlXPtrGetArity(xmlNodePtr cur) {
int i;
- if (cur == NULL)
+ if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
return(-1);
cur = cur->children;
for (i = 0;cur != NULL;cur = cur->next) {
@@ -157,7 +157,7 @@
static int
xmlXPtrGetIndex(xmlNodePtr cur) {
int i;
- if (cur == NULL)
+ if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
return(-1);
for (i = 1;cur != NULL;cur = cur->prev) {
if ((cur->type == XML_ELEMENT_NODE) ||
@@ -179,7 +179,7 @@
static xmlNodePtr
xmlXPtrGetNthChild(xmlNodePtr cur, int no) {
int i;
- if (cur == NULL)
+ if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
return(cur);
cur = cur->children;
for (i = 0;i <= no;cur = cur->next) {
@@ -1444,11 +1444,13 @@
return(NULL);
start = (xmlNodePtr) range->user;
- if (start == NULL)
+ if ((start == NULL) || (start->type == XML_NAMESPACE_DECL))
return(NULL);
end = range->user2;
if (end == NULL)
return(xmlCopyNode(start, 1));
+ if (end->type == XML_NAMESPACE_DECL)
+ return(NULL);
cur = start;
index1 = range->index;
@@ -2304,7 +2306,7 @@
xmlNodePtr
xmlXPtrAdvanceNode(xmlNodePtr cur, int *level) {
next:
- if (cur == NULL)
+ if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
return(NULL);
if (cur->children != NULL) {
cur = cur->children ;
@@ -2362,7 +2364,7 @@
if ((node == NULL) || (indx == NULL))
return(-1);
cur = *node;
- if (cur == NULL)
+ if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
return(-1);
pos = *indx;
@@ -2453,9 +2455,10 @@
if (string == NULL)
return(-1);
- if (start == NULL)
+ if ((start == NULL) || (start->type == XML_NAMESPACE_DECL))
return(-1);
- if ((end == NULL) || (endindex == NULL))
+ if ((end == NULL) || (*end == NULL) ||
+ ((*end)->type == XML_NAMESPACE_DECL) || (endindex == NULL))
return(-1);
cur = start;
if (cur == NULL)
@@ -2538,13 +2541,12 @@
if (string == NULL)
return(-1);
- if ((start == NULL) || (startindex == NULL))
+ if ((start == NULL) || (*start == NULL) ||
+ ((*start)->type == XML_NAMESPACE_DECL) || (startindex == NULL))
return(-1);
if ((end == NULL) || (endindex == NULL))
return(-1);
cur = *start;
- if (cur == NULL)
- return(-1);
pos = *startindex - 1;
first = string[0];
@@ -2618,14 +2620,12 @@
xmlNodePtr cur;
int pos, len = 0;
- if ((node == NULL) || (indx == NULL))
+ if ((node == NULL) || (*node == NULL) ||
+ ((*node)->type == XML_NAMESPACE_DECL) || (indx == NULL))
return(-1);
cur = *node;
pos = *indx;
- if (cur == NULL)
- return(-1);
-
if ((cur->type == XML_ELEMENT_NODE) ||
(cur->type == XML_DOCUMENT_NODE) ||
(cur->type == XML_HTML_DOCUMENT_NODE)) {