]> xenbits.xensource.com Git - libvirt.git/commitdiff
commandhelper: Use automatic memory management in main
authorTim Wiederhake <twiederh@redhat.com>
Mon, 1 Feb 2021 11:28:04 +0000 (12:28 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 2 Feb 2021 14:00:54 +0000 (15:00 +0100)
Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
tests/commandhelper.c

index df9be7d424b3c20a91bdbdfefa0813eb7de76bf4..bf6a5baa4093b0c88e4db34953b6a8bc7be203b0 100644 (file)
@@ -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