]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: buffer: Introduce VIR_AUTOCLEAN function for virBuffer
authorPeter Krempa <pkrempa@redhat.com>
Thu, 21 Feb 2019 15:37:50 +0000 (16:37 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 22 Feb 2019 09:05:45 +0000 (10:05 +0100)
virBuffer is almost always stack-allocated, but requires freeing of the
internals on error. Introduce a VIR_AUTOCLEAN function to deal with
this.

Along with the addition add a test which would leak the buffer contents
if it weren't autocleaned.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
src/util/virbuffer.h
tests/virbuftest.c

index 7e4e7645dfc9b9d48e8f2f93899e58b20ec17232..b399c9015477f2d1db2efdbb76127f3fba8102eb 100644 (file)
@@ -59,6 +59,9 @@ int virBufferCheckErrorInternal(const virBuffer *buf,
                                 const char *funcname,
                                 size_t linenr)
     ATTRIBUTE_NONNULL(1);
+
+VIR_DEFINE_AUTOCLEAN_FUNC(virBuffer, virBufferFreeAndReset);
+
 /**
  * virBufferCheckError
  *
index bdb0a5e934ddc4619fe04871e6ffc86245dfc3e4..34f02b128120abf95baf0ac324925d2f771f6579 100644 (file)
@@ -429,6 +429,17 @@ testBufSetIndent(const void *opaque ATTRIBUTE_UNUSED)
 }
 
 
+/* Result of this shows up only in valgrind or similar */
+static int
+testBufferAutoclean(const void *opaque ATTRIBUTE_UNUSED)
+{
+    VIR_AUTOCLEAN(virBuffer) buf = VIR_BUFFER_INITIALIZER;
+
+    virBufferAddLit(&buf, "test test test\n");
+    return 0;
+}
+
+
 static int
 mymain(void)
 {
@@ -448,6 +459,7 @@ mymain(void)
     DO_TEST("Trim", testBufTrim, 0);
     DO_TEST("AddBuffer", testBufAddBuffer, 0);
     DO_TEST("set indent", testBufSetIndent, 0);
+    DO_TEST("autoclean", testBufferAutoclean, 0);
 
 #define DO_TEST_ADD_STR(DATA, EXPECT) \
     do { \