Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Cleanup and fixes based on Coverity pass
[palacios.git] / palacios / src / palacios / vmm_xml.c
index d09a176..a9c6659 100644 (file)
@@ -54,7 +54,6 @@ struct v3_xml_root {       // additional data for the root tag
     char *tmp_start;              // start of work area
     char *tmp_end;              // end of work area
     short standalone;     // non-zero if <?xml standalone="yes"?>
-    char err[V3_XML_ERRL]; // error string
 };
 
 static char * empty_attrib_list[] = { NULL }; // empty, null terminated array of strings
@@ -67,6 +66,7 @@ static void * tmp_realloc(void * old_ptr, size_t old_size, size_t new_size) {
     new_buf = V3_Malloc(new_size);
     
     if (new_buf == NULL) {
+       PrintError(VM_NONE, VCORE_NONE, "Cannot allocate in tmp_realloc in xml\n");
         return NULL;
     }
 
@@ -79,11 +79,9 @@ static void * tmp_realloc(void * old_ptr, size_t old_size, size_t new_size) {
 }
 
 // set an error string and return root
-static void v3_xml_err(struct v3_xml_root * root, char * xml_str, const char * err, ...) {
-    va_list ap;
+static void v3_xml_err(struct v3_xml_root * root, char * xml_str, const char * err, const char *arg) {
     int line = 1;
     char * tmp;
-    char fmt[V3_XML_ERRL];
     
     for (tmp = root->tmp_start; tmp < xml_str; tmp++) {
        if (*tmp == '\n') {
@@ -91,14 +89,8 @@ static void v3_xml_err(struct v3_xml_root * root, char * xml_str, const char * e
        }
     }
 
-    snprintf(fmt, V3_XML_ERRL, "[error near line %d]: %s", line, err);
-
-    va_start(ap, err);
-    vsnprintf(root->err, V3_XML_ERRL, fmt, ap);
-    va_end(ap);
-
-    PrintError("XML Error: %s\n", root->err);
-
+    PrintError(VM_NONE, VCORE_NONE, "XML Error: [error near line %d]: %s (%s)", line, err ,arg ? arg : "");
+    
     // free memory
     v3_xml_free(&(root->xml));
 
@@ -151,33 +143,7 @@ const char * v3_xml_attr(struct v3_xml * xml, const char * attr) {
     return NULL; // found default
 }
 
-// same as v3_xml_get but takes an already initialized va_list
-static struct v3_xml * v3_xml_vget(struct v3_xml * xml, va_list ap) {
-    char * name = va_arg(ap, char *);
-    int idx = -1;
-
-    if ((name != NULL) && (*name != 0)) {
-        idx = va_arg(ap, int);    
-        xml = v3_xml_child(xml, name);
-    }
-    return (idx < 0) ? xml : v3_xml_vget(v3_xml_idx(xml, idx), ap);
-}
 
-// Traverses the xml tree to retrieve a specific subtag. Takes a variable
-// length list of tag names and indexes. The argument list must be terminated
-// by either an index of -1 or an empty string tag name. Example: 
-// title = v3_xml_get(library, "shelf", 0, "book", 2, "title", -1);
-// This retrieves the title of the 3rd book on the 1st shelf of library.
-// Returns NULL if not found.
-struct v3_xml * v3_xml_get(struct v3_xml * xml, ...) {
-    va_list ap;
-    struct v3_xml * r;
-
-    va_start(ap, xml);
-    r = v3_xml_vget(xml, ap);
-    va_end(ap);
-    return r;
-}
 
 
 // sets a flag for the given tag and returns the tag
@@ -302,6 +268,12 @@ static void v3_xml_char_content(struct v3_xml_root * root, char * s, size_t len,
            char * tmp = NULL;
 
            tmp = V3_Malloc((l = strlen(xml->txt)) + len);
+
+           if (!tmp) {
+               PrintError(VM_NONE, VCORE_NONE, "Cannot allocate in xml char content\n");
+               return ;
+           }
+
            strcpy(tmp, xml->txt);
            xml->txt = tmp;
        }
@@ -323,7 +295,7 @@ static int v3_xml_close_tag(struct v3_xml_root * root, char * name, char * s) {
     if ( (root->cur == NULL) || 
         (root->cur->name == NULL) || 
         (strcasecmp(name, root->cur->name))) {
-       v3_xml_err(root, s, "unexpected closing tag </%s>", name);
+       v3_xml_err(root, s, "unexpected closing tag", name);
        return -1;
     }
 
@@ -362,12 +334,17 @@ static void v3_xml_free_attr(char **attr) {
 static struct v3_xml * v3_xml_new(const char * name) {
 
     struct v3_xml_root * root = (struct v3_xml_root *)V3_Malloc(sizeof(struct v3_xml_root));
+
+    if (!root) {
+       PrintError(VM_NONE, VCORE_NONE, "Cannot allocate in xml_new\n");
+       return NULL;
+    }
+
     memset(root, 0, sizeof(struct v3_xml_root));
 
     root->xml.name = (char *)name;
     root->cur = &root->xml;
     root->xml.txt = "";
-    memset(root->err, 0, V3_XML_ERRL);
 
 
     return &root->xml;
@@ -457,6 +434,12 @@ static struct v3_xml * v3_xml_add_child(struct v3_xml * xml, const char * name,
     }
 
     child = (struct v3_xml *)V3_Malloc(sizeof(struct v3_xml));
+
+    if (!child) {
+       PrintError(VM_NONE, VCORE_NONE, "Cannot allocate in xml_add_child\n");
+       return NULL;
+    }
+
     memset(child, 0, sizeof(struct v3_xml));
 
     child->name = (char *)name;
@@ -497,10 +480,18 @@ static struct v3_xml * parse_str(char * buf, size_t len) {
     char ** attr; 
     int attr_idx;
 
+    if (buf==NULL) {
+      return NULL;
+    }
+
+    if (root==NULL) {
+      return NULL;
+    }
+
     root->str_ptr = buf;
 
     if (len == 0) {
-       v3_xml_err(root, NULL, "Empty XML String\n");
+       v3_xml_err(root, NULL, "Empty XML String", 0);
        return NULL;
     }
 
@@ -516,7 +507,7 @@ static struct v3_xml * parse_str(char * buf, size_t len) {
     }
 
     if (*buf == '\0') {
-       v3_xml_err(root, buf, "root tag missing");
+       v3_xml_err(root, buf, "root tag missing", 0);
        return NULL;
     }
 
@@ -528,7 +519,7 @@ static struct v3_xml * parse_str(char * buf, size_t len) {
            // new tag
 
             if (root->cur == NULL) {
-                v3_xml_err(root, tag_ptr, "markup outside of root element");
+                v3_xml_err(root, tag_ptr, "markup outside of root element", 0);
                return NULL;
            }
 
@@ -565,12 +556,31 @@ static struct v3_xml * parse_str(char * buf, size_t len) {
                                       ((attr_cnt * (2 * sizeof(char *))) + 
                                        (2 * sizeof(char *))));
 
+                   if (!attr) {
+                       PrintError(VM_NONE, VCORE_NONE, "Cannot reallocate in xml parse string\n");
+                       return NULL;
+                   }
+
                    attr[last_idx] = tmp_realloc(attr[last_idx - 2], 
                                                 attr_cnt,
                                                 (attr_cnt + 1)); 
+
+                   if (!attr[last_idx]) {
+                       PrintError(VM_NONE, VCORE_NONE, "Cannot reallocate in xml parse string\n");
+                       return NULL;
+                   }
+
                } else {
                    attr = V3_Malloc(4 * sizeof(char *)); 
+                   if (!attr) {
+                       PrintError(VM_NONE, VCORE_NONE, "Cannot allocate in xml parse string\n");
+                       return NULL;
+                   }
                    attr[last_idx] = V3_Malloc(2);
+                   if (!attr[last_idx]) {
+                       PrintError(VM_NONE, VCORE_NONE, "Cannot alloocate in xml parse string\n");
+                       return NULL;
+                   }
                }
 
                 attr[attr_idx] = buf; // set attribute name
@@ -600,8 +610,10 @@ static struct v3_xml * parse_str(char * buf, size_t len) {
                            // null terminate attribute val
                            *(buf++) = '\0';
                        } else {
+                           char err_buf[2] = {quote_char,0};
+
                             v3_xml_free_attr(attr);
-                            v3_xml_err(root, tag_ptr, "missing %c", quote_char);
+                            v3_xml_err(root, tag_ptr, "missing quote char", err_buf);
                            return NULL;
                         }
 
@@ -624,7 +636,7 @@ static struct v3_xml * parse_str(char * buf, size_t len) {
                     if (attr_idx > 0) {
                        v3_xml_free_attr(attr);
                    }
-                   v3_xml_err(root, tag_ptr, "missing >");
+                   v3_xml_err(root, tag_ptr, "missing >", 0);
                    return NULL;
                 }
                 v3_xml_open_tag(root, tag_ptr, attr);
@@ -639,7 +651,7 @@ static struct v3_xml * parse_str(char * buf, size_t len) {
                 if (attr_idx > 0) {
                    v3_xml_free_attr(attr);
                }
-               v3_xml_err(root, tag_ptr, "missing >"); 
+               v3_xml_err(root, tag_ptr, "missing >", 0); 
                return NULL;
             }
         } else if (*buf == '/') { 
@@ -649,7 +661,7 @@ static struct v3_xml * parse_str(char * buf, size_t len) {
             
            quote_char = *buf;
            if ((*buf == '\0') && (last_char != '>')) {
-               v3_xml_err(root, tag_ptr, "missing >");
+               v3_xml_err(root, tag_ptr, "missing >", 0);
                return NULL;
             }
 
@@ -668,7 +680,7 @@ static struct v3_xml * parse_str(char * buf, size_t len) {
             if ( ((buf = strstr(buf + 3, "--")) == 0) || 
                 ((*(buf += 2) != '>') && (*buf)) ||
                 ((!*buf) && (last_char != '>'))) {
-               v3_xml_err(root, tag_ptr, "unclosed <!--");
+               v3_xml_err(root, tag_ptr, "unclosed <!--", 0);
                return NULL;
            }
         } else if (! strncmp(buf, "![CDATA[", 8)) { 
@@ -676,15 +688,16 @@ static struct v3_xml * parse_str(char * buf, size_t len) {
             if ((buf = strstr(buf, "]]>"))) {
                 v3_xml_char_content(root, tag_ptr + 8, (buf += 2) - tag_ptr - 10, 'c');
            } else {
-               v3_xml_err(root, tag_ptr, "unclosed <![CDATA[");
+               v3_xml_err(root, tag_ptr, "unclosed <![CDATA[", 0);
                return NULL;
            }
        } else {
-           v3_xml_err(root, tag_ptr, "unexpected <");
+           v3_xml_err(root, tag_ptr, "unexpected <",0);
            return NULL;
         }
 
-        if (! buf || ! *buf) {
+       // at this point, buf cannot be NULL
+        if (!*buf) {
            break;
        }
 
@@ -715,10 +728,10 @@ static struct v3_xml * parse_str(char * buf, size_t len) {
     if (root->cur == NULL) {
        return &root->xml;
     } else if (root->cur->name == NULL) {
-       v3_xml_err(root, tag_ptr, "root tag missing");
+       v3_xml_err(root, tag_ptr, "root tag missing",0);
        return NULL;
     } else {
-       v3_xml_err(root, tag_ptr, "unclosed tag <%s>", root->cur->name);
+       v3_xml_err(root, tag_ptr, "unclosed tag", root->cur->name);
        return NULL;
     }
 }
@@ -734,6 +747,12 @@ struct v3_xml * v3_xml_parse(char * buf) {
 
     str_len = strlen(buf);
     xml_buf = (char *)V3_Malloc(str_len + 1);
+
+    if (!xml_buf) {
+       PrintError(VM_NONE, VCORE_NONE, "Cannot allocate in xml parse\n");
+       return NULL;
+    }
+
     strcpy(xml_buf, buf);
 
     return parse_str(xml_buf, str_len);
@@ -825,10 +844,26 @@ struct v3_xml * v3_xml_set_attr(struct v3_xml * xml, const char * name, const ch
            // first attribute
             xml->attr = V3_Malloc(4 * sizeof(char *));
 
+           if (!xml->attr) {
+               PrintError(VM_NONE, VCORE_NONE, "Cannot allocate in xml set attr\n");
+               return NULL;
+           }
+
            // empty list of malloced names/vals
             xml->attr[1] = strdup(""); 
+
+           if (!xml->attr[1]) {
+               PrintError(VM_NONE, VCORE_NONE, "Cannot strdup in xml set attr\n");
+               return NULL;
+           }
+
         } else {
            xml->attr = tmp_realloc(xml->attr, l * sizeof(char *), (l + 4) * sizeof(char *));
+
+           if (!xml->attr) {
+               PrintError(VM_NONE, VCORE_NONE, "Cannot reallocate in xml set attr\n");
+               return NULL;
+           }
        }
 
        // set attribute name
@@ -841,6 +876,12 @@ struct v3_xml * v3_xml_set_attr(struct v3_xml * xml, const char * name, const ch
                                       strlen(xml->attr[l + 1]),
                                       (c = strlen(xml->attr[l + 1])) + 2);
 
+
+       if (!xml->attr[l + 3]) {
+           PrintError(VM_NONE, VCORE_NONE, "Cannot reallocate in xml set attr\n");
+           return NULL;
+       }
+
        // set name/value as not malloced
         strcpy(xml->attr[l + 3] + c, " "); 
 
@@ -1045,7 +1086,7 @@ static char *toxml_r(struct v3_xml * xml, char **s, size_t *len, size_t *max,
 
     *len += sprintf(*s + *len, "</%s>", xml->name); // close tag
 
-    while (txt[off] && off < xml->off) off++; // make sure off is within bounds
+    while (off < xml->off && txt[off]) off++; // make sure off is within bounds
     return (xml->ordered) ? toxml_r(xml->ordered, s, len, max, off)
                           : ampencode(txt + off, -1, s, len, max, 0);
 }
@@ -1057,7 +1098,14 @@ char * v3_xml_tostr(struct v3_xml * xml) {
     struct v3_xml * o = (xml) ? xml->ordered : NULL;
     struct v3_xml_root * root = (struct v3_xml_root *)xml;
     size_t len = 0, max = V3_XML_BUFSIZE;
-    char *s = strcpy(V3_Malloc(max), "");
+    char *s = V3_Malloc(max);
+
+    if (!s) {
+       PrintError(VM_NONE, VCORE_NONE, "Cannot allocate in xml tostrr\n");
+       return NULL;
+    }
+
+    strcpy(s, "");
 
     if (! xml || ! xml->name) return tmp_realloc(s, max, len + 1);
     while (root->xml.parent) root = (struct v3_xml_root *)root->xml.parent; // root tag