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