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

[libvirt] [PATCH v2 13/35] util: socketaddr: use VIR_AUTOFREE instead of VIR_FREE for scalar types



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 gmail com>
Reviewed-by: Erik Skultety <eskultet redhat com>
---
 src/util/virsocketaddr.c | 70 ++++++++++++++++++++----------------------------
 1 file changed, 29 insertions(+), 41 deletions(-)

diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c
index 6b52a3d..eee725d 100644
--- a/src/util/virsocketaddr.c
+++ b/src/util/virsocketaddr.c
@@ -432,10 +432,10 @@ virSocketAddrFormatFull(const virSocketAddr *addr,
         if (withService) {
             if (virAsprintf(&addrstr, VIR_LOOPBACK_IPV4_ADDR"%s0",
                             separator ? separator : ":") < 0)
-                goto error;
+                return NULL;
         } else {
             if (VIR_STRDUP(addrstr, VIR_LOOPBACK_IPV4_ADDR) < 0)
-                goto error;
+                return NULL;
         }
         return addrstr;
     }
@@ -452,33 +452,27 @@ virSocketAddrFormatFull(const virSocketAddr *addr,
     }
 
     if (withService) {
-        char *ipv6_host = NULL;
+        VIR_AUTOFREE(char *) ipv6_host = NULL;
         /* sasl_new_client demands the socket address to be in an odd format:
          * a.b.c.d;port or e:f:g:h:i:j:k:l;port, so use square brackets for
          * IPv6 only if no separator is passed to the function
          */
         if (!separator && VIR_SOCKET_ADDR_FAMILY(addr) == AF_INET6) {
             if (virAsprintf(&ipv6_host, "[%s]", host) < 0)
-                goto error;
+                return NULL;
         }
 
         if (virAsprintf(&addrstr, "%s%s%s",
                         ipv6_host ? ipv6_host : host,
                         separator ? separator : ":", port) == -1) {
-            VIR_FREE(ipv6_host);
-            goto error;
+            return NULL;
         }
-
-        VIR_FREE(ipv6_host);
     } else {
         if (VIR_STRDUP(addrstr, host) < 0)
-            goto error;
+            return NULL;
     }
 
     return addrstr;
-
- error:
-    return NULL;
 }
 
 
@@ -759,24 +753,26 @@ virSocketAddrGetRange(virSocketAddrPtr start, virSocketAddrPtr end,
     int ret = 0;
     size_t i;
     virSocketAddr netmask;
-    char *startStr = NULL, *endStr = NULL, *netStr = NULL;
+    VIR_AUTOFREE(char *) startStr = NULL;
+    VIR_AUTOFREE(char *) endStr = NULL;
+    VIR_AUTOFREE(char *) netStr = NULL;
 
     if (start == NULL || end == NULL) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("NULL argument - %p %p"), start, end);
-        goto error;
+        return -1;
     }
 
     startStr = virSocketAddrFormat(start);
     endStr = virSocketAddrFormat(end);
     if (!startStr || !endStr)
-        goto error; /*error already reported */
+        return -1; /*error already reported */
 
     if (VIR_SOCKET_ADDR_FAMILY(start) != VIR_SOCKET_ADDR_FAMILY(end)) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("mismatch of address family in range %s - %s"),
                        startStr, endStr);
-        goto error;
+        return -1;
     }
 
     if (network) {
@@ -784,14 +780,14 @@ virSocketAddrGetRange(virSocketAddrPtr start, virSocketAddrPtr end,
          * network the range should be within
          */
         if (!(netStr = virSocketAddrFormat(network)))
-            goto error;
+            return -1;
 
         if (VIR_SOCKET_ADDR_FAMILY(start) != VIR_SOCKET_ADDR_FAMILY(network)) {
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("mismatch of address family in "
                              "range %s - %s for network %s"),
                            startStr, endStr, netStr);
-            goto error;
+            return -1;
         }
 
         if (prefix < 0 ||
@@ -801,7 +797,7 @@ virSocketAddrGetRange(virSocketAddrPtr start, virSocketAddrPtr end,
                            _("bad prefix %d for network %s when "
                              " checking range %s - %s"),
                            prefix, netStr, startStr, endStr);
-            goto error;
+            return -1;
         }
 
         /* both start and end of range need to be within network */
@@ -811,7 +807,7 @@ virSocketAddrGetRange(virSocketAddrPtr start, virSocketAddrPtr end,
                            _("range %s - %s is not entirely within "
                              "network %s/%d"),
                            startStr, endStr, netStr, prefix);
-            goto error;
+            return -1;
         }
 
         if (VIR_SOCKET_ADDR_IS_FAMILY(start, AF_INET)) {
@@ -823,7 +819,7 @@ virSocketAddrGetRange(virSocketAddrPtr start, virSocketAddrPtr end,
                                _("failed to construct broadcast or network "
                                  "address for network %s/%d"),
                                netStr, prefix);
-                goto error;
+                return -1;
             }
 
             /* Don't allow the start of the range to be the network
@@ -838,7 +834,7 @@ virSocketAddrGetRange(virSocketAddrPtr start, virSocketAddrPtr end,
                                _("start of range %s - %s in network %s/%d "
                                  "is the network address"),
                                startStr, endStr, netStr, prefix);
-                goto error;
+                return -1;
             }
 
             if (virSocketAddrEqual(end, &broadcast)) {
@@ -846,7 +842,7 @@ virSocketAddrGetRange(virSocketAddrPtr start, virSocketAddrPtr end,
                                _("end of range %s - %s in network %s/%d "
                                  "is the broadcast address"),
                                startStr, endStr, netStr, prefix);
-                goto error;
+                return -1;
             }
         }
     }
@@ -860,7 +856,7 @@ virSocketAddrGetRange(virSocketAddrPtr start, virSocketAddrPtr end,
                            _("failed to get IPv4 address "
                              "for start or end of range %s - %s"),
                            startStr, endStr);
-            goto error;
+            return -1;
         }
 
         /* legacy check that everything except the last two bytes
@@ -868,10 +864,10 @@ virSocketAddrGetRange(virSocketAddrPtr start, virSocketAddrPtr end,
          */
         for (i = 0; i < 2; i++) {
             if (t1[i] != t2[i]) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("range %s - %s is too large (> 65535)"),
-                           startStr, endStr);
-            goto error;
+                virReportError(VIR_ERR_INTERNAL_ERROR,
+                               _("range %s - %s is too large (> 65535)"),
+                               startStr, endStr);
+                return -1;
             }
         }
         ret = (t2[2] - t1[2]) * 256 + (t2[3] - t1[3]);
@@ -879,7 +875,7 @@ virSocketAddrGetRange(virSocketAddrPtr start, virSocketAddrPtr end,
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("range %s - %s is reversed "),
                            startStr, endStr);
-            goto error;
+            return -1;
         }
         ret++;
     } else if (VIR_SOCKET_ADDR_IS_FAMILY(start, AF_INET6)) {
@@ -891,7 +887,7 @@ virSocketAddrGetRange(virSocketAddrPtr start, virSocketAddrPtr end,
                            _("failed to get IPv6 address "
                              "for start or end of range %s - %s"),
                            startStr, endStr);
-            goto error;
+            return -1;
         }
 
         /* legacy check that everything except the last two bytes are
@@ -902,7 +898,7 @@ virSocketAddrGetRange(virSocketAddrPtr start, virSocketAddrPtr end,
                 virReportError(VIR_ERR_INTERNAL_ERROR,
                                _("range %s - %s is too large (> 65535)"),
                                startStr, endStr);
-                goto error;
+                return -1;
             }
         }
         ret = t2[7] - t1[7];
@@ -910,7 +906,7 @@ virSocketAddrGetRange(virSocketAddrPtr start, virSocketAddrPtr end,
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("range %s - %s start larger than end"),
                            startStr, endStr);
-            goto error;
+            return -1;
         }
         ret++;
     } else {
@@ -918,18 +914,10 @@ virSocketAddrGetRange(virSocketAddrPtr start, virSocketAddrPtr end,
                        _("unsupported address family "
                          "for range %s - %s, must be ipv4 or ipv6"),
                        startStr, endStr);
-        goto error;
+        return -1;
     }
 
- cleanup:
-    VIR_FREE(startStr);
-    VIR_FREE(endStr);
-    VIR_FREE(netStr);
     return ret;
-
- error:
-    ret = -1;
-    goto cleanup;
 }
 
 
-- 
1.8.3.1


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