[libvirt] [PATCH 3/7] Clean up error handling in qemudDomainAttachNetDevice()

Mark McLoughlin markmc at redhat.com
Wed Jul 22 21:57:32 UTC 2009


In subsequent patches we're going to have a file descriptor to close
too, so centralize the error handling cleanups to make things easier.

* src/qemu_conf.c: in qemudDomainAttachNetDevice() consolidate the
  error handling cleanups together
---
 src/qemu_driver.c |   37 +++++++++++++++----------------------
 1 files changed, 15 insertions(+), 22 deletions(-)

diff --git a/src/qemu_driver.c b/src/qemu_driver.c
index 7f4e2a1..4f0c60e 100644
--- a/src/qemu_driver.c
+++ b/src/qemu_driver.c
@@ -4549,7 +4549,7 @@ static int qemudDomainAttachNetDevice(virConnectPtr conn,
                                       unsigned int qemuCmdFlags)
 {
     virDomainNetDefPtr net = dev->data.net;
-    char *cmd, *reply, *remove_cmd;
+    char *cmd = NULL, *reply = NULL, *remove_cmd = NULL;
     int i;
     unsigned domain, bus, slot;
 
@@ -4567,16 +4567,12 @@ static int qemudDomainAttachNetDevice(virConnectPtr conn,
         return -1;
     }
 
-    if (VIR_REALLOC_N(vm->def->nets, vm->def->nnets+1) < 0) {
-        virReportOOMError(conn);
-        return -1;
-    }
+    if (VIR_REALLOC_N(vm->def->nets, vm->def->nnets+1) < 0)
+        goto no_memory;
 
     if ((qemuCmdFlags & QEMUD_CMD_FLAG_NET_NAME) &&
-        qemuAssignNetNames(vm->def, net) < 0) {
-        virReportOOMError(conn);
-        return -1;
-    }
+        qemuAssignNetNames(vm->def, net) < 0)
+        goto no_memory;
 
     /* Choose a vlan value greater than all other values since
      * older versions did not store the value in the state file.
@@ -4588,23 +4584,19 @@ static int qemudDomainAttachNetDevice(virConnectPtr conn,
 
     if (qemuBuildHostNetStr(conn, net,
                             "host_net_add ", ' ', net->vlan, NULL, &cmd) < 0)
-        return -1;
+        goto cleanup;
 
     remove_cmd = NULL;
     if (net->vlan >= 0 && net->hostnet_name &&
         virAsprintf(&remove_cmd, "host_net_remove %d %s",
                     net->vlan, net->hostnet_name) < 0) {
-        VIR_FREE(cmd);
-        virReportOOMError(conn);
-        return -1;
+        goto no_memory;
     }
 
     if (qemudMonitorCommand(vm, cmd, &reply) < 0) {
         qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                          _("failed to add network backend with '%s'"), cmd);
-        VIR_FREE(remove_cmd);
-        VIR_FREE(cmd);
-        return -1;
+        goto cleanup;
     }
 
     DEBUG("%s: host_net_add reply: %s", vm->def->name, reply);
@@ -4619,19 +4611,16 @@ static int qemudDomainAttachNetDevice(virConnectPtr conn,
     if (qemudMonitorCommand(vm, cmd, &reply) < 0) {
         qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                          _("failed to add NIC with '%s'"), cmd);
-        VIR_FREE(cmd);
         goto try_remove;
     }
 
-    VIR_FREE(cmd);
-
     if (qemudParsePciAddReply(vm, reply, &domain, &bus, &slot) < 0) {
         qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                          _("parsing pci_add reply failed: %s"), reply);
-        VIR_FREE(reply);
         goto try_remove;
     }
 
+    VIR_FREE(cmd);
     VIR_FREE(reply);
     VIR_FREE(remove_cmd);
 
@@ -4644,7 +4633,7 @@ static int qemudDomainAttachNetDevice(virConnectPtr conn,
     return 0;
 
 try_remove:
-    reply = NULL;
+    VIR_FREE(reply);
 
     if (!remove_cmd)
         VIR_WARN0(_("Unable to remove network backend\n"));
@@ -4652,10 +4641,14 @@ try_remove:
         VIR_WARN(_("Failed to remove network backend with '%s'\n"), remove_cmd);
     else
         VIR_DEBUG("%s: host_net_remove reply: %s\n", vm->def->name, reply);
+    goto cleanup;
 
+no_memory:
+    virReportOOMError(conn);
+cleanup:
+    VIR_FREE(cmd);
     VIR_FREE(reply);
     VIR_FREE(remove_cmd);
-
     return -1;
 }
 
-- 
1.6.2.5




More information about the libvir-list mailing list