]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: netlink: use VIR_AUTOPTR for aggregate types
authorSukrit Bhatnagar <skrtbhtngr@gmail.com>
Thu, 9 Aug 2018 04:12:12 +0000 (09:42 +0530)
committerErik Skultety <eskultet@redhat.com>
Mon, 13 Aug 2018 18:03:56 +0000 (20:03 +0200)
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/virnetlink.c

index cb8072ff94e9431c5d78b7e9633a3ed6bf07cc47..8f064465a90a9c940360e13ea97c2c84a4958a99 100644 (file)
@@ -403,7 +403,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
         .ifi_index  = ifindex
     };
     unsigned int recvbuflen;
-    struct nl_msg *nl_msg;
+    VIR_AUTOPTR(virNetlinkMsg) nl_msg = NULL;
     VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 
     if (ifname && ifindex <= 0 && virNetDevGetIndex(ifname, &ifindex) < 0)
@@ -442,7 +442,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
 
     if (virNetlinkCommand(nl_msg, &resp, &recvbuflen,
                           src_pid, dst_pid, NETLINK_ROUTE, 0) < 0)
-        goto cleanup;
+        return -1;
 
     if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
         goto malformed_resp;
@@ -457,7 +457,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
             virReportSystemError(-err->error,
                                  _("error dumping %s (%d) interface"),
                                  ifname, ifindex);
-            goto cleanup;
+            return -1;
         }
         break;
 
@@ -474,21 +474,17 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
     }
 
     VIR_STEAL_PTR(*nlData, resp);
-    rc = 0;
-
- cleanup:
-    nlmsg_free(nl_msg);
-    return rc;
+    return 0;
 
  malformed_resp:
     virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("malformed netlink response message"));
-    goto cleanup;
+    return rc;
 
  buffer_too_small:
     virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("allocated netlink buffer is too small"));
-    goto cleanup;
+    return rc;
 }
 
 
@@ -510,11 +506,10 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
 int
 virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
 {
-    int rc = -1;
     struct nlmsgerr *err;
     struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
     unsigned int recvbuflen;
-    struct nl_msg *nl_msg;
+    VIR_AUTOPTR(virNetlinkMsg) nl_msg = NULL;
     VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 
     nl_msg = nlmsg_alloc_simple(RTM_DELLINK,
@@ -532,7 +527,7 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
 
     if (virNetlinkCommand(nl_msg, &resp, &recvbuflen, 0, 0,
                           NETLINK_ROUTE, 0) < 0) {
-        goto cleanup;
+        return -1;
     }
 
     if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
@@ -544,15 +539,14 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
         if (resp->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))
             goto malformed_resp;
 
-        if (-err->error == EOPNOTSUPP && fallback) {
-            rc = fallback(ifname);
-            goto cleanup;
-        }
+        if (-err->error == EOPNOTSUPP && fallback)
+            return fallback(ifname);
+
         if (err->error) {
             virReportSystemError(-err->error,
                                  _("error destroying network device %s"),
                                  ifname);
-            goto cleanup;
+            return -1;
         }
         break;
 
@@ -563,20 +557,17 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
         goto malformed_resp;
     }
 
-    rc = 0;
- cleanup:
-    nlmsg_free(nl_msg);
-    return rc;
+    return 0;
 
  malformed_resp:
     virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("malformed netlink response message"));
-    goto cleanup;
+    return -1;
 
  buffer_too_small:
     virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("allocated netlink buffer is too small"));
-    goto cleanup;
+    return -1;
 }
 
 /**
@@ -597,13 +588,12 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
 int
 virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
 {
-    int rc = -1;
     struct nlmsgerr *err;
     struct ndmsg ndinfo = {
         .ndm_family = AF_UNSPEC,
     };
     unsigned int recvbuflen;
-    struct nl_msg *nl_msg;
+    VIR_AUTOPTR(virNetlinkMsg) nl_msg = NULL;
     VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 
     nl_msg = nlmsg_alloc_simple(RTM_GETNEIGH, NLM_F_DUMP | NLM_F_REQUEST);
@@ -618,7 +608,7 @@ virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
 
     if (virNetlinkCommand(nl_msg, &resp, &recvbuflen,
                           src_pid, dst_pid, NETLINK_ROUTE, 0) < 0)
-        goto cleanup;
+        return -1;
 
     if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
         goto malformed_resp;
@@ -632,7 +622,7 @@ virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
         if (err->error) {
             virReportSystemError(-err->error,
                                  "%s", _("error dumping"));
-            goto cleanup;
+            return -1;
         }
         break;
 
@@ -644,21 +634,17 @@ virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
     }
 
     VIR_STEAL_PTR(*nlData, resp);
-    rc = recvbuflen;
-
- cleanup:
-    nlmsg_free(nl_msg);
-    return rc;
+    return recvbuflen;
 
  malformed_resp:
     virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("malformed netlink response message"));
-    goto cleanup;
+    return -1;
 
  buffer_too_small:
     virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("allocated netlink buffer is too small"));
-    goto cleanup;
+    return -1;
 }
 
 int