[libvirt PATCH v2 17/20] commandhelper: Use automatic memory management in printEnvironment

Tim Wiederhake twiederh at redhat.com
Mon Feb 1 11:28:01 UTC 2021


Signed-off-by: Tim Wiederhake <twiederh at redhat.com>
---
 tests/commandhelper.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/tests/commandhelper.c b/tests/commandhelper.c
index 19dfc09151..854f3c09bf 100644
--- a/tests/commandhelper.c
+++ b/tests/commandhelper.c
@@ -54,6 +54,12 @@ static void cleanupArguments(struct Arguments **ptr)
     free(args);
 }
 
+static void cleanupGeneric(void *ptr)
+{
+    void **ptrptr = ptr;
+    free (*ptrptr);
+}
+
 static struct Arguments *parseArguments(int argc, char** argv)
 {
     cleanup(struct Arguments *, cleanupArguments) args = NULL;
@@ -124,16 +130,15 @@ static int envsort(const void *a, const void *b)
 
 static int printEnvironment(FILE *log)
 {
-    char **newenv;
+    cleanup(char **, cleanupGeneric) newenv = NULL;
     size_t length;
     size_t i;
-    int ret = -1;
 
     for (length = 0; environ[length]; length++) {
     }
 
     if (!(newenv = malloc(sizeof(*newenv) * length)))
-        goto cleanup;
+        return -1;
 
     for (i = 0; i < length; i++) {
         newenv[i] = environ[i];
@@ -148,12 +153,7 @@ static int printEnvironment(FILE *log)
             fprintf(log, "ENV:%s\n", newenv[i]);
     }
 
-    ret = 0;
-
- cleanup:
-    if (newenv)
-        free(newenv);
-    return ret;
+    return 0;
 }
 
 static int printFds(FILE *log)
-- 
2.26.2




More information about the libvir-list mailing list