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

[Libvir] [PATCH] test.c: Avoid segfault upon malloc failure, and plug a leak.



Testing exposed a tiny leak:

  $ valgrind --leak-check=full ./virsh --connect \
    test://$PWD/../docs/testnode.xml save fc4 /dev/null
    ...
  ==11077== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 1)
  ==11077== malloc/free: in use at exit: 24,677 bytes in 367 blocks.
  ==11077== malloc/free: 23,521 allocs, 23,154 frees, 3,424,316 bytes allocated.
  ==11077== For counts of detected errors, rerun with: -v
  ==11077== searching for pointers to 367 not-freed blocks.
  ==11077== checked 538,400 bytes.
  ==11077==
  ==11077== 4,000 bytes in 1 blocks are definitely lost in loss record 26 of 27
  ==11077==    at 0x4A059F6: malloc (vg_replace_malloc.c:149)
  ==11077==    by 0x41C53A: virBufferNew (buf.c:132)
  ==11077==    by 0x41AB22: testDomainDumpXML (test.c:1473)
  ==11077==    by 0x41A1B5: testDomainSave (test.c:1284)
  ==11077==    by 0x41120B: virDomainSave (libvirt.c:1342)
  ==11077==    by 0x406169: cmdSave (virsh.c:1057)
  ==11077==    by 0x40C5F9: vshCommandRun (virsh.c:4032)
  ==11077==    by 0x40E6AF: main (virsh.c:4991)
  ==11077==
  ==11077== LEAK SUMMARY:
  ==11077==    definitely lost: 4,000 bytes in 1 blocks.
  ==11077==      possibly lost: 0 bytes in 0 blocks.
  ==11077==    still reachable: 20,677 bytes in 366 blocks.
  ==11077==         suppressed: 0 bytes in 0 blocks.

Here's the fix:

	test.c: Avoid segfault upon malloc failure, and plug a leak.
	* src/test.c (testDomainSave):

Time permitting, I will add tests to exercise as many of the
commands as possible.

---
 src/test.c |    9 ++++++++-
 1 files changed, 8 insertions(+), 1 deletions(-)

diff --git a/src/test.c b/src/test.c
index 35e41a3..85170d9 100644
--- a/src/test.c
+++ b/src/test.c
@@ -1,7 +1,7 @@
 /*
  * test.c: A "mock" hypervisor for use by application unit tests
  *
- * Copyright (C) 2006-2007 Red Hat, Inc.
+ * Copyright (C) 2006-2008 Red Hat, Inc.
  * Copyright (C) 2006 Daniel P. Berrange
  *
  * This library is free software; you can redistribute it and/or
@@ -1281,6 +1281,11 @@ static int testDomainSave(virDomainPtr domain,
     GET_DOMAIN(domain, -1);

     xml = testDomainDumpXML(domain, 0);
+    if (xml == NULL) {
+        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
+                  "cannot allocate space for metadata");
+        return (-1);
+    }

     if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
         testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
@@ -1303,9 +1308,11 @@ static int testDomainSave(virDomainPtr domain,
     if (write(fd, xml, len) != len) {
         testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
                   "cannot write metadata");
+        free(xml);
         close(fd);
         return (-1);
     }
+    free(xml);
     if (close(fd) < 0) {
         testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
                   "cannot save domain data");
--
1.5.4.rc5.1.g0fa73


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