[Libvir] PATCH Fix a couple of mem leaks

Daniel P. Berrange berrange at redhat.com
Mon Jul 30 21:14:25 UTC 2007


I ran 'make valgrind' for the first time in too long and found a hanful of
memory leaks in code exercised by the test suite. The attached patch fixes
them all.

I'm sure there are more lurking though - the test suite is mainly focused
on exercising & validating correctness of parsing routines - its not really
tried doing any validation of failure scenaarios. So I fear there's probably
a number of error code paths where we don't cleanup properly, and/or don't
reject invalid data. Its an area which should be addressed by someone with
some copious free time ;-) 

Dan.
-- 
|=- Red Hat, Engineering, Emerging Technologies, Boston.  +1 978 392 2496 -=|
|=-           Perl modules: http://search.cpan.org/~danberr/              -=|
|=-               Projects: http://freshmeat.net/~danielpb/               -=|
|=-  GnuPG: 7D3B9505   F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505  -=| 
-------------- next part --------------
Index: src/qemu_conf.c
===================================================================
RCS file: /data/cvs/libvirt/src/qemu_conf.c,v
retrieving revision 1.8
diff -u -p -r1.8 qemu_conf.c
--- src/qemu_conf.c	30 Jul 2007 18:50:07 -0000	1.8
+++ src/qemu_conf.c	30 Jul 2007 19:47:11 -0000
@@ -135,6 +135,7 @@ struct qemud_network *qemudFindNetworkBy
 void qemudFreeVMDef(struct qemud_vm_def *def) {
     struct qemud_vm_disk_def *disk = def->disks;
     struct qemud_vm_net_def *net = def->nets;
+    struct qemud_vm_input_def *input = def->inputs;
 
     while (disk) {
         struct qemud_vm_disk_def *prev = disk;
@@ -146,6 +147,11 @@ void qemudFreeVMDef(struct qemud_vm_def 
         net = net->next;
         free(prev);
     }
+    while (input) {
+        struct qemud_vm_input_def *prev = input;
+        input = input->next;
+        free(prev);
+    }
     free(def);
 }
 
Index: src/xml.c
===================================================================
RCS file: /data/cvs/libvirt/src/xml.c,v
retrieving revision 1.85
diff -u -p -r1.85 xml.c
--- src/xml.c	18 Jul 2007 21:08:22 -0000	1.85
+++ src/xml.c	30 Jul 2007 19:47:12 -0000
@@ -70,8 +70,11 @@ virXPathString(const char *xpath, xmlXPa
     }
     obj = xmlXPathEval(BAD_CAST xpath, ctxt);
     if ((obj == NULL) || (obj->type != XPATH_STRING) ||
-        (obj->stringval == NULL) || (obj->stringval[0] == 0))
+        (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
+        if (obj)
+            xmlXPathFreeObject(obj);
         return(NULL);
+    }
     ret = strdup((char *) obj->stringval);
     xmlXPathFreeObject(obj);
     if (ret == NULL) {
@@ -618,6 +621,8 @@ virDomainParseXMLOSDescHVM(virConnectPtr
     if (str != NULL && !strcmp(str, "localtime")) {
         virBufferAdd(buf, "(localtime 1)", 13);
     }
+    if (str)
+        free(str);
 
     virBufferAdd(buf, "))", 2);
 
Index: tests/qemuxml2argvtest.c
===================================================================
RCS file: /data/cvs/libvirt/tests/qemuxml2argvtest.c,v
retrieving revision 1.2
diff -u -p -r1.2 qemuxml2argvtest.c
--- tests/qemuxml2argvtest.c	24 Jul 2007 14:30:06 -0000	1.2
+++ tests/qemuxml2argvtest.c	30 Jul 2007 19:47:12 -0000
@@ -22,7 +22,7 @@ static int testCompareXMLToArgvFiles(con
     char **argv = NULL;
     char **tmp = NULL;
     int ret = -1, len;
-    struct qemud_vm_def *vmdef;
+    struct qemud_vm_def *vmdef = NULL;
     struct qemud_vm vm;
 
     if (virtTestLoadFile(xml, &xmlPtr, MAX_FILE) < 0)
@@ -80,6 +80,7 @@ static int testCompareXMLToArgvFiles(con
         free(argv);
     }
 
+    qemudFreeVMDef(vmdef);
     return ret;
 }
 
Index: tests/qemuxml2xmltest.c
===================================================================
RCS file: /data/cvs/libvirt/tests/qemuxml2xmltest.c,v
retrieving revision 1.2
diff -u -p -r1.2 qemuxml2xmltest.c
--- tests/qemuxml2xmltest.c	24 Jul 2007 14:30:06 -0000	1.2
+++ tests/qemuxml2xmltest.c	30 Jul 2007 19:47:12 -0000
@@ -19,7 +19,7 @@ static int testCompareXMLToXMLFiles(cons
     char *xmlPtr = &(xmlData[0]);
     char *actual = NULL;
     int ret = -1;
-    struct qemud_vm_def *vmdef;
+    struct qemud_vm_def *vmdef = NULL;
     struct qemud_vm vm;
 
     if (virtTestLoadFile(xml, &xmlPtr, MAX_FILE) < 0)
@@ -48,7 +48,7 @@ static int testCompareXMLToXMLFiles(cons
 
  fail:
     free(actual);
-
+    qemudFreeVMDef(vmdef);
     return ret;
 }
 


More information about the libvir-list mailing list