From 62e22785c706e7ee7f99a6600d4505dc890838ef Mon Sep 17 00:00:00 2001 Message-Id: <62e22785c706e7ee7f99a6600d4505dc890838ef@dist-git> From: Peter Krempa Date: Fri, 19 Jun 2020 11:43:11 +0200 Subject: [PATCH] util: string: Introduce macro for automatic string lists MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Similar to VIR_AUTOPTR, VIR_AUTOSTRINGLIST defines a list of strings which will be freed if the pointer is leaving scope. Signed-off-by: Peter Krempa Reviewed-by: Erik Skultety (cherry picked from commit daefda165b28b399478137c75b5466f865c65be5) https://bugzilla.redhat.com/show_bug.cgi?id=1839992 Conflicts: - src/util/virstring.h - Context. This is RHEL-7.9; did you really think this would be a clean backport? Plenty of patches changed virstring.h in the last two years. Signed-off-by: Michal Privoznik Message-Id: Reviewed-by: Ján Tomko --- src/libvirt_private.syms | 1 + src/util/virstring.c | 10 ++++++++++ src/util/virstring.h | 10 ++++++++++ 3 files changed, 21 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 2a3950fd35..3e5448af94 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2917,6 +2917,7 @@ virStringHasControlChars; virStringIsEmpty; virStringIsPrintable; virStringListAdd; +virStringListAutoFree; virStringListFree; virStringListFreeCount; virStringListGetFirstWithPrefix; diff --git a/src/util/virstring.c b/src/util/virstring.c index 15f367af7c..0f13b6c664 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -332,6 +332,16 @@ void virStringListFree(char **strings) } +void virStringListAutoFree(char ***strings) +{ + if (!*strings) + return; + + virStringListFree(*strings); + *strings = NULL; +} + + /** * virStringListFreeCount: * @strings: array of strings to free diff --git a/src/util/virstring.h b/src/util/virstring.h index 607ae66e99..e68b9eec79 100644 --- a/src/util/virstring.h +++ b/src/util/virstring.h @@ -53,6 +53,7 @@ int virStringListCopy(char ***dst, const char **src); void virStringListFree(char **strings); +void virStringListAutoFree(char ***strings); void virStringListFreeCount(char **strings, size_t count); @@ -309,4 +310,13 @@ int virStringParsePort(const char *str, unsigned int *port) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK; +/** + * VIR_AUTOSTRINGLIST: + * + * Declares a NULL-terminated list of strings which will be automatically freed + * when the pointer goes out of scope. + */ +# define VIR_AUTOSTRINGLIST \ + __attribute__((cleanup(virStringListAutoFree))) char ** + #endif /* __VIR_STRING_H__ */ -- 2.27.0