virDomainSnapshotDiskDefParseXML: Automatically free temporary variables and remove cleanup

Refactor the function to avoid the cleanup section used to just free
memory associated with the parsed object.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2022-03-08 09:11:15 +01:00
parent 8a0e9e106b
commit b150c6cf31

View File

@ -138,21 +138,20 @@ virDomainSnapshotDiskDefParseXML(xmlNodePtr node,
unsigned int flags, unsigned int flags,
virDomainXMLOption *xmlopt) virDomainXMLOption *xmlopt)
{ {
int ret = -1; g_autofree char *snapshot = NULL;
char *snapshot = NULL; g_autofree char *type = NULL;
char *type = NULL; g_autofree char *driver = NULL;
char *driver = NULL; g_autofree char *name = NULL;
g_autoptr(virStorageSource) src = virStorageSourceNew();
xmlNodePtr cur; xmlNodePtr cur;
VIR_XPATH_NODE_AUTORESTORE(ctxt) VIR_XPATH_NODE_AUTORESTORE(ctxt)
ctxt->node = node; ctxt->node = node;
def->src = virStorageSourceNew(); if (!(name = virXMLPropString(node, "name"))) {
def->name = virXMLPropString(node, "name");
if (!def->name) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("missing name from disk snapshot element")); _("missing name from disk snapshot element"));
goto cleanup; return -1;
} }
snapshot = virXMLPropString(node, "snapshot"); snapshot = virXMLPropString(node, "snapshot");
@ -162,59 +161,54 @@ virDomainSnapshotDiskDefParseXML(xmlNodePtr node,
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("unknown disk snapshot setting '%s'"), _("unknown disk snapshot setting '%s'"),
snapshot); snapshot);
goto cleanup; return -1;
} }
} }
if ((type = virXMLPropString(node, "type"))) { if ((type = virXMLPropString(node, "type"))) {
if ((def->src->type = virStorageTypeFromString(type)) <= 0 || if ((src->type = virStorageTypeFromString(type)) <= 0 ||
def->src->type == VIR_STORAGE_TYPE_VOLUME || src->type == VIR_STORAGE_TYPE_VOLUME ||
def->src->type == VIR_STORAGE_TYPE_DIR) { src->type == VIR_STORAGE_TYPE_DIR) {
virReportError(VIR_ERR_XML_ERROR, virReportError(VIR_ERR_XML_ERROR,
_("unknown disk snapshot type '%s'"), type); _("unknown disk snapshot type '%s'"), type);
goto cleanup; return -1;
} }
} else { } else {
def->src->type = VIR_STORAGE_TYPE_FILE; src->type = VIR_STORAGE_TYPE_FILE;
} }
if ((cur = virXPathNode("./source", ctxt)) && if ((cur = virXPathNode("./source", ctxt)) &&
virDomainStorageSourceParse(cur, ctxt, def->src, flags, xmlopt) < 0) virDomainStorageSourceParse(cur, ctxt, src, flags, xmlopt) < 0)
goto cleanup; return -1;
if ((driver = virXPathString("string(./driver/@type)", ctxt)) && if ((driver = virXPathString("string(./driver/@type)", ctxt)) &&
(def->src->format = virStorageFileFormatTypeFromString(driver)) <= 0) { (src->format = virStorageFileFormatTypeFromString(driver)) <= 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("unknown disk snapshot driver '%s'"), driver); _("unknown disk snapshot driver '%s'"), driver);
goto cleanup; return -1;
} }
if (virParseScaledValue("./driver/metadata_cache/max_size", NULL, if (virParseScaledValue("./driver/metadata_cache/max_size", NULL,
ctxt, ctxt,
&def->src->metadataCacheMaxSize, &src->metadataCacheMaxSize,
1, ULLONG_MAX, false) < 0) 1, ULLONG_MAX, false) < 0)
goto cleanup; return -1;
/* validate that the passed path is absolute */ /* validate that the passed path is absolute */
if (virStorageSourceIsRelative(def->src)) { if (virStorageSourceIsRelative(src)) {
virReportError(VIR_ERR_XML_ERROR, virReportError(VIR_ERR_XML_ERROR,
_("disk snapshot image path '%s' must be absolute"), _("disk snapshot image path '%s' must be absolute"),
def->src->path); src->path);
goto cleanup; return -1;
} }
if (!def->snapshot && (def->src->path || def->src->format)) if (!def->snapshot && (src->path || src->format))
def->snapshot = VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL; def->snapshot = VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL;
ret = 0; def->name = g_steal_pointer(&name);
cleanup: def->src = g_steal_pointer(&src);
VIR_FREE(driver); return 0;
VIR_FREE(snapshot);
VIR_FREE(type);
if (ret < 0)
virDomainSnapshotDiskDefClear(def);
return ret;
} }
/* flags is bitwise-or of virDomainSnapshotParseFlags. /* flags is bitwise-or of virDomainSnapshotParseFlags.