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@xxxxxxxxxx> --- src/util/virbuffer.h | 3 +++ tests/virbuftest.c | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/util/virbuffer.h b/src/util/virbuffer.h index 7e4e7645df..b399c90154 100644 --- a/src/util/virbuffer.h +++ b/src/util/virbuffer.h @@ -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 * diff --git a/tests/virbuftest.c b/tests/virbuftest.c index bdb0a5e934..34f02b1281 100644 --- a/tests/virbuftest.c +++ b/tests/virbuftest.c @@ -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 { \ -- 2.20.1