[PATCH v3 10/35] util: command: use VIR_AUTOFREE instead of VIR_FREE for scalar types

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar <skrtbhtngr@xxxxxxxxx>
---
 src/util/vircommand.c | 40 ++++++++++++----------------------------
 1 file changed, 12 insertions(+), 28 deletions(-)

diff --git a/src/util/vircommand.c b/src/util/vircommand.c
index 8681e7b..d328431 100644
--- a/src/util/vircommand.c
+++ b/src/util/vircommand.c
@@ -507,11 +507,11 @@ virExec(virCommandPtr cmd)
     int childout = -1;
     int childerr = -1;
     int tmpfd;
-    char *binarystr = NULL;
+    VIR_AUTOFREE(char *) binarystr = NULL;
     const char *binary = NULL;
     int ret;
     struct sigaction waxon, waxoff;
-    gid_t *groups = NULL;
+    VIR_AUTOFREE(gid_t *) groups = NULL;
     int ngroups;
 
     if (cmd->args[0][0] != '/') {
@@ -604,9 +604,6 @@ virExec(virCommandPtr cmd)
 
         cmd->pid = pid;
 
-        VIR_FREE(groups);
-        VIR_FREE(binarystr);
-
         return 0;
     }
 
@@ -796,9 +793,6 @@ virExec(virCommandPtr cmd)
     /* This is cleanup of parent process only - child
        should never jump here on error */
 
-    VIR_FREE(binarystr);
-    VIR_FREE(groups);
-
     /* NB we don't virReportError() on any failures here
        because the code which jumped here already raised
        an error condition which we must not overwrite */
@@ -2386,7 +2380,7 @@ int
 virCommandRunAsync(virCommandPtr cmd, pid_t *pid)
 {
     int ret = -1;
-    char *str = NULL;
+    VIR_AUTOFREE(char *) str = NULL;
     size_t i;
     bool synchronous = false;
     int infd[2] = {-1, -1};
@@ -2511,7 +2505,6 @@ virCommandRunAsync(virCommandPtr cmd, pid_t *pid)
         VIR_FORCE_CLOSE(cmd->infd);
         VIR_FORCE_CLOSE(cmd->inpipe);
     }
-    VIR_FREE(str);
     return ret;
 }
 
@@ -2588,8 +2581,8 @@ virCommandWait(virCommandPtr cmd, int *exitstatus)
         if (exitstatus && (cmd->rawStatus || WIFEXITED(status))) {
             *exitstatus = cmd->rawStatus ? status : WEXITSTATUS(status);
         } else if (status) {
-            char *str = virCommandToString(cmd);
-            char *st = virProcessTranslateStatus(status);
+            VIR_AUTOFREE(char *) str = virCommandToString(cmd);
+            VIR_AUTOFREE(char *) st = virProcessTranslateStatus(status);
             bool haveErrMsg = cmd->errbuf && *cmd->errbuf && (*cmd->errbuf)[0];
 
             virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -2597,8 +2590,6 @@ virCommandWait(virCommandPtr cmd, int *exitstatus)
                            str ? str : cmd->args[0], NULLSTR(st),
                            haveErrMsg ? ": " : "",
                            haveErrMsg ? *cmd->errbuf : "");
-            VIR_FREE(str);
-            VIR_FREE(st);
             return -1;
         }
     }
@@ -2718,7 +2709,7 @@ int virCommandHandshakeWait(virCommandPtr cmd)
         return -1;
     }
     if (c != '1') {
-        char *msg;
+        VIR_AUTOFREE(char *) msg = NULL;
         ssize_t len;
         if (VIR_ALLOC_N(msg, 1024) < 0) {
             VIR_FORCE_CLOSE(cmd->handshakeWait[0]);
@@ -2731,7 +2722,6 @@ int virCommandHandshakeWait(virCommandPtr cmd)
 
         if ((len = saferead(cmd->handshakeWait[0], msg, 1024)) < 0) {
             VIR_FORCE_CLOSE(cmd->handshakeWait[0]);
-            VIR_FREE(msg);
             virReportSystemError(errno, "%s",
                                  _("No error message from child failure"));
             return -1;
@@ -2739,7 +2729,6 @@ int virCommandHandshakeWait(virCommandPtr cmd)
         VIR_FORCE_CLOSE(cmd->handshakeWait[0]);
         msg[len-1] = '\0';
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s", msg);
-        VIR_FREE(msg);
         return -1;
     }
     VIR_FORCE_CLOSE(cmd->handshakeWait[0]);
@@ -2853,8 +2842,8 @@ virCommandFree(virCommandPtr cmd)
  * This requests asynchronous string IO on @cmd. It is useful in
  * combination with virCommandRunAsync():
  *
- *      virCommandPtr cmd = virCommandNew*(...);
- *      char *buf = NULL;
+ *      VIR_AUTOPTR(virCommand) cmd = virCommandNew*(...);
+ *      VIR_AUTOFREE(char *) buf = NULL;
  *
  *      ...
  *
@@ -2862,21 +2851,18 @@ virCommandFree(virCommandPtr cmd)
  *      virCommandDoAsyncIO(cmd);
  *
  *      if (virCommandRunAsync(cmd, NULL) < 0)
- *          goto cleanup;
+ *          return;
  *
  *      ...
  *
  *      if (virCommandWait(cmd, NULL) < 0)
- *          goto cleanup;
+ *          return;
  *
  *      // @buf now contains @cmd's stdout
  *      VIR_DEBUG("STDOUT: %s", NULLSTR(buf));
  *
  *      ...
  *
- *  cleanup:
- *      VIR_FREE(buf);
- *      virCommandFree(cmd);
  *
  * The libvirt's event loop is used for handling stdios of @cmd.
  * Since current implementation uses strlen to determine length
@@ -2969,11 +2955,11 @@ virCommandRunRegex(virCommandPtr cmd,
 {
     int err;
     regex_t *reg;
-    regmatch_t *vars = NULL;
+    VIR_AUTOFREE(regmatch_t *) vars = NULL;
     size_t i, j, k;
     int totgroups = 0, ngroup = 0, maxvars = 0;
     char **groups;
-    char *outbuf = NULL;
+    VIR_AUTOFREE(char *) outbuf = NULL;
     char **lines = NULL;
     int ret = -1;
 
@@ -3054,13 +3040,11 @@ virCommandRunRegex(virCommandPtr cmd,
     ret = 0;
  cleanup:
     virStringListFree(lines);
-    VIR_FREE(outbuf);
     if (groups) {
         for (j = 0; j < totgroups; j++)
             VIR_FREE(groups[j]);
         VIR_FREE(groups);
     }
-    VIR_FREE(vars);
 
     for (i = 0; i < nregex; i++)
         regfree(&reg[i]);
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux