[PATCH 02/25] virLogDaemonPreExecRestart: Refactor memory cleanup

Peter Krempa pkrempa at redhat.com
Fri Feb 12 17:55:15 UTC 2021


Switch to using the 'g_auto*' helpers.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/logging/log_daemon.c | 54 +++++++++++++++++-----------------------
 1 file changed, 23 insertions(+), 31 deletions(-)

diff --git a/src/logging/log_daemon.c b/src/logging/log_daemon.c
index 770f6dd273..ceffa6a368 100644
--- a/src/logging/log_daemon.c
+++ b/src/logging/log_daemon.c
@@ -505,62 +505,54 @@ virLogDaemonPreExecRestart(const char *state_file,
                            virNetDaemonPtr dmn,
                            char **argv)
 {
-    virJSONValuePtr child;
-    char *state = NULL;
-    virJSONValuePtr object = virJSONValueNewObject();
-    char *magic = NULL;
+    g_autoptr(virJSONValue) object = virJSONValueNewObject();
+    g_autoptr(virJSONValue) daemon = NULL;
+    g_autoptr(virJSONValue) handler = NULL;
+    g_autofree char *state = NULL;
+    g_autofree char *magic = NULL;

     VIR_DEBUG("Running pre-restart exec");

-    if (!(child = virNetDaemonPreExecRestart(dmn)))
-        goto cleanup;
+    if (!(daemon = virNetDaemonPreExecRestart(dmn)))
+        return -1;

-    if (virJSONValueObjectAppend(object, "daemon", child) < 0) {
-        virJSONValueFree(child);
-        goto cleanup;
-    }
+    if (virJSONValueObjectAppend(object, "daemon", daemon) < 0)
+        return -1;
+    daemon = NULL;

     if (!(magic = virLogDaemonGetExecRestartMagic()))
-        goto cleanup;
+        return -1;

     if (virJSONValueObjectAppendString(object, "magic", magic) < 0)
-        goto cleanup;
-
-    if (!(child = virLogHandlerPreExecRestart(logDaemon->handler)))
-        goto cleanup;
+        return -1;

-    if (virJSONValueObjectAppend(object, "handler", child) < 0) {
-        virJSONValueFree(child);
-        goto cleanup;
-    }
+    if (!(handler = virLogHandlerPreExecRestart(logDaemon->handler)))
+        return -1;

+    if (virJSONValueObjectAppend(object, "handler", handler) < 0)
+        return -1;
+    handler = NULL;

     if (!(state = virJSONValueToString(object, true)))
-        goto cleanup;
+        return -1;

     VIR_DEBUG("Saving state %s", state);

-    if (virFileWriteStr(state_file,
-                        state, 0700) < 0) {
+    if (virFileWriteStr(state_file, state, 0700) < 0) {
         virReportSystemError(errno,
-                             _("Unable to save state file %s"),
-                             state_file);
-        goto cleanup;
+                             _("Unable to save state file %s"), state_file);
+        return -1;
     }

     if (execvp(argv[0], argv) < 0) {
         virReportSystemError(errno, "%s",
                              _("Unable to restart self"));
-        goto cleanup;
+        return -1;
     }

     abort(); /* This should be impossible to reach */

- cleanup:
-    VIR_FREE(magic);
-    VIR_FREE(state);
-    virJSONValueFree(object);
-    return -1;
+    return 0;
 }


-- 
2.29.2




More information about the libvir-list mailing list