From: Peter Krempa Date: Fri, 2 Oct 2020 07:53:54 +0000 (+0200) Subject: qemuMigrationCookieNBDXMLParse: Refactor memory handling X-Git-Tag: v6.9.0-rc1~335 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9d7ca30ecefe76bd81696682d75774ec0378fa3f;p=thirdparty%2Flibvirt.git qemuMigrationCookieNBDXMLParse: Refactor memory handling 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 Reviewed-by: Michal Privoznik --- diff --git a/src/qemu/qemu_migration_cookie.c b/src/qemu/qemu_migration_cookie.c index d00aa2aa96..73f7ce68bc 100644 --- a/src/qemu/qemu_migration_cookie.c +++ b/src/qemu/qemu_migration_cookie.c @@ -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); }