[PATCH 1/3] virbuffer: Don't leak memory in virBufferAddBuffer

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

 



If an error occurs in a virBuffer* API the idea is to free the
content immediately and set @error member used in error reporting
later. Well, this is not what how virBufferAddBuffer works.

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 src/util/virbuffer.c |  2 +-
 tests/virbuftest.c   | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c
index 54703a28d8..b2ae7963a1 100644
--- a/src/util/virbuffer.c
+++ b/src/util/virbuffer.c
@@ -197,7 +197,7 @@ virBufferAddBuffer(virBufferPtr buf, virBufferPtr toadd)
 
     if (buf->error || toadd->error) {
         if (!buf->error)
-            buf->error = toadd->error;
+            virBufferSetError(buf, toadd->error);
         goto done;
     }
 
diff --git a/tests/virbuftest.c b/tests/virbuftest.c
index 778754d7c1..bdd0c16462 100644
--- a/tests/virbuftest.c
+++ b/tests/virbuftest.c
@@ -303,6 +303,37 @@ static int testBufAddBuffer(const void *data ATTRIBUTE_UNUSED)
     return ret;
 }
 
+static int
+testBufAddBuffer2(const void *opaque ATTRIBUTE_UNUSED)
+{
+    virBuffer buf1 = VIR_BUFFER_INITIALIZER;
+    virBuffer buf2 = VIR_BUFFER_INITIALIZER;
+    int ret = -1;
+
+    /* Intent of this test is to demonstrate a memleak that happen with
+     * virBufferAddBuffer */
+
+    virBufferAddLit(&buf1, "Hello world!\n");
+    virBufferAddLit(&buf2, "Hello world!\n");
+
+    /* Intentional usage error */
+    virBufferAdjustIndent(&buf2, -2);
+
+    virBufferAddBuffer(&buf1, &buf2);
+
+    if (virBufferCurrentContent(&buf1) ||
+        !virBufferCurrentContent(&buf2)) {
+        VIR_TEST_DEBUG("Unexpected buffer content");
+        goto cleanup;
+    }
+
+    ret = 0;
+ cleanup:
+    virBufferFreeAndReset(&buf1);
+    virBufferFreeAndReset(&buf2);
+    return ret;
+}
+
 struct testBufAddStrData {
     const char *data;
     const char *expect;
@@ -460,6 +491,7 @@ mymain(void)
     DO_TEST("Auto-indentation", testBufAutoIndent, 0);
     DO_TEST("Trim", testBufTrim, 0);
     DO_TEST("AddBuffer", testBufAddBuffer, 0);
+    DO_TEST("AddBuffer2", testBufAddBuffer2, 0);
     DO_TEST("set indent", testBufSetIndent, 0);
     DO_TEST("autoclean", testBufferAutoclean, 0);
 
-- 
2.21.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux