[PATCH 15/20] qemuMigrationCookieNetworkXMLParse: Refactor memory handling

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


Use modern allocators, automatic memory feeing, and decrease the scope
of some variables to remove the 'error' label.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/qemu/qemu_migration_cookie.c | 27 ++++++++-------------------
 1 file changed, 8 insertions(+), 19 deletions(-)

diff --git a/src/qemu/qemu_migration_cookie.c b/src/qemu/qemu_migration_cookie.c
index 8b5491e388..c2ae3ab6a7 100644
--- a/src/qemu/qemu_migration_cookie.c
+++ b/src/qemu/qemu_migration_cookie.c
@@ -882,27 +882,24 @@ qemuMigrationCookieGraphicsXMLParse(xmlXPathContextPtr ctxt)
 static qemuMigrationCookieNetworkPtr
 qemuMigrationCookieNetworkXMLParse(xmlXPathContextPtr ctxt)
 {
-    qemuMigrationCookieNetworkPtr optr;
+    g_autoptr(qemuMigrationCookieNetwork) optr = g_new0(qemuMigrationCookieNetwork, 1);
     size_t i;
     int n;
-    xmlNodePtr *interfaces = NULL;
-    char *vporttype;
+    g_autofree xmlNodePtr *interfaces = NULL;
     VIR_XPATH_NODE_AUTORESTORE(ctxt)

-    if (VIR_ALLOC(optr) < 0)
-        goto error;
-
     if ((n = virXPathNodeSet("./network/interface", ctxt, &interfaces)) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        "%s", _("missing interface information"));
-        goto error;
+        return NULL;
     }

     optr->nnets = n;
-    if (VIR_ALLOC_N(optr->net, optr->nnets) < 0)
-        goto error;
+    optr->net = g_new0(qemuMigrationCookieNetData, optr->nnets);

     for (i = 0; i < n; i++) {
+        g_autofree char *vporttype = NULL;
+
         /* portdata is optional, and may not exist */
         ctxt->node = interfaces[i];
         optr->net[i].portdata = virXPathString("string(./portdata[1])", ctxt);
@@ -910,20 +907,12 @@ qemuMigrationCookieNetworkXMLParse(xmlXPathContextPtr ctxt)
         if (!(vporttype = virXMLPropString(interfaces[i], "vporttype"))) {
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            "%s", _("missing vporttype attribute in migration data"));
-            goto error;
+            return NULL;
         }
         optr->net[i].vporttype = virNetDevVPortTypeFromString(vporttype);
-        VIR_FREE(vporttype);
     }

-    VIR_FREE(interfaces);
-
-    return optr;
-
- error:
-    VIR_FREE(interfaces);
-    qemuMigrationCookieNetworkFree(optr);
-    return NULL;
+    return g_steal_pointer(&optr);
 }


-- 
2.26.2




More information about the libvir-list mailing list