]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemuMigrationCookieNBDXMLParse: Refactor memory handling
authorPeter Krempa <pkrempa@redhat.com>
Fri, 2 Oct 2020 07:53:54 +0000 (09:53 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 5 Oct 2020 13:58:53 +0000 (15:58 +0200)
Use modern allocators, automatic memory feeing, and decrease the scope
of some variables to remove the 'error' and 'cleanup' labels.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/qemu/qemu_migration_cookie.c

index d00aa2aa96f3b976f2727db58a835e4483b6b023..73f7ce68bc3755374c6994385a9f6bab7cfe21a6 100644 (file)
@@ -916,40 +916,37 @@ qemuMigrationCookieNetworkXMLParse(xmlXPathContextPtr ctxt)
 static qemuMigrationCookieNBDPtr
 qemuMigrationCookieNBDXMLParse(xmlXPathContextPtr ctxt)
 {
-    qemuMigrationCookieNBDPtr ret = NULL;
-    char *port = NULL, *capacity = NULL;
+    g_autoptr(qemuMigrationCookieNBD) ret = g_new0(qemuMigrationCookieNBD, 1);
+    g_autofree char *port = NULL;
     size_t i;
     int n;
-    xmlNodePtr *disks = NULL;
+    g_autofree xmlNodePtr *disks = NULL;
     VIR_XPATH_NODE_AUTORESTORE(ctxt)
 
-    if (VIR_ALLOC(ret) < 0)
-        goto error;
-
     port = virXPathString("string(./nbd/@port)", ctxt);
     if (port && virStrToLong_i(port, NULL, 10, &ret->port) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Malformed nbd port '%s'"),
                        port);
-        goto error;
+        return NULL;
     }
 
     /* Now check if source sent a list of disks to prealloc. We might be
      * talking to an older server, so it's not an error if the list is
      * missing. */
     if ((n = virXPathNodeSet("./nbd/disk", ctxt, &disks)) > 0) {
-        if (VIR_ALLOC_N(ret->disks, n) < 0)
-            goto error;
+        ret->disks = g_new0(struct qemuMigrationCookieNBDDisk, n);
         ret->ndisks = n;
 
         for (i = 0; i < n; i++) {
+            g_autofree char *capacity = NULL;
+
             ctxt->node = disks[i];
-            VIR_FREE(capacity);
 
             if (!(ret->disks[i].target = virXPathString("string(./@target)", ctxt))) {
                 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                                _("Malformed disk target"));
-                goto error;
+                return NULL;
             }
 
             capacity = virXPathString("string(./@capacity)", ctxt);
@@ -959,20 +956,12 @@ qemuMigrationCookieNBDXMLParse(xmlXPathContextPtr ctxt)
                 virReportError(VIR_ERR_INTERNAL_ERROR,
                                _("Malformed disk capacity: '%s'"),
                                NULLSTR(capacity));
-                goto error;
+                return NULL;
             }
         }
     }
 
- cleanup:
-    VIR_FREE(port);
-    VIR_FREE(capacity);
-    VIR_FREE(disks);
-    return ret;
- error:
-    qemuMigrationCookieNBDFree(ret);
-    ret = NULL;
-    goto cleanup;
+    return g_steal_pointer(&ret);
 }