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 <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Peter Krempa 2020-10-02 09:53:54 +02:00
parent abfb033ddb
commit 9d7ca30ece

View File

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