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 sanity-checking of before/after null-check and copy+paste errors (Coverit...
[palacios.git] / palacios / src / palacios / vmm_xml.c
index d09a176..c2213ca 100644 (file)
@@ -67,6 +67,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;
     }
 
@@ -97,7 +98,7 @@ static void v3_xml_err(struct v3_xml_root * root, char * xml_str, const char * e
     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: %s\n", root->err);
 
     // free memory
     v3_xml_free(&(root->xml));
@@ -302,6 +303,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;
        }
@@ -362,6 +369,12 @@ 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;
@@ -457,6 +470,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,6 +516,10 @@ static struct v3_xml * parse_str(char * buf, size_t len) {
     char ** attr; 
     int attr_idx;
 
+    if (!buf) {
+       return NULL;
+    }
+
     root->str_ptr = buf;
 
     if (len == 0) {
@@ -565,12 +588,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
@@ -734,6 +776,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 +873,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 +905,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 +1115,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 +1127,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