VIR_AUTODISPOSE_STR is similar to VIR_AUTOFREE(char *) but uses
virDispose for clearing of the stored string.
This patch also refactors VIR_DISPOSE to use the new helper which is
used for the new macro.
Signed-off-by: Peter Krempa <pkrempa(a)redhat.com>
---
src/libvirt_private.syms | 1 +
src/util/viralloc.c | 16 ++++++++++++++++
src/util/viralloc.h | 14 ++++++++++++--
3 files changed, 29 insertions(+), 2 deletions(-)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 73ef24d66f..bae7f646fe 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1427,6 +1427,7 @@ virAllocTestOOM;
virAllocVar;
virDeleteElementsN;
virDispose;
+virDisposeString;
virExpandN;
virFree;
virInsertElementsN;
diff --git a/src/util/viralloc.c b/src/util/viralloc.c
index b583294760..e82bfa0acd 100644
--- a/src/util/viralloc.c
+++ b/src/util/viralloc.c
@@ -618,3 +618,19 @@ void virDispose(void *ptrptr,
*countptr = 0;
errno = save_errno;
}
+
+
+/**
+ * virDisposeString:
+ * @ptrptr: pointer to pointer for a string which should be sanitized and cleared
+ *
+ * See virDispose.
+ */
+void
+virDisposeString(char **strptr)
+{
+ if (!*strptr)
+ return;
+
+ virDispose(strptr, strlen(*strptr), sizeof(char), NULL);
+}
diff --git a/src/util/viralloc.h b/src/util/viralloc.h
index 226d4d9b64..a1708b772c 100644
--- a/src/util/viralloc.h
+++ b/src/util/viralloc.h
@@ -79,6 +79,8 @@ void virFree(void *ptrptr) ATTRIBUTE_NONNULL(1);
void virDispose(void *ptrptr, size_t count, size_t element_size, size_t *countptr)
ATTRIBUTE_NONNULL(1);
+void virDisposeString(char **strptr)
+ ATTRIBUTE_NONNULL(1);
/**
* VIR_ALLOC:
@@ -575,9 +577,17 @@ void virDispose(void *ptrptr, size_t count, size_t element_size,
size_t *countpt
*
* This macro is not safe to be used on arguments with side effects.
*/
-# define VIR_DISPOSE_STRING(ptr) virDispose(1 ? (void *) &(ptr) : (ptr), \
- (ptr) ? strlen((ptr)) : 0, 1, NULL)
+# define VIR_DISPOSE_STRING(ptr) virDisposeString(&(ptr))
+/**
+ * VIR_AUTODISPOSE_STR:
+ *
+ * Macro to automatically free and clear the memory allocated to
+ * the string variable declared with it by calling virDisposeString
+ * when the variable goes out of scope.
+ */
+# define VIR_AUTODISPOSE_STR \
+ __attribute__((cleanup(virDisposeString))) char *
/**
* VIR_DISPOSE:
--
2.20.1