c480ed
From d808105a19611b7561c7ab0e4b128fbabebc88e2 Mon Sep 17 00:00:00 2001
c480ed
Message-Id: <d808105a19611b7561c7ab0e4b128fbabebc88e2@dist-git>
c480ed
From: Sukrit Bhatnagar <skrtbhtngr@gmail.com>
c480ed
Date: Tue, 30 Jul 2019 15:30:49 +0200
c480ed
Subject: [PATCH] util: netdevopenvswitch: use VIR_AUTOPTR for aggregate types
c480ed
MIME-Version: 1.0
c480ed
Content-Type: text/plain; charset=UTF-8
c480ed
Content-Transfer-Encoding: 8bit
c480ed
c480ed
By making use of GNU C's cleanup attribute handled by the
c480ed
VIR_AUTOPTR macro for declaring aggregate pointer variables,
c480ed
majority of the calls to *Free functions can be dropped, which
c480ed
in turn leads to getting rid of most of our cleanup sections.
c480ed
c480ed
Signed-off-by: Sukrit Bhatnagar <skrtbhtngr@gmail.com>
c480ed
Reviewed-by: Erik Skultety <eskultet@redhat.com>
c480ed
(cherry picked from commit 1077b46de6730eea76884b921bed1ece65be26ef)
c480ed
c480ed
Prerequisite of: https://bugzilla.redhat.com/show_bug.cgi?id=1721434
c480ed
c480ed
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
c480ed
Message-Id: <1b5f75ad2258bb10ea75fe646271e9625d49f998.1564493409.git.mprivozn@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
---
c480ed
 src/util/virnetdevopenvswitch.c | 80 +++++++++++----------------------
c480ed
 1 file changed, 27 insertions(+), 53 deletions(-)
c480ed
c480ed
diff --git a/src/util/virnetdevopenvswitch.c b/src/util/virnetdevopenvswitch.c
c480ed
index 0dcd49d40f..2f5c7ac789 100644
c480ed
--- a/src/util/virnetdevopenvswitch.c
c480ed
+++ b/src/util/virnetdevopenvswitch.c
c480ed
@@ -144,11 +144,10 @@ int virNetDevOpenvswitchAddPort(const char *brname, const char *ifname,
c480ed
                                    virNetDevVPortProfilePtr ovsport,
c480ed
                                    virNetDevVlanPtr virtVlan)
c480ed
 {
c480ed
-    int ret = -1;
c480ed
-    virCommandPtr cmd = NULL;
c480ed
     char macaddrstr[VIR_MAC_STRING_BUFLEN];
c480ed
     char ifuuidstr[VIR_UUID_STRING_BUFLEN];
c480ed
     char vmuuidstr[VIR_UUID_STRING_BUFLEN];
c480ed
+    VIR_AUTOPTR(virCommand) cmd = NULL;
c480ed
     VIR_AUTOFREE(char *) attachedmac_ex_id = NULL;
c480ed
     VIR_AUTOFREE(char *) ifaceid_ex_id = NULL;
c480ed
     VIR_AUTOFREE(char *) profile_ex_id = NULL;
c480ed
@@ -160,17 +159,17 @@ int virNetDevOpenvswitchAddPort(const char *brname, const char *ifname,
c480ed
 
c480ed
     if (virAsprintf(&attachedmac_ex_id, "external-ids:attached-mac=\"%s\"",
c480ed
                     macaddrstr) < 0)
c480ed
-        goto cleanup;
c480ed
+        return -1;
c480ed
     if (virAsprintf(&ifaceid_ex_id, "external-ids:iface-id=\"%s\"",
c480ed
                     ifuuidstr) < 0)
c480ed
-        goto cleanup;
c480ed
+        return -1;
c480ed
     if (virAsprintf(&vmid_ex_id, "external-ids:vm-id=\"%s\"",
c480ed
                     vmuuidstr) < 0)
c480ed
-        goto cleanup;
c480ed
+        return -1;
c480ed
     if (ovsport->profileID[0] != '\0') {
c480ed
         if (virAsprintf(&profile_ex_id, "external-ids:port-profile=\"%s\"",
c480ed
                         ovsport->profileID) < 0)
c480ed
-            goto cleanup;
c480ed
+            return -1;
c480ed
     }
c480ed
 
c480ed
     cmd = virCommandNew(OVSVSCTL);
c480ed
@@ -179,7 +178,7 @@ int virNetDevOpenvswitchAddPort(const char *brname, const char *ifname,
c480ed
                          ifname, "--", "add-port", brname, ifname, NULL);
c480ed
 
c480ed
     if (virNetDevOpenvswitchConstructVlans(cmd, virtVlan) < 0)
c480ed
-        goto cleanup;
c480ed
+        return -1;
c480ed
 
c480ed
     if (ovsport->profileID[0] == '\0') {
c480ed
         virCommandAddArgList(cmd,
c480ed
@@ -204,13 +203,10 @@ int virNetDevOpenvswitchAddPort(const char *brname, const char *ifname,
c480ed
         virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                        _("Unable to add port %s to OVS bridge %s"),
c480ed
                        ifname, brname);
c480ed
-        goto cleanup;
c480ed
+        return -1;
c480ed
     }
c480ed
 
c480ed
-    ret = 0;
c480ed
- cleanup:
c480ed
-    virCommandFree(cmd);
c480ed
-    return ret;
c480ed
+    return 0;
c480ed
 }
c480ed
 
c480ed
 /**
c480ed
@@ -223,8 +219,7 @@ int virNetDevOpenvswitchAddPort(const char *brname, const char *ifname,
c480ed
  */
c480ed
 int virNetDevOpenvswitchRemovePort(const char *brname ATTRIBUTE_UNUSED, const char *ifname)
c480ed
 {
c480ed
-    int ret = -1;
c480ed
-    virCommandPtr cmd = NULL;
c480ed
+    VIR_AUTOPTR(virCommand) cmd = NULL;
c480ed
 
c480ed
     cmd = virCommandNew(OVSVSCTL);
c480ed
     virNetDevOpenvswitchAddTimeout(cmd);
c480ed
@@ -233,13 +228,10 @@ int virNetDevOpenvswitchRemovePort(const char *brname ATTRIBUTE_UNUSED, const ch
c480ed
     if (virCommandRun(cmd, NULL) < 0) {
c480ed
         virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                        _("Unable to delete port %s from OVS"), ifname);
c480ed
-        goto cleanup;
c480ed
+        return -1;
c480ed
     }
c480ed
 
c480ed
-    ret = 0;
c480ed
- cleanup:
c480ed
-    virCommandFree(cmd);
c480ed
-    return ret;
c480ed
+    return 0;
c480ed
 }
c480ed
 
c480ed
 /**
c480ed
@@ -253,9 +245,8 @@ int virNetDevOpenvswitchRemovePort(const char *brname ATTRIBUTE_UNUSED, const ch
c480ed
  */
c480ed
 int virNetDevOpenvswitchGetMigrateData(char **migrate, const char *ifname)
c480ed
 {
c480ed
-    virCommandPtr cmd = NULL;
c480ed
     size_t len;
c480ed
-    int ret = -1;
c480ed
+    VIR_AUTOPTR(virCommand) cmd = NULL;
c480ed
 
c480ed
     cmd = virCommandNew(OVSVSCTL);
c480ed
     virNetDevOpenvswitchAddTimeout(cmd);
c480ed
@@ -269,7 +260,7 @@ int virNetDevOpenvswitchGetMigrateData(char **migrate, const char *ifname)
c480ed
         virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                        _("Unable to run command to get OVS port data for "
c480ed
                          "interface %s"), ifname);
c480ed
-        goto cleanup;
c480ed
+        return -1;
c480ed
     }
c480ed
 
c480ed
     /* Wipeout the newline, if it exists */
c480ed
@@ -277,10 +268,7 @@ int virNetDevOpenvswitchGetMigrateData(char **migrate, const char *ifname)
c480ed
     if (len > 0)
c480ed
         (*migrate)[len - 1] = '\0';
c480ed
 
c480ed
-    ret = 0;
c480ed
- cleanup:
c480ed
-    virCommandFree(cmd);
c480ed
-    return ret;
c480ed
+    return 0;
c480ed
 }
c480ed
 
c480ed
 /**
c480ed
@@ -294,8 +282,7 @@ int virNetDevOpenvswitchGetMigrateData(char **migrate, const char *ifname)
c480ed
  */
c480ed
 int virNetDevOpenvswitchSetMigrateData(char *migrate, const char *ifname)
c480ed
 {
c480ed
-    virCommandPtr cmd = NULL;
c480ed
-    int ret = -1;
c480ed
+    VIR_AUTOPTR(virCommand) cmd = NULL;
c480ed
 
c480ed
     if (!migrate) {
c480ed
         VIR_DEBUG("No OVS port data for interface %s", ifname);
c480ed
@@ -312,13 +299,10 @@ int virNetDevOpenvswitchSetMigrateData(char *migrate, const char *ifname)
c480ed
         virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                        _("Unable to run command to set OVS port data for "
c480ed
                          "interface %s"), ifname);
c480ed
-        goto cleanup;
c480ed
+        return -1;
c480ed
     }
c480ed
 
c480ed
-    ret = 0;
c480ed
- cleanup:
c480ed
-    virCommandFree(cmd);
c480ed
-    return ret;
c480ed
+    return 0;
c480ed
 }
c480ed
 
c480ed
 /**
c480ed
@@ -334,10 +318,9 @@ int
c480ed
 virNetDevOpenvswitchInterfaceStats(const char *ifname,
c480ed
                                    virDomainInterfaceStatsPtr stats)
c480ed
 {
c480ed
-    virCommandPtr cmd = NULL;
c480ed
     char *tmp;
c480ed
     bool gotStats = false;
c480ed
-    int ret = -1;
c480ed
+    VIR_AUTOPTR(virCommand) cmd = NULL;
c480ed
     VIR_AUTOFREE(char *) output = NULL;
c480ed
 
c480ed
     /* Just ensure the interface exists in ovs */
c480ed
@@ -350,7 +333,7 @@ virNetDevOpenvswitchInterfaceStats(const char *ifname,
c480ed
         /* no ovs-vsctl or interface 'ifname' doesn't exists in ovs */
c480ed
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c480ed
                        _("Interface not found"));
c480ed
-        goto cleanup;
c480ed
+        return -1;
c480ed
     }
c480ed
 
c480ed
 #define GET_STAT(name, member) \
c480ed
@@ -369,7 +352,7 @@ virNetDevOpenvswitchInterfaceStats(const char *ifname,
c480ed
                 *tmp != '\n') { \
c480ed
                 virReportError(VIR_ERR_INTERNAL_ERROR, "%s", \
c480ed
                                _("Fail to parse ovs-vsctl output")); \
c480ed
-                goto cleanup; \
c480ed
+                return -1; \
c480ed
             } \
c480ed
             gotStats = true; \
c480ed
         } \
c480ed
@@ -389,14 +372,10 @@ virNetDevOpenvswitchInterfaceStats(const char *ifname,
c480ed
     if (!gotStats) {
c480ed
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c480ed
                        _("Interface doesn't have any statistics"));
c480ed
-        goto cleanup;
c480ed
+        return -1;
c480ed
     }
c480ed
 
c480ed
-    ret = 0;
c480ed
-
c480ed
- cleanup:
c480ed
-    virCommandFree(cmd);
c480ed
-    return ret;
c480ed
+    return 0;
c480ed
 }
c480ed
 
c480ed
 /**
c480ed
@@ -414,12 +393,12 @@ int
c480ed
 virNetDevOpenvswitchGetVhostuserIfname(const char *path,
c480ed
                                        char **ifname)
c480ed
 {
c480ed
-    virCommandPtr cmd = NULL;
c480ed
     char *tmpIfname = NULL;
c480ed
     char **tokens = NULL;
c480ed
     size_t ntokens = 0;
c480ed
     int status;
c480ed
     int ret = -1;
c480ed
+    VIR_AUTOPTR(virCommand) cmd = NULL;
c480ed
 
c480ed
     /* Openvswitch vhostuser path are hardcoded to
c480ed
      * /<runstatedir>/openvswitch/<ifname>
c480ed
@@ -450,7 +429,6 @@ virNetDevOpenvswitchGetVhostuserIfname(const char *path,
c480ed
 
c480ed
  cleanup:
c480ed
     virStringListFreeCount(tokens, ntokens);
c480ed
-    virCommandFree(cmd);
c480ed
     return ret;
c480ed
 }
c480ed
 
c480ed
@@ -466,8 +444,7 @@ virNetDevOpenvswitchGetVhostuserIfname(const char *path,
c480ed
 int virNetDevOpenvswitchUpdateVlan(const char *ifname,
c480ed
                                    virNetDevVlanPtr virtVlan)
c480ed
 {
c480ed
-    int ret = -1;
c480ed
-    virCommandPtr cmd = NULL;
c480ed
+    VIR_AUTOPTR(virCommand) cmd = NULL;
c480ed
 
c480ed
     cmd = virCommandNew(OVSVSCTL);
c480ed
     virNetDevOpenvswitchAddTimeout(cmd);
c480ed
@@ -478,16 +455,13 @@ int virNetDevOpenvswitchUpdateVlan(const char *ifname,
c480ed
                          "--", "--if-exists", "set", "Port", ifname, NULL);
c480ed
 
c480ed
     if (virNetDevOpenvswitchConstructVlans(cmd, virtVlan) < 0)
c480ed
-        goto cleanup;
c480ed
+        return -1;
c480ed
 
c480ed
     if (virCommandRun(cmd, NULL) < 0) {
c480ed
         virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                        _("Unable to set vlan configuration on port %s"), ifname);
c480ed
-        goto cleanup;
c480ed
+        return -1;
c480ed
     }
c480ed
 
c480ed
-    ret = 0;
c480ed
- cleanup:
c480ed
-    virCommandFree(cmd);
c480ed
-    return ret;
c480ed
+    return 0;
c480ed
 }
c480ed
-- 
c480ed
2.22.0
c480ed