[xslt] coverity warning fixes (batch 1)



hi,

I also went over the warnings the coverity tool (former stanford
checker) reports. Attached are patches. These are not all of the yet
(coverity has about 230 complaints regarding libxml2). For some of them
I need to consult you case by case what the intended behaviour is.

One such case for a start below:

nanoftp.c:

Event cannot_single: After this line (or expression), the value of
"file" cannot be 0
1323         if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET)
|| (file == NULL)) return(-1);
Event dead_error_condition: On this path, the condition "file == NULL"
could not be true
Event dead_error_line: Cannot reach dead statement "return 0;"
1324         if (file == NULL) return (0);

Would you like to return -1 or 0 for file==NULL?


Ciao
Stefan
>From 5dbe1f17bbd15365806f374a55069219b22678b1 Mon Sep 17 00:00:00 2001
From: Stefan Kost <ensonic users sf net>
Date: Mon, 9 May 2011 15:43:06 +0300
Subject: [PATCH 07/25] tree: remove dead assignments

---
 tree.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/tree.c b/tree.c
index 24db82a..e333696 100644
--- a/tree.c
+++ b/tree.c
@@ -1615,9 +1615,9 @@ xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) {
 	    node = xmlNewDocTextLen(doc, q, cur - q);
 	    if (node == NULL) return(ret);
 	    if (last == NULL) {
-		last = ret = node;
+		ret = node;
 	    } else {
-		last = xmlAddNextSibling(last, node);
+		xmlAddNextSibling(last, node);
 	    }
 	}
     }
-- 
1.7.1

>From 202b8cc66397594db03a8091b9a3d0f39e3091b2 Mon Sep 17 00:00:00 2001
From: Stefan Kost <ensonic users sf net>
Date: Mon, 9 May 2011 15:47:48 +0300
Subject: [PATCH 08/25] parser: remove NULL check as we deref the pointer before a couple of times

This is a static function which must be not called with oldctx==NULL anyway.
During the execution of the function no codepath is seeting oldctx=NULL.
---
 parser.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)

diff --git a/parser.c b/parser.c
index 9ab8641..1fcf5e0 100644
--- a/parser.c
+++ b/parser.c
@@ -12917,8 +12917,7 @@ xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt,
      * Record in the parent context the number of entities replacement
      * done when parsing that reference.
      */
-    if (oldctxt != NULL)
-        oldctxt->nbentities += ctxt->nbentities;
+    oldctxt->nbentities += ctxt->nbentities;
 
     /*
      * Also record the last error if any
-- 
1.7.1

>From 1a90f353db0148910cfa26a8bd6e13664372d239 Mon Sep 17 00:00:00 2001
From: Stefan Kost <ensonic users sf net>
Date: Mon, 9 May 2011 15:53:20 +0300
Subject: [PATCH 09/25] testdict: remove duplicated functions calls with unused returns

The function xmlDictQLookup(9 is stateless, calling it twice should not affect
the result.
---
 testdict.c |    5 -----
 1 files changed, 0 insertions(+), 5 deletions(-)

diff --git a/testdict.c b/testdict.c
index 4e8581f..89aac82 100644
--- a/testdict.c
+++ b/testdict.c
@@ -152,7 +152,6 @@ static int run_test2(xmlDictPtr parent) {
     int ret = 0;
     xmlChar prefix[40];
     xmlChar *cur, *pref;
-    const xmlChar *tmp;
 
     dict = xmlDictCreateSub(parent);
     if (dict == NULL) {
@@ -276,7 +275,6 @@ static int run_test2(xmlDictPtr parent) {
 	while (*cur != ':') *pref++ = *cur++;
 	cur++;
 	*pref = 0;
-	tmp = xmlDictQLookup(dict, &prefix[0], cur);
 	if (xmlDictQLookup(dict, &prefix[0], cur) != test2[i]) {
 	    fprintf(stderr, "Failed lookup check for '%s':'%s'\n",
 	            &prefix[0], cur);
@@ -293,7 +291,6 @@ static int run_test2(xmlDictPtr parent) {
 	while (*cur != ':') *pref++ = *cur++;
 	cur++;
 	*pref = 0;
-	tmp = xmlDictQLookup(dict, &prefix[0], cur);
 	if (xmlDictQLookup(dict, &prefix[0], cur) != test1[i]) {
 	    fprintf(stderr, "Failed parent lookup check for '%s':'%s'\n",
 	            &prefix[0], cur);
@@ -315,7 +312,6 @@ static int run_test1(void) {
     int ret = 0;
     xmlChar prefix[40];
     xmlChar *cur, *pref;
-    const xmlChar *tmp;
 
     dict = xmlDictCreate();
     if (dict == NULL) {
@@ -407,7 +403,6 @@ static int run_test1(void) {
 	while (*cur != ':') *pref++ = *cur++;
 	cur++;
 	*pref = 0;
-	tmp = xmlDictQLookup(dict, &prefix[0], cur);
 	if (xmlDictQLookup(dict, &prefix[0], cur) != test1[i]) {
 	    fprintf(stderr, "Failed lookup check for '%s':'%s'\n",
 	            &prefix[0], cur);
-- 
1.7.1

>From f3e294c7662cc333832a86dc30defced789e67c7 Mon Sep 17 00:00:00 2001
From: Stefan Kost <ensonic users sf net>
Date: Mon, 9 May 2011 16:06:20 +0300
Subject: [PATCH 10/25] htmlparser: init all fields to zero

We later pass the stacked structure to htmlNodeInfoPush() where it will be
copied. Before we only init the begin_pos and begin_line fields.
---
 HTMLparser.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/HTMLparser.c b/HTMLparser.c
index 4d43b93..2c8270f 100644
--- a/HTMLparser.c
+++ b/HTMLparser.c
@@ -4291,7 +4291,7 @@ static void
 htmlParseElementInternal(htmlParserCtxtPtr ctxt) {
     const xmlChar *name;
     const htmlElemDesc * info;
-    htmlParserNodeInfo node_info;
+    htmlParserNodeInfo node_info = {0,0,0,0};
     int failed;
 
     if ((ctxt == NULL) || (ctxt->input == NULL)) {
-- 
1.7.1

>From 6d90501286a27fb9efc13d1c6fc6411178ace382 Mon Sep 17 00:00:00 2001
From: Stefan Kost <ensonic users sf net>
Date: Mon, 9 May 2011 15:35:52 +0300
Subject: [PATCH 11/25] debugxml: remove non-sense check as arg is a stack variable

The address of an array on the stack is not NULL.
---
 debugXML.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/debugXML.c b/debugXML.c
index c26217a..1d489a8 100644
--- a/debugXML.c
+++ b/debugXML.c
@@ -2937,7 +2937,7 @@ xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
         } else if (!strcmp(command, "save")) {
             xmlShellSave(ctxt, arg, NULL, NULL);
         } else if (!strcmp(command, "write")) {
-	    if ((arg == NULL) || (arg[0] == 0))
+	    if (arg[0] == 0)
 		xmlGenericError(xmlGenericErrorContext,
                         "Write command requires a filename argument\n");
 	    else
-- 
1.7.1

>From 32214792ed93c76743d8d93a7b37d10da8d6debb Mon Sep 17 00:00:00 2001
From: Stefan Kost <ensonic users sf net>
Date: Mon, 9 May 2011 16:14:46 +0300
Subject: [PATCH 12/25] SAX2: move NULL ptr check before dereferencing it

---
 SAX2.c |   11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/SAX2.c b/SAX2.c
index c0482c0..caf9707 100644
--- a/SAX2.c
+++ b/SAX2.c
@@ -962,14 +962,15 @@ xmlSAX2StartDocument(void *ctx)
 #endif
     if (ctxt->html) {
 #ifdef LIBXML_HTML_ENABLED
-	if (ctxt->myDoc == NULL)
+	if (ctxt->myDoc == NULL) {
 	    ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
+	    if (ctxt->myDoc == NULL) {
+		xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
+		return;
+	    }
+	}
 	ctxt->myDoc->properties = XML_DOC_HTML;
 	ctxt->myDoc->parseFlags = ctxt->options;
-	if (ctxt->myDoc == NULL) {
-	    xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
-	    return;
-	}
 #else
         xmlGenericError(xmlGenericErrorContext,
 		"libxml2 built without HTML support\n");
-- 
1.7.1

>From 6bb3b2ea8d1de2d514b0e8cc1ae0bea85bd185e3 Mon Sep 17 00:00:00 2001
From: Stefan Kost <ensonic users sf net>
Date: Mon, 9 May 2011 16:34:08 +0300
Subject: [PATCH 13/25] runxmlconf: add a check for filename==NULL

---
 runxmlconf.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/runxmlconf.c b/runxmlconf.c
index 38b0ce4..810f281 100644
--- a/runxmlconf.c
+++ b/runxmlconf.c
@@ -60,7 +60,7 @@ const char *skipped_tests[] = {
 static int checkTestFile(const char *filename) {
     struct stat buf;
 
-    if (stat(filename, &buf) == -1)
+    if (!filename || (stat(filename, &buf) == -1))
         return(0);
 
 #if defined(_WIN32) && !defined(__CYGWIN__)
-- 
1.7.1

>From 595b89e3a4625dbe552a2f0211e89fe7c91193a6 Mon Sep 17 00:00:00 2001
From: Stefan Kost <ensonic users sf net>
Date: Mon, 9 May 2011 15:26:01 +0300
Subject: [PATCH 13/18] xpath: move the null-ptr check before we deref it

If cur is NULL we set it to a new value. We need to move the != NULL check
before dereferencing it.
---
 xpath.c |    7 ++++---
 1 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/xpath.c b/xpath.c
index b59ac5a..90ff5d3 100644
--- a/xpath.c
+++ b/xpath.c
@@ -8112,12 +8112,13 @@ xmlXPathNextFollowing(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
 
     if (cur == NULL) {
         cur = ctxt->context->node;
+        if (cur == NULL)
+            return(NULL) ; /* ERROR */
         if (cur->type == XML_NAMESPACE_DECL)
             return(NULL);
         if (cur->type == XML_ATTRIBUTE_NODE)
             cur = cur->parent;
     }
-    if (cur == NULL) return(NULL) ; /* ERROR */
     if (cur->next != NULL) return(cur->next) ;
     do {
         cur = cur->parent;
@@ -8172,13 +8173,13 @@ xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt, xmlNodePtr cur)
     if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
     if (cur == NULL) {
         cur = ctxt->context->node;
+        if (cur == NULL)
+            return (NULL);
         if (cur->type == XML_NAMESPACE_DECL)
             return(NULL);
         if (cur->type == XML_ATTRIBUTE_NODE)
             return(cur->parent);
     }
-    if (cur == NULL)
-	return (NULL);
     if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE))
 	cur = cur->prev;
     do {
-- 
1.7.1

>From 05d57c2c4d5a275f5ea687fc441c399193a7d7e5 Mon Sep 17 00:00:00 2001
From: Stefan Kost <ensonic users sf net>
Date: Mon, 9 May 2011 16:38:43 +0300
Subject: [PATCH 14/25] runtest: first check for temp!=NULL and then run the test

The test is dereferencing temp without an extra NULL check.
---
 runtest.c |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/runtest.c b/runtest.c
index fa925c1..c222f15 100644
--- a/runtest.c
+++ b/runtest.c
@@ -2154,15 +2154,15 @@ streamProcessTest(const char *filename, const char *result, const char *err,
     xmlGetWarningsDefaultValue = 0;
     if (t != NULL) {
         fclose(t);
-	ret = compareFiles(temp, result);
         if (temp != NULL) {
+            ret = compareFiles(temp, result);
             unlink(temp);
             free(temp);
+            if (ret) {
+                fprintf(stderr, "Result for %s failed\n", filename);
+                return(-1);
+            }
         }
-	if (ret) {
-	    fprintf(stderr, "Result for %s failed\n", filename);
-	    return(-1);
-	}
     }
     if (err != NULL) {
 	ret = compareFileMem(err, testErrors, testErrorsSize);
-- 
1.7.1

>From 51e76b061e5097d8d40bb88b92ee0ab4789ca3a5 Mon Sep 17 00:00:00 2001
From: Stefan Kost <ensonic users sf net>
Date: Tue, 3 May 2011 17:21:57 +0300
Subject: [PATCH 14/18] xpath: annotate branch prediction

---
 include/libxml/xpathInternals.h |   29 ++++++++++++++------
 xpath.c                         |   56 ++++++++++++++++++++------------------
 2 files changed, 49 insertions(+), 36 deletions(-)

diff --git a/include/libxml/xpathInternals.h b/include/libxml/xpathInternals.h
index dcd5243..f384e1a 100644
--- a/include/libxml/xpathInternals.h
+++ b/include/libxml/xpathInternals.h
@@ -21,6 +21,17 @@
 extern "C" {
 #endif
 
+/*
+ * Ideally have this for whole libxml2
+ */
+#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
+#define XML_LIKELY(expr) (__builtin_expect ((expr), 1))
+#define XML_UNLIKELY(expr) (__builtin_expect ((expr), 0))
+#else
+#define XML_LIKELY(expr) (expr)
+#define XML_UNLIKELY(expr) (expr)
+#endif
+
 /************************************************************************
  *									*
  *			Helpers						*
@@ -237,7 +248,7 @@ XMLPUBFUN void * XMLCALL
  * Macro to return from the function if an XPath error was detected.
  */
 #define CHECK_ERROR							\
-    if (ctxt->error != XPATH_EXPRESSION_OK) return
+    if (XML_UNLIKELY (ctxt->error != XPATH_EXPRESSION_OK)) return
 
 /**
  * CHECK_ERROR0:
@@ -245,7 +256,7 @@ XMLPUBFUN void * XMLCALL
  * Macro to return 0 from the function if an XPath error was detected.
  */
 #define CHECK_ERROR0							\
-    if (ctxt->error != XPATH_EXPRESSION_OK) return(0)
+    if (XML_UNLIKELY (ctxt->error != XPATH_EXPRESSION_OK)) return(0)
 
 /**
  * XP_ERROR:
@@ -273,7 +284,7 @@ XMLPUBFUN void * XMLCALL
  * type.
  */
 #define CHECK_TYPE(typeval)						\
-    if ((ctxt->value == NULL) || (ctxt->value->type != typeval))	\
+    if (XML_UNLIKELY ((ctxt->value == NULL) || (ctxt->value->type != typeval)))	\
         XP_ERROR(XPATH_INVALID_TYPE)
 
 /**
@@ -284,7 +295,7 @@ XMLPUBFUN void * XMLCALL
  * type. Return(0) in case of failure
  */
 #define CHECK_TYPE0(typeval)						\
-    if ((ctxt->value == NULL) || (ctxt->value->type != typeval))	\
+    if (XML_UNLIKELY ((ctxt->value == NULL) || (ctxt->value->type != typeval)))	\
         XP_ERROR0(XPATH_INVALID_TYPE)
 
 /**
@@ -294,8 +305,8 @@ XMLPUBFUN void * XMLCALL
  * Macro to check that the number of args passed to an XPath function matches.
  */
 #define CHECK_ARITY(x)							\
-    if (ctxt == NULL) return;						\
-    if (nargs != (x))							\
+    if (XML_UNLIKELY (ctxt == NULL)) return;						\
+    if (XML_UNLIKELY (nargs != (x)))							\
         XP_ERROR(XPATH_INVALID_ARITY);
 
 /**
@@ -304,7 +315,7 @@ XMLPUBFUN void * XMLCALL
  * Macro to try to cast the value on the top of the XPath stack to a string.
  */
 #define CAST_TO_STRING							\
-    if ((ctxt->value != NULL) && (ctxt->value->type != XPATH_STRING))	\
+    if (XML_LIKELY ((ctxt->value != NULL) && (ctxt->value->type != XPATH_STRING)))	\
         xmlXPathStringFunction(ctxt, 1);
 
 /**
@@ -313,7 +324,7 @@ XMLPUBFUN void * XMLCALL
  * Macro to try to cast the value on the top of the XPath stack to a number.
  */
 #define CAST_TO_NUMBER							\
-    if ((ctxt->value != NULL) && (ctxt->value->type != XPATH_NUMBER))	\
+    if (XML_LIKELY ((ctxt->value != NULL) && (ctxt->value->type != XPATH_NUMBER)))	\
         xmlXPathNumberFunction(ctxt, 1);
 
 /**
@@ -322,7 +333,7 @@ XMLPUBFUN void * XMLCALL
  * Macro to try to cast the value on the top of the XPath stack to a boolean.
  */
 #define CAST_TO_BOOLEAN							\
-    if ((ctxt->value != NULL) && (ctxt->value->type != XPATH_BOOLEAN))	\
+    if (XML_LIKELY ((ctxt->value != NULL) && (ctxt->value->type != XPATH_BOOLEAN)))	\
         xmlXPathBooleanFunction(ctxt, 1);
 
 /*
diff --git a/xpath.c b/xpath.c
index 90ff5d3..c433d53 100644
--- a/xpath.c
+++ b/xpath.c
@@ -2410,7 +2410,7 @@ valuePop(xmlXPathParserContextPtr ctxt)
 {
     xmlXPathObjectPtr ret;
 
-    if ((ctxt == NULL) || (ctxt->valueNr <= 0))
+    if (XML_UNLIKELY((ctxt == NULL) || (ctxt->valueNr <= 0)))
         return (NULL);
     ctxt->valueNr--;
     if (ctxt->valueNr > 0)
@@ -2433,8 +2433,9 @@ valuePop(xmlXPathParserContextPtr ctxt)
 int
 valuePush(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr value)
 {
-    if ((ctxt == NULL) || (value == NULL)) return(-1);
-    if (ctxt->valueNr >= ctxt->valueMax) {
+    if (XML_UNLIKELY((ctxt == NULL) || (value == NULL)))
+        return(-1);
+    if (XML_UNLIKELY(ctxt->valueNr >= ctxt->valueMax)) {
         xmlXPathObjectPtr *tmp;
 
         tmp = (xmlXPathObjectPtr *) xmlRealloc(ctxt->valueTab,
@@ -7524,7 +7525,7 @@ typedef xmlNodeSetPtr (*xmlXPathNodeSetMergeFunction)
  */
 xmlNodePtr
 xmlXPathNextSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
-    if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
+    if (XML_UNLIKELY((ctxt == NULL) || (ctxt->context == NULL))) return(NULL);
     if (cur == NULL)
         return(ctxt->context->node);
     return(NULL);
@@ -7542,10 +7543,11 @@ xmlXPathNextSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
  */
 xmlNodePtr
 xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
-    if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
-    if (cur == NULL) {
-	if (ctxt->context->node == NULL) return(NULL);
-	switch (ctxt->context->node->type) {
+    if (XML_UNLIKELY((ctxt == NULL) || (ctxt->context == NULL))) return(NULL);
+    if (XML_UNLIKELY(cur == NULL)) {
+        cur = ctxt->context->node;
+	if (XML_UNLIKELY(cur == NULL)) return(NULL);
+	switch (cur->type) {
             case XML_ELEMENT_NODE:
             case XML_TEXT_NODE:
             case XML_CDATA_SECTION_NODE:
@@ -7555,7 +7557,7 @@ xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
             case XML_COMMENT_NODE:
             case XML_NOTATION_NODE:
             case XML_DTD_NODE:
-		return(ctxt->context->node->children);
+		return(cur->children);
             case XML_DOCUMENT_NODE:
             case XML_DOCUMENT_TYPE_NODE:
             case XML_DOCUMENT_FRAG_NODE:
@@ -7563,7 +7565,7 @@ xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
 #ifdef LIBXML_DOCB_ENABLED
 	    case XML_DOCB_DOCUMENT_NODE:
 #endif
-		return(((xmlDocPtr) ctxt->context->node)->children);
+		return(((xmlDocPtr) cur)->children);
 	    case XML_ELEMENT_DECL:
 	    case XML_ATTRIBUTE_DECL:
 	    case XML_ENTITY_DECL:
@@ -7575,8 +7577,8 @@ xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
 	}
 	return(NULL);
     }
-    if ((cur->type == XML_DOCUMENT_NODE) ||
-        (cur->type == XML_HTML_DOCUMENT_NODE))
+    if (XML_UNLIKELY((cur->type == XML_DOCUMENT_NODE) ||
+        (cur->type == XML_HTML_DOCUMENT_NODE)))
 	return(NULL);
     return(cur->next);
 }
@@ -7593,8 +7595,8 @@ xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
  */
 static xmlNodePtr
 xmlXPathNextChildElement(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
-    if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
-    if (cur == NULL) {
+    if (XML_UNLIKELY((ctxt == NULL) || (ctxt->context == NULL))) return(NULL);
+    if (XML_UNLIKELY(cur == NULL)) {
 	cur = ctxt->context->node;
 	if (cur == NULL) return(NULL);
 	/*
@@ -7745,7 +7747,7 @@ next_sibling:
  */
 xmlNodePtr
 xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
-    if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
+    if (XML_UNLIKELY((ctxt == NULL) || (ctxt->context == NULL))) return(NULL);
     if (cur == NULL) {
 	if (ctxt->context->node == NULL)
 	    return(NULL);
@@ -7808,7 +7810,7 @@ xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
  */
 xmlNodePtr
 xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
-    if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
+    if (XML_UNLIKELY((ctxt == NULL) || (ctxt->context == NULL))) return(NULL);
     if (cur == NULL) {
 	if (ctxt->context->node == NULL)
 	    return(NULL);
@@ -7833,7 +7835,7 @@ xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
  */
 xmlNodePtr
 xmlXPathNextParent(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
-    if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
+    if (XML_UNLIKELY((ctxt == NULL) || (ctxt->context == NULL))) return(NULL);
     /*
      * the parent of an attribute or namespace node is the element
      * to which the attribute or namespace node is attached
@@ -7906,7 +7908,7 @@ xmlXPathNextParent(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
  */
 xmlNodePtr
 xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
-    if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
+    if (XML_UNLIKELY((ctxt == NULL) || (ctxt->context == NULL))) return(NULL);
     /*
      * the parent of an attribute or namespace node is the element
      * to which the attribute or namespace node is attached
@@ -8030,7 +8032,7 @@ xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
  */
 xmlNodePtr
 xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
-    if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
+    if (XML_UNLIKELY((ctxt == NULL) || (ctxt->context == NULL))) return(NULL);
     if (cur == NULL)
         return(ctxt->context->node);
     return(xmlXPathNextAncestor(ctxt, cur));
@@ -8049,7 +8051,7 @@ xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
  */
 xmlNodePtr
 xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
-    if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
+    if (XML_UNLIKELY((ctxt == NULL) || (ctxt->context == NULL))) return(NULL);
     if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
 	(ctxt->context->node->type == XML_NAMESPACE_DECL))
 	return(NULL);
@@ -8074,7 +8076,7 @@ xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
  */
 xmlNodePtr
 xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
-    if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
+    if (XML_UNLIKELY((ctxt == NULL) || (ctxt->context == NULL))) return(NULL);
     if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
 	(ctxt->context->node->type == XML_NAMESPACE_DECL))
 	return(NULL);
@@ -8105,7 +8107,7 @@ xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
  */
 xmlNodePtr
 xmlXPathNextFollowing(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
-    if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
+    if (XML_UNLIKELY((ctxt == NULL) || (ctxt->context == NULL))) return(NULL);
     if ((cur != NULL) && (cur->type  != XML_ATTRIBUTE_NODE) &&
         (cur->type != XML_NAMESPACE_DECL) && (cur->children != NULL))
         return(cur->children);
@@ -8170,7 +8172,7 @@ xmlXPathIsAncestor(xmlNodePtr ancestor, xmlNodePtr node) {
 xmlNodePtr
 xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt, xmlNodePtr cur)
 {
-    if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
+    if (XML_UNLIKELY((ctxt == NULL) || (ctxt->context == NULL))) return(NULL);
     if (cur == NULL) {
         cur = ctxt->context->node;
         if (cur == NULL)
@@ -8216,7 +8218,7 @@ static xmlNodePtr
 xmlXPathNextPrecedingInternal(xmlXPathParserContextPtr ctxt,
                               xmlNodePtr cur)
 {
-    if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
+    if (XML_UNLIKELY((ctxt == NULL) || (ctxt->context == NULL))) return(NULL);
     if (cur == NULL) {
         cur = ctxt->context->node;
         if (cur == NULL)
@@ -8259,7 +8261,7 @@ xmlXPathNextPrecedingInternal(xmlXPathParserContextPtr ctxt,
  */
 xmlNodePtr
 xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
-    if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
+    if (XML_UNLIKELY((ctxt == NULL) || (ctxt->context == NULL))) return(NULL);
     if (ctxt->context->node->type != XML_ELEMENT_NODE) return(NULL);
     if (ctxt->context->tmpNsList == NULL && cur != (xmlNodePtr) xmlXPathXMLNamespace) {
         if (ctxt->context->tmpNsList != NULL)
@@ -8296,7 +8298,7 @@ xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
  */
 xmlNodePtr
 xmlXPathNextAttribute(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
-    if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
+    if (XML_UNLIKELY((ctxt == NULL) || (ctxt->context == NULL))) return(NULL);
     if (ctxt->context->node == NULL)
 	return(NULL);
     if (ctxt->context->node->type != XML_ELEMENT_NODE)
@@ -9741,7 +9743,7 @@ xmlXPathParseNCName(xmlXPathParserContextPtr ctxt) {
     xmlChar *ret;
     int count = 0;
 
-    if ((ctxt == NULL) || (ctxt->cur == NULL)) return(NULL);
+    if (XML_UNLIKELY((ctxt == NULL) || (ctxt->cur == NULL))) return(NULL);
     /*
      * Accelerator for simple ASCII names
      */
-- 
1.7.1

>From 9127dedad6809d4b90c56eea11b4400382595054 Mon Sep 17 00:00:00 2001
From: Stefan Kost <ensonic users sf net>
Date: Mon, 9 May 2011 16:45:25 +0300
Subject: [PATCH 15/25] htmlparser: move ctxt dereference after ctxt!=NULL check

---
 HTMLparser.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/HTMLparser.c b/HTMLparser.c
index 2c8270f..5723f3e 100644
--- a/HTMLparser.c
+++ b/HTMLparser.c
@@ -3597,13 +3597,13 @@ htmlParseStartTag(htmlParserCtxtPtr ctxt) {
     int i;
     int discardtag = 0;
 
-    if (ctxt->instate == XML_PARSER_EOF)
-        return(-1);
     if ((ctxt == NULL) || (ctxt->input == NULL)) {
 	htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
 		     "htmlParseStartTag: context error\n", NULL, NULL);
 	return -1;
     }
+    if (ctxt->instate == XML_PARSER_EOF)
+        return(-1);
     if (CUR != '<') return -1;
     NEXT;
 
-- 
1.7.1

>From 6399dfd35562447b35569988a3d64f74529a4dd7 Mon Sep 17 00:00:00 2001
From: Stefan Kost <ensonic users sf net>
Date: Tue, 3 May 2011 17:22:40 +0300
Subject: [PATCH 15/18] xpath: split traversal into init and next functions

This avoid us checking cur=NULL in next().
---
 xpath.c |  100 ++++++++++++++++++++++++++++++++++++--------------------------
 1 files changed, 58 insertions(+), 42 deletions(-)

diff --git a/xpath.c b/xpath.c
index c433d53..deaf296 100644
--- a/xpath.c
+++ b/xpath.c
@@ -7487,6 +7487,12 @@ xmlXPathModValues(xmlXPathParserContextPtr ctxt) {
  *									*
  ************************************************************************/
 
+ /*
+ * A function to initialize traversal.
+ */
+typedef xmlNodePtr (*xmlXPathTraversalStartFunction)
+                    (xmlXPathParserContextPtr ctxt);
+
 /*
  * A traversal function enumerates nodes along an axis.
  * Initially it must be called with NULL, and it indicates
@@ -7584,6 +7590,51 @@ xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
 }
 
 /**
+ * xmlXPathFirstChildElement:
+ * @ctxt:  the XPath Parser context
+ *
+ * Init traversal for the "child" direction and nodes of type element.
+ * The child axis contains the children of the context node in document order.
+ *
+ * Returns the first element following that axis
+ */
+static xmlNodePtr
+xmlXPathFirstChildElement(xmlXPathParserContextPtr ctxt) {
+    xmlNodePtr cur = ctxt->context->node;
+
+    if (XML_UNLIKELY(cur == NULL)) return(NULL);
+    /*
+    * Get the first element child.
+    */
+    switch (cur->type) {
+        case XML_ELEMENT_NODE:
+        case XML_DOCUMENT_FRAG_NODE:
+        case XML_ENTITY_REF_NODE: /* URGENT TODO: entify-refs as well? */
+        case XML_ENTITY_NODE:
+            cur = cur->children;
+            if (cur != NULL) {
+                if (cur->type == XML_ELEMENT_NODE)
+                    return(cur);
+                do {
+                    cur = cur->next;
+                } while ((cur != NULL) &&
+                    (cur->type != XML_ELEMENT_NODE));
+                return(cur);
+            }
+            return(NULL);
+        case XML_DOCUMENT_NODE:
+        case XML_HTML_DOCUMENT_NODE:
+#ifdef LIBXML_DOCB_ENABLED
+        case XML_DOCB_DOCUMENT_NODE:
+#endif
+            return(xmlDocGetRootElement((xmlDocPtr) cur));
+        default:
+            return(NULL);
+    }
+    return(NULL);
+}
+
+/**
  * xmlXPathNextChildElement:
  * @ctxt:  the XPath Parser context
  * @cur:  the current node in the traversal
@@ -7594,41 +7645,7 @@ xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
  * Returns the next element following that axis
  */
 static xmlNodePtr
-xmlXPathNextChildElement(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
-    if (XML_UNLIKELY((ctxt == NULL) || (ctxt->context == NULL))) return(NULL);
-    if (XML_UNLIKELY(cur == NULL)) {
-	cur = ctxt->context->node;
-	if (cur == NULL) return(NULL);
-	/*
-	* Get the first element child.
-	*/
-	switch (cur->type) {
-            case XML_ELEMENT_NODE:
-	    case XML_DOCUMENT_FRAG_NODE:
-	    case XML_ENTITY_REF_NODE: /* URGENT TODO: entify-refs as well? */
-            case XML_ENTITY_NODE:
-		cur = cur->children;
-		if (cur != NULL) {
-		    if (cur->type == XML_ELEMENT_NODE)
-			return(cur);
-		    do {
-			cur = cur->next;
-		    } while ((cur != NULL) &&
-			(cur->type != XML_ELEMENT_NODE));
-		    return(cur);
-		}
-		return(NULL);
-            case XML_DOCUMENT_NODE:
-            case XML_HTML_DOCUMENT_NODE:
-#ifdef LIBXML_DOCB_ENABLED
-	    case XML_DOCB_DOCUMENT_NODE:
-#endif
-		return(xmlDocGetRootElement((xmlDocPtr) cur));
-	    default:
-		return(NULL);
-	}
-	return(NULL);
-    }
+xmlXPathNextChildElement(ATTRIBUTE_UNUSED xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
     /*
     * Get the next sibling element node.
     */
@@ -11979,6 +11996,7 @@ xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt,
     int hasPredicateRange, hasAxisRange, pos, size, newSize;
     int breakOnFirstHit;
 
+    xmlXPathTraversalStartFunction next_i = NULL;
     xmlXPathTraversalFunction next = NULL;
     /* compound axis traversal */
     xmlXPathTraversalFunctionExt outerNext = NULL;
@@ -12044,6 +12062,7 @@ xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt,
 		/*
 		* Optimization if an element node type is 'element'.
 		*/
+		next_i = xmlXPathFirstChildElement;
 		next = xmlXPathNextChildElement;
 	    } else
 		next = xmlXPathNextChild;
@@ -12205,13 +12224,9 @@ xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt,
 	* Traverse the axis and test the nodes.
 	*/
 	pos = 0;
-	cur = NULL;
+	cur = next_i ? next_i(ctxt) : next(ctxt, NULL);
 	hasNsNodes = 0;
-        do {
-            cur = next(ctxt, cur);
-            if (cur == NULL)
-                break;
-
+        while (cur) {
 	    /*
 	    * QUESTION TODO: What does the "first" and "last" stuff do?
 	    */
@@ -12390,7 +12405,8 @@ xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt,
                     }
                     break;
 	    } /* switch(test) */
-        } while (cur != NULL);
+            cur = next(ctxt, cur);
+        };
 
 	goto apply_predicates;
 
-- 
1.7.1

>From ac42517e835829ad9314c6d00060dae5de21b39a Mon Sep 17 00:00:00 2001
From: Stefan Kost <ensonic users sf net>
Date: Mon, 9 May 2011 16:52:12 +0300
Subject: [PATCH 16/25] xmlsave: check for doc!=NULL here to before accessing

---
 xmlsave.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/xmlsave.c b/xmlsave.c
index ddf7143..fd28135 100644
--- a/xmlsave.c
+++ b/xmlsave.c
@@ -769,7 +769,7 @@ htmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
 	encoding = htmlGetMetaEncoding(doc);
     if (encoding == NULL)
 	encoding = BAD_CAST "HTML";
-    if ((encoding != NULL) && (oldctxtenc == NULL) &&
+    if ((encoding != NULL) && (doc != NULL) && (oldctxtenc == NULL) &&
 	(buf->encoder == NULL) && (buf->conv == NULL)) {
 	if (xmlSaveSwitchEncoding(ctxt, (const char*) encoding) < 0) {
 	    doc->encoding = oldenc;
-- 
1.7.1



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