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

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


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

diff --git a/tests/commandhelper.c b/tests/commandhelper.c
index df9be7d424..bf6a5baa40 100644
--- a/tests/commandhelper.c
+++ b/tests/commandhelper.c
@@ -67,6 +67,12 @@ static void cleanupStringList(char ***ptr)
     free(strings);
 }
 
+static void cleanupFile(FILE **ptr)
+{
+    FILE *file = *ptr;
+    fclose(file);
+}
+
 static void cleanupGeneric(void *ptr)
 {
     void **ptrptr = ptr;
@@ -330,42 +336,34 @@ static int printInput(struct Arguments *args)
 }
 
 int main(int argc, char **argv) {
-    struct Arguments *args = parseArguments(argc, argv);
-    FILE *log = fopen(abs_builddir "/commandhelper.log", "w");
-    int ret = EXIT_FAILURE;
+    cleanup(struct Arguments *, cleanupArguments) args = NULL;
+    cleanup(FILE *, cleanupFile) log = NULL;
 
-    if (!log || !args)
-        goto cleanup;
+    if (!(log = fopen(abs_builddir "/commandhelper.log", "w")))
+        return EXIT_FAILURE;
+
+    if (!(args = parseArguments(argc, argv)))
+        return EXIT_FAILURE;
 
     printArguments(log, argc, argv);
 
     if (printEnvironment(log) != 0)
-        goto cleanup;
+        return EXIT_FAILURE;
 
     if (printFds(log) != 0)
-        goto cleanup;
+        return EXIT_FAILURE;
 
     printDaemonization(log, args);
 
     if (printCwd(log) != 0)
-        goto cleanup;
+        return EXIT_FAILURE;
 
     fprintf(log, "UMASK:%04o\n", umask(0));
 
     if (printInput(args) != 0)
-        goto cleanup;
-
-    ret = EXIT_SUCCESS;
+        return EXIT_FAILURE;
 
- cleanup:
-    if (args) {
-        if (args->readfds)
-            free(args->readfds);
-        free(args);
-    }
-    if (log)
-        fclose(log);
-    return ret;
+    return EXIT_SUCCESS;
 }
 
 #else
-- 
2.26.2




More information about the libvir-list mailing list