various assorted small cleanups based on the Coverity reports Daniel
authorDaniel Veillard <veillard@src.gnome.org>
Fri, 10 Mar 2006 11:51:20 +0000 (11:51 +0000)
committerDaniel Veillard <veillard@src.gnome.org>
Fri, 10 Mar 2006 11:51:20 +0000 (11:51 +0000)
* libexslt/crypto.c libexslt/date.c libexslt/saxon.c
  libxslt/attributes.c libxslt/imports.c libxslt/pattern.c
  libxslt/preproc.c libxslt/transform.c libxslt/variables.c
  libxslt/xslt.c libxslt/xsltutils.c: various assorted small cleanups
  based on the Coverity reports
Daniel

13 files changed:
ChangeLog
NEWS
libexslt/crypto.c
libexslt/date.c
libexslt/saxon.c
libxslt/attributes.c
libxslt/imports.c
libxslt/pattern.c
libxslt/preproc.c
libxslt/transform.c
libxslt/variables.c
libxslt/xslt.c
libxslt/xsltutils.c

index f428863..2b090d6 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Fri Mar 10 12:49:18 CET 2006 Daniel Veillard <daniel@veillard.com>
+
+       * libexslt/crypto.c libexslt/date.c libexslt/saxon.c
+         libxslt/attributes.c libxslt/imports.c libxslt/pattern.c
+         libxslt/preproc.c libxslt/transform.c libxslt/variables.c
+         libxslt/xslt.c libxslt/xsltutils.c: various assorted small cleanups
+         based on the Coverity reports
+
 Wed Feb 22 16:09:10 CET 2006 Daniel Veillard <daniel@veillard.com>
 
        * python/types.c: Nic Ferrier found debug statement left in the
diff --git a/NEWS b/NEWS
index 4fa7c1c..0fd39e7 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -11,51 +11,45 @@ to the CVS at
 http://cvs.gnome.org/viewcvs/libxslt/
  code base.Those are the public releases made:
 1.1.15: Sep 04 2005:
-   - build fixes: Windows build cleanups and updates (Igor Zlatkovic), remove
-      jhbuild warnings
-   - bug fixes: negative number formatting (William Brack), number formatting
-      per mille definition (William Brack), XInclude default values (William),
-      text copy bugs (William), bug related to xmlXPathContext size, reuse
-      libxml2 memory management for text nodes, dictionnary text bug, forbid
-      variables in match (needs libxml2-2.6.21)
-   - improvements: EXSLT dyn:map (Mark Vakoc), 
-  
+   - build fixes: Windows build cleanups and updates (Igor Zlatkovic),
+    remove jhbuild warnings
+   - bug fixes: negative number formatting (William Brack), number
+    formatting per mille definition (William Brack), XInclude default values
+    (William), text copy bugs (William), bug related to xmlXPathContext size,
+    reuse libxml2 memory management for text nodes, dictionnary text bug,
+    forbid variables in match (needs libxml2-2.6.21)
+   - improvements: EXSLT dyn:map (Mark Vakoc),
    - documentation: EXSLT date and time functions namespace in man (Jonathan
-      Wakely)
+    Wakely)
 
 
 1.1.14: Apr 02 2005:
    - bug fixes: text node on stylesheet document without a dictionary
-      (William Brack), more checking of XSLT syntax, calling xsltInit() multiple
-      times, mode values interning raised by Mark Vakoc, bug in pattern
-      matching with ancestors, bug in patterna matching with cascading select,
-      xinclude and document() problem, build outside of source tree (Mike
-      Castle)
+    (William Brack), more checking of XSLT syntax, calling xsltInit()
+    multiple times, mode values interning raised by Mark Vakoc, bug in
+    pattern matching with ancestors, bug in patterna matching with cascading
+    select, xinclude and document() problem, build outside of source tree
+    (Mike Castle)
    - improvement: added a --nodict mode to xsltproc to check problems for
-      docuemtns without dictionnaries
-
+    docuemtns without dictionnaries
 
 
 1.1.13: Mar 13 2005:
    - build fixes: 64bits cleanup (William Brack), python 2.4 test (William),
-      LIBXSLT_VERSION_EXTRA on Windows (William), Windows makefiles fixes
-      (Joel Reed), libgcrypt-devel requires for RPM spec.
-  
+    LIBXSLT_VERSION_EXTRA on Windows (William), Windows makefiles fixes (Joel
+    Reed), libgcrypt-devel requires for RPM spec.
    - bug fixes: exslt day-of-week-in-month (Sal Paradise), xsl:call-template
-      should not change the current template rule (William Brack), evaluation
-      of global variables (William Brack), RVT's in XPath predicates (William),
-      namespace URI on template names (Mark Vakoc), stat() for Windows patch
-      (Aleksey Gurtovoy), pattern expression fixes (William Brack), out of
-      memory detection misses (William), parserOptions propagation (William),
-      exclude-result-prefixes fix (William), // patten fix (William).
-  
-   - extensions: module support (Joel Reed), dictionnary based speedups 
-      trying to get rid of xmlStrEqual as much as possible.
-  
+    should not change the current template rule (William Brack), evaluation
+    of global variables (William Brack), RVT's in XPath predicates (William),
+    namespace URI on template names (Mark Vakoc), stat() for Windows patch
+    (Aleksey Gurtovoy), pattern expression fixes (William Brack), out of
+    memory detection misses (William), parserOptions propagation (William),
+    exclude-result-prefixes fix (William), // patten fix (William).
+   - extensions: module support (Joel Reed), dictionnary based speedups
+    trying to get rid of xmlStrEqual as much as possible.
    - documentation: added Wiki (Joel Reed)
 
 
-
 1.1.12: Oct 29 2004:
    - build fixes: warnings removal (William).
    - bug fixes: attribute document pointer fix (Mark Vakoc), exslt date
index aac601c..2627147 100644 (file)
@@ -621,15 +621,24 @@ exsltCryptoRc4EncryptFunction (xmlXPathParserContextPtr ctxt, int nargs) {
 /* encrypt it */
     bin_len = str_len;
     bin = xmlStrdup (str);
+    if (bin == NULL) {
+       xmlXPathReturnEmptyString (ctxt);
+       goto done;
+    }
     PLATFORM_RC4_ENCRYPT (ctxt, padkey, str, str_len, bin, bin_len);
 
 /* encode it */
     hex_len = str_len * 2 + 1;
     hex = xmlMallocAtomic (hex_len);
+    if (hex == NULL) {
+       xmlXPathReturnEmptyString (ctxt);
+       goto done;
+    }
 
     exsltCryptoBin2Hex (bin, str_len, hex, hex_len);
     xmlXPathReturnString (ctxt, hex);
 
+done:
     if (key != NULL)
        xmlFree (key);
     if (str != NULL)
index 37aa888..59ca136 100644 (file)
@@ -553,12 +553,12 @@ _exsltDateParseTime (exsltDateValDatePtr dt, const xmlChar **str)
 static int
 _exsltDateParseTimeZone (exsltDateValDatePtr dt, const xmlChar **str)
 {
-    const xmlChar *cur = *str;
+    const xmlChar *cur;
     int ret = 0;
 
     if (str == NULL)
        return -1;
-
+    cur = *str;
     switch (*cur) {
     case 0:
        dt->tz_flag = 0;
@@ -1522,6 +1522,14 @@ _exsltDateAdd (exsltDateValPtr dt, exsltDateValPtr dur)
             long tyr  = r->year + (long)FQUOTIENT_RANGE((int)r->mon-1, 1, 13);
             if (tyr == 0)
                 tyr--;
+           /*
+            * Coverity detected an overrun in daysInMonth 
+            * of size 12 at position 12 with index variable "((r)->mon - 1)"
+            */
+           if (tmon < 0)
+               tmon = 0;
+           if (tmon > 12)
+               tmon = 12;
             tempdays += MAX_DAYINMONTH(tyr, tmon);
             carry = -1;
         } else if (tempdays > (long)MAX_DAYINMONTH(r->year, r->mon)) {
index f59bfb4..fcb1547 100644 (file)
@@ -221,6 +221,7 @@ exsltSaxonLineNumberFunction(xmlXPathParserContextPtr ctxt, int nargs) {
        if ((nodelist == NULL) || (nodelist->nodeNr <= 0)) {
            xmlXPathFreeObject(obj);
            valuePush(ctxt, xmlXPathNewFloat(-1));
+           return;
        }
        cur = nodelist->nodeTab[0];
        for (i = 1;i < nodelist->nodeNr;i++) {
index d5a53fe..1265831 100644 (file)
@@ -578,16 +578,15 @@ xsltAttributeInternal(xsltTransformContextPtr ctxt, xmlNodePtr node,
     const xmlChar *URL = NULL;
 
 
-    if (ctxt->insert == NULL)
-        return;
-    if (comp == NULL) {
+    if ((comp == NULL) || (ctxt == NULL)) {
         xsltTransformError(ctxt, NULL, inst,
                          "xsl:attribute : compilation failed\n");
         return;
     }
+    if (ctxt->insert == NULL)
+        return;
 
-    if ((ctxt == NULL) || (node == NULL) || (inst == NULL)
-        || (comp == NULL))
+    if ((node == NULL) || (inst == NULL) || (comp == NULL))
         return;
     if (!comp->has_name) {
         return;
index e1456ba..a2832f0 100644 (file)
@@ -278,7 +278,7 @@ xsltNextImport(xsltStylesheetPtr cur) {
        return(cur->next) ;
     do {
        cur = cur->parent;
-       if (cur == NULL) return(NULL);
+       if (cur == NULL) break;
        if (cur->next != NULL) return(cur->next);
     } while (cur != NULL);
     return(cur);
index c8e4932..6c72499 100644 (file)
@@ -740,8 +740,6 @@ restart:
                    continue;
                }
                while (node != NULL) {
-                   if (node == NULL)
-                       goto rollback;
                    if ((node->type == XML_ELEMENT_NODE) &&
                        (step->value[0] == node->name[0]) &&
                        (xmlStrEqual(step->value, node->name))) {
index e4e69dc..7a5092b 100644 (file)
@@ -217,11 +217,14 @@ static xsltStylePreCompPtr
 xsltNewStylePreComp(xsltStylesheetPtr style, xsltStyleType type) {
     xsltStylePreCompPtr cur;
 
+    if (style == NULL)
+        return(NULL);
+
     cur = (xsltStylePreCompPtr) xmlMalloc(sizeof(xsltStylePreComp));
     if (cur == NULL) {
        xsltTransformError(NULL, style, NULL,
                "xsltNewStylePreComp : malloc failed\n");
-       if (style != NULL) style->errors++;
+       style->errors++;
        return(NULL);
     }
     memset(cur, 0, sizeof(xsltStylePreComp));
@@ -275,7 +278,7 @@ xsltNewStylePreComp(xsltStylesheetPtr style, xsltStyleType type) {
        if (cur->func == NULL) {
            xsltTransformError(NULL, style, NULL,
                    "xsltNewStylePreComp : no function for type %d\n", type);
-           if (style != NULL) style->errors++;
+           style->errors++;
        }
     }
     cur->next = style->preComps;
index b9f7538..caf41bc 100644 (file)
@@ -758,9 +758,10 @@ xsltCopyText(xsltTransformContextPtr ctxt, xmlNodePtr target,
        ctxt->lasttuse = len;
     }
     if (copy != NULL) {
-        copy->doc = target->doc;
-       if (target != NULL)
+       if (target != NULL) {
+           copy->doc = target->doc;
            xmlAddChild(target, copy);
+       }
     } else {
        xsltTransformError(ctxt, NULL, target,
                         "xsltCopyText: text copy failed\n");
@@ -857,7 +858,7 @@ xsltCopyNode(xsltTransformContextPtr ctxt, xmlNodePtr node,
             xmlNodePtr insert) {
     xmlNodePtr copy;
 
-    if (node->type == XML_DTD_NODE)
+    if ((node->type == XML_DTD_NODE) || (insert == NULL))
        return(NULL);
     if ((node->type == XML_TEXT_NODE) ||
        (node->type == XML_CDATA_SECTION_NODE))
@@ -877,8 +878,8 @@ xsltCopyNode(xsltTransformContextPtr ctxt, xmlNodePtr node,
             (node->type == XML_ATTRIBUTE_NODE)) {
            if (node->ns != NULL) {
                copy->ns = xsltGetNamespace(ctxt, node, node->ns, copy);
-           } else if ((insert != NULL) && (insert->type == XML_ELEMENT_NODE) &&
-                    (insert->ns != NULL)) {
+           } else if ((insert->type == XML_ELEMENT_NODE) &&
+                      (insert->ns != NULL)) {
                xmlNsPtr defaultNs;
 
                defaultNs = xmlSearchNs(insert->doc, insert, NULL);
@@ -1520,6 +1521,8 @@ xsltApplyOneTemplateInt(xsltTransformContextPtr ctxt, xmlNodePtr node,
 #endif
     long start = 0;
 
+    if (ctxt == NULL) return;
+
 #ifdef WITH_DEBUGGER
     if (ctxt->debugStatus != XSLT_DEBUG_NONE) {
         if (templ) {
@@ -1550,7 +1553,7 @@ xsltApplyOneTemplateInt(xsltTransformContextPtr ctxt, xmlNodePtr node,
     }
 #endif
 
-    if ((ctxt == NULL) || (list == NULL))
+    if (list == NULL)
         return;
     CHECK_STOPPED;
 
@@ -3497,7 +3500,6 @@ error:
 void
 xsltChoose(xsltTransformContextPtr ctxt, xmlNodePtr node,
           xmlNodePtr inst, xsltStylePreCompPtr comp ATTRIBUTE_UNUSED) {
-    xmlChar *prop = NULL;
     xmlXPathObjectPtr res = NULL;
     xmlNodePtr replacement, when;
     int doit = 1;
@@ -3587,9 +3589,6 @@ xsltChoose(xsltTransformContextPtr ctxt, xmlNodePtr node,
                                 NULL, NULL, 0);
            goto done;
        }
-       if (prop != NULL)
-           xmlFree(prop);
-       prop = NULL;
        if (res != NULL)
            xmlXPathFreeObject(res);
        res = NULL;
@@ -3620,8 +3619,6 @@ xsltChoose(xsltTransformContextPtr ctxt, xmlNodePtr node,
 
 done:
 error:
-    if (prop != NULL)
-       xmlFree(prop);
     if (res != NULL)
        xmlXPathFreeObject(res);
 }
@@ -3714,7 +3711,7 @@ xsltForEach(xsltTransformContextPtr ctxt, xmlNodePtr node,
     xmlNodePtr replacement;
     xmlNodeSetPtr list = NULL, oldList;
     int i, oldProximityPosition, oldContextSize;
-    xmlNodePtr oldNode = ctxt->node;
+    xmlNodePtr oldNode;
     int nbsorts = 0;
     xmlNodePtr sorts[XSLT_MAX_SORT];
     xmlDocPtr oldXDocPtr;
@@ -3729,6 +3726,7 @@ xsltForEach(xsltTransformContextPtr ctxt, xmlNodePtr node,
             "xsl:for-each : compilation failed\n");
        return;
     }
+    oldNode = ctxt->node;
 
 #ifdef WITH_XSLT_DEBUG_PROCESS
     XSLT_TRACE(ctxt,XSLT_TRACE_FOR_EACH,xsltGenericDebug(xsltGenericDebugContext,
@@ -4303,7 +4301,7 @@ xsltApplyStylesheetInternal(xsltStylesheetPtr style, xmlDocPtr doc,
     if ((res != NULL) && (ctxt != NULL) && (output != NULL)) {
        int ret;
 
-       ret = xsltCheckWrite(userCtxt->sec, userCtxt, (const xmlChar *) output);
+       ret = xsltCheckWrite(ctxt->sec, ctxt, (const xmlChar *) output);
        if (ret == 0) {
            xsltTransformError(ctxt, NULL, NULL,
                     "xsltApplyStylesheet: forbidden to save to %s\n",
index c9049af..48e6292 100644 (file)
@@ -477,8 +477,12 @@ xsltEvalVariable(xsltTransformContextPtr ctxt, xsltStackElemPtr elem,
        if ((precomp == NULL) || (precomp->comp == NULL))
            xmlXPathFreeCompExpr(comp);
        if (result == NULL) {
-           xsltTransformError(ctxt, NULL, precomp->inst,
-               "Evaluating variable %s failed\n", elem->name);
+           if (precomp == NULL)
+               xsltTransformError(ctxt, NULL, NULL,
+                   "Evaluating variable %s failed\n", elem->name);
+           else
+               xsltTransformError(ctxt, NULL, precomp->inst,
+                   "Evaluating variable %s failed\n", elem->name);
            ctxt->state = XSLT_STATE_STOPPED;
 #ifdef WITH_XSLT_DEBUG_VARIABLE
 #ifdef LIBXML_DEBUG_ENABLED
@@ -617,8 +621,12 @@ xsltEvalGlobalVariable(xsltStackElemPtr elem, xsltTransformContextPtr ctxt) {
        if ((precomp == NULL) || (precomp->comp == NULL))
            xmlXPathFreeCompExpr(comp);
        if (result == NULL) {
-           xsltTransformError(ctxt, NULL, precomp->inst,
-               "Evaluating global variable %s failed\n", elem->name);
+           if (precomp == NULL)
+               xsltTransformError(ctxt, NULL, NULL,
+                   "Evaluating global variable %s failed\n", elem->name);
+           else
+               xsltTransformError(ctxt, NULL, precomp->inst,
+                   "Evaluating global variable %s failed\n", elem->name);
            ctxt->state = XSLT_STATE_STOPPED;
 #ifdef WITH_XSLT_DEBUG_VARIABLE
 #ifdef LIBXML_DEBUG_ENABLED
@@ -1021,11 +1029,8 @@ xsltProcessUserParamInternal(xsltTransformContextPtr ctxt,
     elem = xsltNewStackElem();
     if (elem != NULL) {
        elem->name = name;
-       if (value != NULL)
-           elem->select = xmlDictLookup(ctxt->dict, value, -1);
-       else
-           elem->select = NULL;
-       if (href)
+       elem->select = xmlDictLookup(ctxt->dict, value, -1);
+       if (href != NULL)
            elem->nameURI = xmlDictLookup(ctxt->dict, href, -1);
        elem->tree = NULL;
        elem->computed = 1;
index 07e4081..6dada2e 100644 (file)
@@ -1198,8 +1198,11 @@ xsltPrecomputeStylesheet(xsltStylesheetPtr style, xmlNodePtr cur) {
     xmlNodePtr delete;
     int internalize = 0;
 
-    if ((style != NULL) && (cur != NULL) && (cur->doc != NULL) &&
-        (style->dict != NULL) && (cur->doc->dict == style->dict))
+    if ((style == NULL) || (cur == NULL))
+        return;
+
+    if ((cur->doc != NULL) && (style->dict != NULL) &&
+        (cur->doc->dict == style->dict))
        internalize = 1;
     else
         style->internalized = 0;
@@ -1393,6 +1396,8 @@ xsltGatherNamespaces(xsltStylesheetPtr style) {
     xmlNodePtr cur;
     const xmlChar *URI;
 
+    if (style == NULL)
+        return;
     /* 
      * TODO: basically if the stylesheet uses the same prefix for different
      *       patterns, well they may be in problem, hopefully they will get
@@ -1409,7 +1414,7 @@ xsltGatherNamespaces(xsltStylesheetPtr style) {
                        if (style->nsHash == NULL) {
                            xsltTransformError(NULL, style, cur,
                 "xsltGatherNamespaces: failed to create hash table\n");
-                           if (style != NULL) style->errors++;
+                           style->errors++;
                            return;
                        }
                    }
@@ -1417,7 +1422,7 @@ xsltGatherNamespaces(xsltStylesheetPtr style) {
                    if ((URI != NULL) && (!xmlStrEqual(URI, ns->href))) {
                        xsltTransformError(NULL, style, cur,
             "Namespaces prefix %s used for multiple namespaces\n",ns->prefix);
-                       if (style != NULL) style->warnings++;
+                       style->warnings++;
                    } else if (URI == NULL) {
                        xmlHashUpdateEntry(style->nsHash, ns->prefix,
                            (void *) ns->href, (xmlHashDeallocator)xmlFree);
@@ -1513,7 +1518,7 @@ xsltParseTemplateContent(xsltStylesheetPtr style, xmlNodePtr templ) {
                                                (const xmlChar *)"no")){
                            xsltTransformError(NULL, style, cur,
             "xsl:text: disable-output-escaping allows only yes or no\n");
-                           if (style != NULL) style->warnings++;
+                           style->warnings++;
 
                        }
                        xmlFree(prop);
@@ -1528,7 +1533,7 @@ xsltParseTemplateContent(xsltStylesheetPtr style, xmlNodePtr templ) {
                             (text->type != XML_CDATA_SECTION_NODE)) {
                            xsltTransformError(NULL, style, cur,
                 "xsltParseTemplateContent: xslt:text content problem\n");
-                           if (style != NULL) style->errors++;
+                           style->errors++;
                            break;
                        }
                        if ((noesc) && (text->type != XML_CDATA_SECTION_NODE))
@@ -1540,14 +1545,6 @@ xsltParseTemplateContent(xsltStylesheetPtr style, xmlNodePtr templ) {
                     * replace xsl:text by the list of childs
                     */
                    if (text == NULL) {
-                       int internalize = 0;
-                       if ((style != NULL) && (text != NULL) &&
-                           (text->doc != NULL) && (style->dict != NULL) &&
-                           (text->doc->dict == style->dict))
-                           internalize = 1;
-                       else
-                           style->internalized = 0;
-
                        text = cur->children;
                        while (text != NULL) {
                            if ((text->content != NULL) &&
index 78f30cd..66b5e1a 100644 (file)
@@ -286,7 +286,8 @@ xsltGetUTF8Char(const unsigned char *utf, int *len) {
     return(c);
 
 error:
-    *len = 0;
+    if (len != NULL)
+       *len = 0;
     return(-1);
 }