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

[libvirt] [PATCH v2 14/35] util: socketaddr: use VIR_AUTOPTR for aggregate types



By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions 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 | 38 ++++++++++++++++----------------------
 1 file changed, 16 insertions(+), 22 deletions(-)

diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c
index eee725d..3e5ea64 100644
--- a/src/util/virsocketaddr.c
+++ b/src/util/virsocketaddr.c
@@ -1193,52 +1193,46 @@ virSocketAddrPTRDomain(const virSocketAddr *addr,
                        unsigned int prefix,
                        char **ptr)
 {
-    virBuffer buf = VIR_BUFFER_INITIALIZER;
     size_t i;
-    int ret = -1;
+    VIR_AUTOPTR(virBuffer) buf = NULL;
+
+    if (VIR_ALLOC(buf) < 0)
+        return -1;
 
     if (VIR_SOCKET_ADDR_IS_FAMILY(addr, AF_INET)) {
         virSocketAddrIPv4 ip;
 
         if (prefix == 0 || prefix >= 32 || prefix % 8 != 0)
-            goto unsupported;
+            return -2;
 
         if (virSocketAddrGetIPv4Addr(addr, &ip) < 0)
-            goto cleanup;
+            return -1;
 
         for (i = prefix / 8; i > 0; i--)
-            virBufferAsprintf(&buf, "%u.", ip[i - 1]);
+            virBufferAsprintf(buf, "%u.", ip[i - 1]);
 
-        virBufferAddLit(&buf, VIR_SOCKET_ADDR_IPV4_ARPA);
+        virBufferAddLit(buf, VIR_SOCKET_ADDR_IPV4_ARPA);
     } else if (VIR_SOCKET_ADDR_IS_FAMILY(addr, AF_INET6)) {
         virSocketAddrIPv6Nibbles ip;
 
         if (prefix == 0 || prefix >= 128 || prefix % 4 != 0)
-            goto unsupported;
+            return -2;
 
         if (virSocketAddrGetIPv6Nibbles(addr, &ip) < 0)
-            goto cleanup;
+            return -1;
 
         for (i = prefix / 4; i > 0; i--)
-            virBufferAsprintf(&buf, "%x.", ip[i - 1]);
+            virBufferAsprintf(buf, "%x.", ip[i - 1]);
 
-        virBufferAddLit(&buf, VIR_SOCKET_ADDR_IPV6_ARPA);
+        virBufferAddLit(buf, VIR_SOCKET_ADDR_IPV6_ARPA);
     } else {
-        goto unsupported;
+        return -2;
     }
 
-    if (!(*ptr = virBufferContentAndReset(&buf)))
-        goto cleanup;
+    if (!(*ptr = virBufferContentAndReset(buf)))
+        return -1;
 
-    ret = 0;
-
- cleanup:
-    virBufferFreeAndReset(&buf);
-    return ret;
-
- unsupported:
-    ret = -2;
-    goto cleanup;
+    return 0;
 }
 
 void
-- 
1.8.3.1


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