]> xenbits.xensource.com Git - libvirt.git/commitdiff
libxl: unref objects in error paths
authorJim Fehlig <jfehlig@suse.com>
Wed, 24 Feb 2016 22:40:00 +0000 (15:40 -0700)
committerJim Fehlig <jfehlig@suse.com>
Thu, 25 Feb 2016 18:46:20 +0000 (11:46 -0700)
libxlMakeNic opens a virConnect object and takes a reference on a
virNetwork object, but doesn't drop the references on all error
paths. Rework the function to follow the standard libvirt pattern
of using a local 'ret' variable to hold the function return value,
performing all cleanup and returning 'ret' at a 'cleanup' label.

src/libxl/libxl_conf.c

index b20df2951048fad432a0e2bea46a37bf9601cc56..93c943ba54306972f0aed64fca63d293320e4564 100644 (file)
@@ -1286,7 +1286,10 @@ libxlMakeNic(virDomainDefPtr def,
 {
     bool ioemu_nic = def->os.type == VIR_DOMAIN_OSTYPE_HVM;
     virDomainNetType actual_type = virDomainNetGetActualType(l_nic);
+    virNetworkPtr network = NULL;
+    virConnectPtr conn = NULL;
     virNetDevBandwidthPtr actual_bw;
+    int ret = -1;
 
     /* TODO: Where is mtu stored?
      *
@@ -1312,64 +1315,47 @@ libxlMakeNic(virDomainDefPtr def,
 
     if (l_nic->model) {
         if (VIR_STRDUP(x_nic->model, l_nic->model) < 0)
-            return -1;
+            goto cleanup;
         if (STREQ(l_nic->model, "netfront"))
             x_nic->nictype = LIBXL_NIC_TYPE_VIF;
     }
 
     if (VIR_STRDUP(x_nic->ifname, l_nic->ifname) < 0)
-        return -1;
+        goto cleanup;
 
     switch (actual_type) {
         case VIR_DOMAIN_NET_TYPE_BRIDGE:
             if (VIR_STRDUP(x_nic->bridge,
                            virDomainNetGetActualBridgeName(l_nic)) < 0)
-                return -1;
+                goto cleanup;
             /* fallthrough */
         case VIR_DOMAIN_NET_TYPE_ETHERNET:
             if (VIR_STRDUP(x_nic->script, l_nic->script) < 0)
-                return -1;
+                goto cleanup;
             if (l_nic->nips > 0) {
                 x_nic->ip = virSocketAddrFormat(&l_nic->ips[0]->address);
                 if (!x_nic->ip)
-                    return -1;
+                    goto cleanup;
             }
             break;
         case VIR_DOMAIN_NET_TYPE_NETWORK:
         {
-            bool fail = false;
-            char *brname = NULL;
-            virNetworkPtr network;
-            virConnectPtr conn;
-
             if (!(conn = virConnectOpen("xen:///system")))
-                return -1;
+                goto cleanup;
 
             if (!(network =
                   virNetworkLookupByName(conn, l_nic->data.network.name))) {
-                virObjectUnref(conn);
-                return -1;
+                goto cleanup;
             }
 
             if (l_nic->nips > 0) {
                 x_nic->ip = virSocketAddrFormat(&l_nic->ips[0]->address);
                 if (!x_nic->ip)
-                    return -1;
-            }
-
-            if ((brname = virNetworkGetBridgeName(network))) {
-                if (VIR_STRDUP(x_nic->bridge, brname) < 0)
-                    fail = true;
-            } else {
-                fail = true;
+                    goto cleanup;
             }
 
-            VIR_FREE(brname);
-
-            virObjectUnref(network);
-            virObjectUnref(conn);
-            if (fail)
-                return -1;
+            if (!(x_nic->bridge = virNetworkGetBridgeName(network)))
+                goto cleanup;
             break;
         }
         case VIR_DOMAIN_NET_TYPE_VHOSTUSER:
@@ -1385,18 +1371,18 @@ libxlMakeNic(virDomainDefPtr def,
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                     _("unsupported interface type %s"),
                     virDomainNetTypeToString(l_nic->type));
-            return -1;
+            goto cleanup;
     }
 
     if (l_nic->domain_name) {
 #ifdef LIBXL_HAVE_DEVICE_BACKEND_DOMNAME
         if (VIR_STRDUP(x_nic->backend_domname, l_nic->domain_name) < 0)
-            return -1;
+            goto cleanup;
 #else
         virReportError(VIR_ERR_XML_DETAIL, "%s",
                 _("this version of libxenlight does not "
                   "support backend domain name"));
-        return -1;
+        goto cleanup;
 #endif
     }
 
@@ -1438,7 +1424,13 @@ libxlMakeNic(virDomainDefPtr def,
         x_nic->rate_interval_usecs =  50000UL;
     }
 
-    return 0;
+    ret = 0;
+
+ cleanup:
+    virObjectUnref(network);
+    virObjectUnref(conn);
+
+    return ret;
 }
 
 static int