render / rpms / libvirt

Forked from rpms/libvirt 4 months ago
Clone
c313de
From 63fe889c0e3d5c9cf44ab030fb63baee3646cb9f Mon Sep 17 00:00:00 2001
c313de
Message-Id: <63fe889c0e3d5c9cf44ab030fb63baee3646cb9f@dist-git>
c313de
From: Sukrit Bhatnagar <skrtbhtngr@gmail.com>
c313de
Date: Tue, 30 Jul 2019 15:30:45 +0200
c313de
Subject: [PATCH] util: command: use VIR_AUTOFREE instead of VIR_FREE for
c313de
 scalar types
c313de
MIME-Version: 1.0
c313de
Content-Type: text/plain; charset=UTF-8
c313de
Content-Transfer-Encoding: 8bit
c313de
c313de
By making use of GNU C's cleanup attribute handled by the
c313de
VIR_AUTOFREE macro for declaring scalar variables, majority
c313de
of the VIR_FREE calls can be dropped, which in turn leads to
c313de
getting rid of most of our cleanup sections.
c313de
c313de
Signed-off-by: Sukrit Bhatnagar <skrtbhtngr@gmail.com>
c313de
Reviewed-by: Erik Skultety <eskultet@redhat.com>
c313de
(cherry picked from commit 46a1f0bb648dec58d6ce4b0eccbb59be435d9073)
c313de
c313de
Prerequisite of: https://bugzilla.redhat.com/show_bug.cgi?id=1721434
c313de
c313de
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
c313de
Message-Id: <f79e228ffb4335b228575e3b35adeb30848497bb.1564493409.git.mprivozn@redhat.com>
c313de
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c313de
---
c313de
 src/util/vircommand.c | 40 ++++++++++++----------------------------
c313de
 1 file changed, 12 insertions(+), 28 deletions(-)
c313de
c313de
diff --git a/src/util/vircommand.c b/src/util/vircommand.c
c313de
index 6dab105f56..c2b8a1a3c3 100644
c313de
--- a/src/util/vircommand.c
c313de
+++ b/src/util/vircommand.c
c313de
@@ -508,11 +508,11 @@ virExec(virCommandPtr cmd)
c313de
     int childout = -1;
c313de
     int childerr = -1;
c313de
     int tmpfd;
c313de
-    char *binarystr = NULL;
c313de
+    VIR_AUTOFREE(char *) binarystr = NULL;
c313de
     const char *binary = NULL;
c313de
     int ret;
c313de
     struct sigaction waxon, waxoff;
c313de
-    gid_t *groups = NULL;
c313de
+    VIR_AUTOFREE(gid_t *) groups = NULL;
c313de
     int ngroups;
c313de
 
c313de
     if (cmd->args[0][0] != '/') {
c313de
@@ -605,9 +605,6 @@ virExec(virCommandPtr cmd)
c313de
 
c313de
         cmd->pid = pid;
c313de
 
c313de
-        VIR_FREE(groups);
c313de
-        VIR_FREE(binarystr);
c313de
-
c313de
         return 0;
c313de
     }
c313de
 
c313de
@@ -797,9 +794,6 @@ virExec(virCommandPtr cmd)
c313de
     /* This is cleanup of parent process only - child
c313de
        should never jump here on error */
c313de
 
c313de
-    VIR_FREE(binarystr);
c313de
-    VIR_FREE(groups);
c313de
-
c313de
     /* NB we don't virReportError() on any failures here
c313de
        because the code which jumped here already raised
c313de
        an error condition which we must not overwrite */
c313de
@@ -2387,7 +2381,7 @@ int
c313de
 virCommandRunAsync(virCommandPtr cmd, pid_t *pid)
c313de
 {
c313de
     int ret = -1;
c313de
-    char *str = NULL;
c313de
+    VIR_AUTOFREE(char *) str = NULL;
c313de
     size_t i;
c313de
     bool synchronous = false;
c313de
     int infd[2] = {-1, -1};
c313de
@@ -2512,7 +2506,6 @@ virCommandRunAsync(virCommandPtr cmd, pid_t *pid)
c313de
         VIR_FORCE_CLOSE(cmd->infd);
c313de
         VIR_FORCE_CLOSE(cmd->inpipe);
c313de
     }
c313de
-    VIR_FREE(str);
c313de
     return ret;
c313de
 }
c313de
 
c313de
@@ -2589,8 +2582,8 @@ virCommandWait(virCommandPtr cmd, int *exitstatus)
c313de
         if (exitstatus && (cmd->rawStatus || WIFEXITED(status))) {
c313de
             *exitstatus = cmd->rawStatus ? status : WEXITSTATUS(status);
c313de
         } else if (status) {
c313de
-            char *str = virCommandToString(cmd);
c313de
-            char *st = virProcessTranslateStatus(status);
c313de
+            VIR_AUTOFREE(char *) str = virCommandToString(cmd);
c313de
+            VIR_AUTOFREE(char *) st = virProcessTranslateStatus(status);
c313de
             bool haveErrMsg = cmd->errbuf && *cmd->errbuf && (*cmd->errbuf)[0];
c313de
 
c313de
             virReportError(VIR_ERR_INTERNAL_ERROR,
c313de
@@ -2598,8 +2591,6 @@ virCommandWait(virCommandPtr cmd, int *exitstatus)
c313de
                            str ? str : cmd->args[0], NULLSTR(st),
c313de
                            haveErrMsg ? ": " : "",
c313de
                            haveErrMsg ? *cmd->errbuf : "");
c313de
-            VIR_FREE(str);
c313de
-            VIR_FREE(st);
c313de
             return -1;
c313de
         }
c313de
     }
c313de
@@ -2719,7 +2710,7 @@ int virCommandHandshakeWait(virCommandPtr cmd)
c313de
         return -1;
c313de
     }
c313de
     if (c != '1') {
c313de
-        char *msg;
c313de
+        VIR_AUTOFREE(char *) msg = NULL;
c313de
         ssize_t len;
c313de
         if (VIR_ALLOC_N(msg, 1024) < 0) {
c313de
             VIR_FORCE_CLOSE(cmd->handshakeWait[0]);
c313de
@@ -2732,7 +2723,6 @@ int virCommandHandshakeWait(virCommandPtr cmd)
c313de
 
c313de
         if ((len = saferead(cmd->handshakeWait[0], msg, 1024)) < 0) {
c313de
             VIR_FORCE_CLOSE(cmd->handshakeWait[0]);
c313de
-            VIR_FREE(msg);
c313de
             virReportSystemError(errno, "%s",
c313de
                                  _("No error message from child failure"));
c313de
             return -1;
c313de
@@ -2740,7 +2730,6 @@ int virCommandHandshakeWait(virCommandPtr cmd)
c313de
         VIR_FORCE_CLOSE(cmd->handshakeWait[0]);
c313de
         msg[len-1] = '\0';
c313de
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s", msg);
c313de
-        VIR_FREE(msg);
c313de
         return -1;
c313de
     }
c313de
     VIR_FORCE_CLOSE(cmd->handshakeWait[0]);
c313de
@@ -2854,8 +2843,8 @@ virCommandFree(virCommandPtr cmd)
c313de
  * This requests asynchronous string IO on @cmd. It is useful in
c313de
  * combination with virCommandRunAsync():
c313de
  *
c313de
- *      virCommandPtr cmd = virCommandNew*(...);
c313de
- *      char *buf = NULL;
c313de
+ *      VIR_AUTOPTR(virCommand) cmd = virCommandNew*(...);
c313de
+ *      VIR_AUTOFREE(char *) buf = NULL;
c313de
  *
c313de
  *      ...
c313de
  *
c313de
@@ -2863,21 +2852,18 @@ virCommandFree(virCommandPtr cmd)
c313de
  *      virCommandDoAsyncIO(cmd);
c313de
  *
c313de
  *      if (virCommandRunAsync(cmd, NULL) < 0)
c313de
- *          goto cleanup;
c313de
+ *          return;
c313de
  *
c313de
  *      ...
c313de
  *
c313de
  *      if (virCommandWait(cmd, NULL) < 0)
c313de
- *          goto cleanup;
c313de
+ *          return;
c313de
  *
c313de
  *      // @buf now contains @cmd's stdout
c313de
  *      VIR_DEBUG("STDOUT: %s", NULLSTR(buf));
c313de
  *
c313de
  *      ...
c313de
  *
c313de
- *  cleanup:
c313de
- *      VIR_FREE(buf);
c313de
- *      virCommandFree(cmd);
c313de
  *
c313de
  * The libvirt's event loop is used for handling stdios of @cmd.
c313de
  * Since current implementation uses strlen to determine length
c313de
@@ -2970,11 +2956,11 @@ virCommandRunRegex(virCommandPtr cmd,
c313de
 {
c313de
     int err;
c313de
     regex_t *reg;
c313de
-    regmatch_t *vars = NULL;
c313de
+    VIR_AUTOFREE(regmatch_t *) vars = NULL;
c313de
     size_t i, j, k;
c313de
     int totgroups = 0, ngroup = 0, maxvars = 0;
c313de
     char **groups;
c313de
-    char *outbuf = NULL;
c313de
+    VIR_AUTOFREE(char *) outbuf = NULL;
c313de
     char **lines = NULL;
c313de
     int ret = -1;
c313de
 
c313de
@@ -3055,13 +3041,11 @@ virCommandRunRegex(virCommandPtr cmd,
c313de
     ret = 0;
c313de
  cleanup:
c313de
     virStringListFree(lines);
c313de
-    VIR_FREE(outbuf);
c313de
     if (groups) {
c313de
         for (j = 0; j < totgroups; j++)
c313de
             VIR_FREE(groups[j]);
c313de
         VIR_FREE(groups);
c313de
     }
c313de
-    VIR_FREE(vars);
c313de
 
c313de
     for (i = 0; i < nregex; i++)
c313de
         regfree(&reg[i]);
c313de
-- 
c313de
2.22.0
c313de