]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemuMigrationCookieNetworkXMLParse: 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' label.

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

index a4f602dcc5b0d3e373022923c7278fa44839c790..d00aa2aa96f3b976f2727db58a835e4483b6b023 100644 (file)
@@ -879,27 +879,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);
@@ -907,20 +904,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);
 }