mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-22 05:35:25 +00:00
util: buffer: Introduce VIR_AUTOCLEAN function for virBuffer
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>
This commit is contained in:
parent
c73f752e46
commit
55ee6ac8de
@ -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
|
||||
*
|
||||
|
@ -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 { \
|
||||
|
Loading…
Reference in New Issue
Block a user