c480ed
From b9490d128b1a65d4d1ca273ddd54c696fa62ad73 Mon Sep 17 00:00:00 2001
c480ed
Message-Id: <b9490d128b1a65d4d1ca273ddd54c696fa62ad73@dist-git>
c480ed
From: Peter Krempa <pkrempa@redhat.com>
c480ed
Date: Fri, 21 Jun 2019 09:26:01 +0200
c480ed
Subject: [PATCH] util: string: Introduce macro for automatic string lists
c480ed
MIME-Version: 1.0
c480ed
Content-Type: text/plain; charset=UTF-8
c480ed
Content-Transfer-Encoding: 8bit
c480ed
c480ed
Similar to VIR_AUTOPTR, VIR_AUTOSTRINGLIST defines a list of strings
c480ed
which will be freed if the pointer is leaving scope.
c480ed
c480ed
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
c480ed
Reviewed-by: Erik Skultety <eskultet@redhat.com>
c480ed
(cherry picked from commit daefda165b28b399478137c75b5466f865c65be5)
c480ed
c480ed
https://bugzilla.redhat.com/show_bug.cgi?id=1697627
c480ed
c480ed
Conflicts:
c480ed
	src/util/virstring.h
c480ed
            - different header file guard
c480ed
            - no AUTOPTR for string lists
c480ed
c480ed
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
c480ed
Message-Id: <7f1e7edf9c9a096fde1a2adf37090f8190e99be2.1561068591.git.jdenemar@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
---
c480ed
 src/libvirt_private.syms |  1 +
c480ed
 src/util/virstring.c     | 10 ++++++++++
c480ed
 src/util/virstring.h     | 10 ++++++++++
c480ed
 3 files changed, 21 insertions(+)
c480ed
c480ed
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
c480ed
index a20e0593f0..06374deaae 100644
c480ed
--- a/src/libvirt_private.syms
c480ed
+++ b/src/libvirt_private.syms
c480ed
@@ -2923,6 +2923,7 @@ virStringHasControlChars;
c480ed
 virStringIsEmpty;
c480ed
 virStringIsPrintable;
c480ed
 virStringListAdd;
c480ed
+virStringListAutoFree;
c480ed
 virStringListFree;
c480ed
 virStringListFreeCount;
c480ed
 virStringListGetFirstWithPrefix;
c480ed
diff --git a/src/util/virstring.c b/src/util/virstring.c
c480ed
index 15f367af7c..0f13b6c664 100644
c480ed
--- a/src/util/virstring.c
c480ed
+++ b/src/util/virstring.c
c480ed
@@ -332,6 +332,16 @@ void virStringListFree(char **strings)
c480ed
 }
c480ed
 
c480ed
 
c480ed
+void virStringListAutoFree(char ***strings)
c480ed
+{
c480ed
+    if (!*strings)
c480ed
+        return;
c480ed
+
c480ed
+    virStringListFree(*strings);
c480ed
+    *strings = NULL;
c480ed
+}
c480ed
+
c480ed
+
c480ed
 /**
c480ed
  * virStringListFreeCount:
c480ed
  * @strings: array of strings to free
c480ed
diff --git a/src/util/virstring.h b/src/util/virstring.h
c480ed
index 607ae66e99..e68b9eec79 100644
c480ed
--- a/src/util/virstring.h
c480ed
+++ b/src/util/virstring.h
c480ed
@@ -53,6 +53,7 @@ int virStringListCopy(char ***dst,
c480ed
                       const char **src);
c480ed
 
c480ed
 void virStringListFree(char **strings);
c480ed
+void virStringListAutoFree(char ***strings);
c480ed
 void virStringListFreeCount(char **strings,
c480ed
                             size_t count);
c480ed
 
c480ed
@@ -309,4 +310,13 @@ int virStringParsePort(const char *str,
c480ed
                        unsigned int *port)
c480ed
     ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
c480ed
 
c480ed
+/**
c480ed
+ * VIR_AUTOSTRINGLIST:
c480ed
+ *
c480ed
+ * Declares a NULL-terminated list of strings which will be automatically freed
c480ed
+ * when the pointer goes out of scope.
c480ed
+ */
c480ed
+# define VIR_AUTOSTRINGLIST \
c480ed
+        __attribute__((cleanup(virStringListAutoFree))) char **
c480ed
+
c480ed
 #endif /* __VIR_STRING_H__ */
c480ed
-- 
c480ed
2.22.0
c480ed