[PATCH 16/20] qemuMigrationCookieNBDXMLParse: Refactor memory handling

Peter Krempa pkrempa at redhat.com
Fri Oct 2 08:57:50 UTC 2020


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 at redhat.com>
---
 src/qemu/qemu_migration_cookie.c | 31 ++++++++++---------------------
 1 file changed, 10 insertions(+), 21 deletions(-)

diff --git a/src/qemu/qemu_migration_cookie.c b/src/qemu/qemu_migration_cookie.c
index c2ae3ab6a7..1e0a1c3d7f 100644
--- a/src/qemu/qemu_migration_cookie.c
+++ b/src/qemu/qemu_migration_cookie.c
@@ -919,40 +919,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);
@@ -962,20 +959,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);
 }


-- 
2.26.2




More information about the libvir-list mailing list