[libvirt] [PATCH v1 10/18] use VIR_AUTOFREE in src/util/virdnsmasq.c

Sukrit Bhatnagar skrtbhtngr at gmail.com
Sun Jun 3 08:12:08 UTC 2018


Modify code to use VIR_AUTOFREE macro wherever required.

Signed-off-by: Sukrit Bhatnagar <skrtbhtngr at gmail.com>
---
 src/util/virdnsmasq.c | 116 +++++++++++++++++---------------------------------
 1 file changed, 39 insertions(+), 77 deletions(-)

diff --git a/src/util/virdnsmasq.c b/src/util/virdnsmasq.c
index 492dcad..c806cd2 100644
--- a/src/util/virdnsmasq.c
+++ b/src/util/virdnsmasq.c
@@ -95,7 +95,7 @@ addnhostsAdd(dnsmasqAddnHostsfile *addnhostsfile,
              virSocketAddr *ip,
              const char *name)
 {
-    char *ipstr = NULL;
+    VIR_AUTOFREE(char *) ipstr = NULL;
     int idx = -1;
     size_t i;
 
@@ -111,35 +111,29 @@ addnhostsAdd(dnsmasqAddnHostsfile *addnhostsfile,
 
     if (idx < 0) {
         if (VIR_REALLOC_N(addnhostsfile->hosts, addnhostsfile->nhosts + 1) < 0)
-            goto error;
+            return -1;
 
         idx = addnhostsfile->nhosts;
         if (VIR_ALLOC(addnhostsfile->hosts[idx].hostnames) < 0)
-            goto error;
+            return -1;
 
         if (VIR_STRDUP(addnhostsfile->hosts[idx].ip, ipstr) < 0)
-            goto error;
+            return -1;
 
         addnhostsfile->hosts[idx].nhostnames = 0;
         addnhostsfile->nhosts++;
     }
 
     if (VIR_REALLOC_N(addnhostsfile->hosts[idx].hostnames, addnhostsfile->hosts[idx].nhostnames + 1) < 0)
-        goto error;
+        return -1;
 
     if (VIR_STRDUP(addnhostsfile->hosts[idx].hostnames[addnhostsfile->hosts[idx].nhostnames],
                    name) < 0)
-        goto error;
-
-    VIR_FREE(ipstr);
+        return -1;
 
     addnhostsfile->hosts[idx].nhostnames++;
 
     return 0;
-
- error:
-    VIR_FREE(ipstr);
-    return -1;
 }
 
 static dnsmasqAddnHostsfile *
@@ -178,11 +172,10 @@ addnhostsWrite(const char *path,
                dnsmasqAddnHost *hosts,
                unsigned int nhosts)
 {
-    char *tmp;
+    VIR_AUTOFREE(char *) tmp = NULL;
     FILE *f;
     bool istmp = true;
     size_t i, j;
-    int rc = 0;
 
     /* even if there are 0 hosts, create a 0 length file, to allow
      * for runtime addition.
@@ -193,61 +186,50 @@ addnhostsWrite(const char *path,
 
     if (!(f = fopen(tmp, "w"))) {
         istmp = false;
-        if (!(f = fopen(path, "w"))) {
-            rc = -errno;
-            goto cleanup;
-        }
+        if (!(f = fopen(path, "w")))
+            return -errno;
     }
 
     for (i = 0; i < nhosts; i++) {
         if (fputs(hosts[i].ip, f) == EOF || fputc('\t', f) == EOF) {
-            rc = -errno;
             VIR_FORCE_FCLOSE(f);
 
             if (istmp)
                 unlink(tmp);
 
-            goto cleanup;
+            return -errno;
         }
 
         for (j = 0; j < hosts[i].nhostnames; j++) {
             if (fputs(hosts[i].hostnames[j], f) == EOF || fputc('\t', f) == EOF) {
-                rc = -errno;
                 VIR_FORCE_FCLOSE(f);
 
                 if (istmp)
                     unlink(tmp);
 
-                goto cleanup;
+                return -errno;
             }
         }
 
         if (fputc('\n', f) == EOF) {
-            rc = -errno;
             VIR_FORCE_FCLOSE(f);
 
             if (istmp)
                 unlink(tmp);
 
-            goto cleanup;
+            return -errno;
         }
     }
 
-    if (VIR_FCLOSE(f) == EOF) {
-        rc = -errno;
-        goto cleanup;
-    }
+    if (VIR_FCLOSE(f) == EOF)
+        return -errno;
 
     if (istmp && rename(tmp, path) < 0) {
-        rc = -errno;
         unlink(tmp);
-        goto cleanup;
+        return -errno;
     }
 
- cleanup:
-    VIR_FREE(tmp);
-
-    return rc;
+    return 0;
 }
 
 static int
@@ -310,9 +292,10 @@ hostsfileAdd(dnsmasqHostsfile *hostsfile,
              const char *id,
              bool ipv6)
 {
-    char *ipstr = NULL;
+    VIR_AUTOFREE(char *) ipstr = NULL;
+
     if (VIR_REALLOC_N(hostsfile->hosts, hostsfile->nhosts + 1) < 0)
-        goto error;
+        return -1;
 
     if (!(ipstr = virSocketAddrFormat(ip)))
         return -1;
@@ -322,38 +305,33 @@ hostsfileAdd(dnsmasqHostsfile *hostsfile,
         if (name && id) {
             if (virAsprintf(&hostsfile->hosts[hostsfile->nhosts].host,
                             "id:%s,%s,[%s]", id, name, ipstr) < 0)
-                goto error;
+                return -1;
         } else if (name && !id) {
             if (virAsprintf(&hostsfile->hosts[hostsfile->nhosts].host,
                             "%s,[%s]", name, ipstr) < 0)
-                goto error;
+                return -1;
         } else if (!name && id) {
             if (virAsprintf(&hostsfile->hosts[hostsfile->nhosts].host,
                             "id:%s,[%s]", id, ipstr) < 0)
-                goto error;
+                return -1;
         }
     } else if (name && mac) {
         if (virAsprintf(&hostsfile->hosts[hostsfile->nhosts].host, "%s,%s,%s",
                         mac, ipstr, name) < 0)
-            goto error;
+            return -1;
     } else if (name && !mac) {
         if (virAsprintf(&hostsfile->hosts[hostsfile->nhosts].host, "%s,%s",
                         name, ipstr) < 0)
-            goto error;
+            return -1;
     } else {
         if (virAsprintf(&hostsfile->hosts[hostsfile->nhosts].host, "%s,%s",
                         mac, ipstr) < 0)
-            goto error;
+            return -1;
     }
-    VIR_FREE(ipstr);
 
     hostsfile->nhosts++;
 
     return 0;
-
- error:
-    VIR_FREE(ipstr);
-    return -1;
 }
 
 static dnsmasqHostsfile *
@@ -391,11 +369,10 @@ hostsfileWrite(const char *path,
                dnsmasqDhcpHost *hosts,
                unsigned int nhosts)
 {
-    char *tmp;
+    VIR_AUTOFREE(char *) tmp = NULL;
     FILE *f;
     bool istmp = true;
     size_t i;
-    int rc = 0;
 
     /* even if there are 0 hosts, create a 0 length file, to allow
      * for runtime addition.
@@ -406,39 +383,30 @@ hostsfileWrite(const char *path,
 
     if (!(f = fopen(tmp, "w"))) {
         istmp = false;
-        if (!(f = fopen(path, "w"))) {
-            rc = -errno;
-            goto cleanup;
-        }
+        if (!(f = fopen(path, "w")))
+            return -errno;
     }
 
     for (i = 0; i < nhosts; i++) {
         if (fputs(hosts[i].host, f) == EOF || fputc('\n', f) == EOF) {
-            rc = -errno;
             VIR_FORCE_FCLOSE(f);
 
             if (istmp)
                 unlink(tmp);
 
-            goto cleanup;
+            return -errno;
         }
     }
 
-    if (VIR_FCLOSE(f) == EOF) {
-        rc = -errno;
-        goto cleanup;
-    }
+    if (VIR_FCLOSE(f) == EOF)
+        return -errno;
 
     if (istmp && rename(tmp, path) < 0) {
-        rc = -errno;
         unlink(tmp);
-        goto cleanup;
+        return -errno;
     }
 
- cleanup:
-    VIR_FREE(tmp);
-
-    return rc;
+    return 0;
 }
 
 static int
@@ -708,17 +676,12 @@ dnsmasqCapsSetFromBuffer(dnsmasqCapsPtr caps, const char *buf)
 static int
 dnsmasqCapsSetFromFile(dnsmasqCapsPtr caps, const char *path)
 {
-    int ret = -1;
-    char *buf = NULL;
+    VIR_AUTOFREE(char *) buf = NULL;
 
     if (virFileReadAll(path, 1024 * 1024, &buf) < 0)
-        goto cleanup;
-
-    ret = dnsmasqCapsSetFromBuffer(caps, buf);
+        return -1;
 
- cleanup:
-    VIR_FREE(buf);
-    return ret;
+    return dnsmasqCapsSetFromBuffer(caps, buf);
 }
 
 static int
@@ -727,7 +690,9 @@ dnsmasqCapsRefreshInternal(dnsmasqCapsPtr caps, bool force)
     int ret = -1;
     struct stat sb;
     virCommandPtr cmd = NULL;
-    char *help = NULL, *version = NULL, *complete = NULL;
+    VIR_AUTOFREE(char *) help = NULL;
+    VIR_AUTOFREE(char *) version = NULL;
+    VIR_AUTOFREE(char *) complete = NULL;
 
     if (!caps || caps->noRefresh)
         return 0;
@@ -779,9 +744,6 @@ dnsmasqCapsRefreshInternal(dnsmasqCapsPtr caps, bool force)
 
  cleanup:
     virCommandFree(cmd);
-    VIR_FREE(help);
-    VIR_FREE(version);
-    VIR_FREE(complete);
     return ret;
 }
 
-- 
1.8.3.1




More information about the libvir-list mailing list