virDomainSnapshotDefParse: Refactor cleanup

Use automatic memory cleanup, decrease scope of variables and remove the
'cleanup' label.

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 18:32:36 +01:00
parent c250ab90ac
commit 12b85a3611

View File

@ -216,17 +216,12 @@ virDomainSnapshotDefParse(xmlXPathContextPtr ctxt,
bool *current, bool *current,
unsigned int flags) unsigned int flags)
{ {
virDomainSnapshotDef *def = NULL; g_autoptr(virDomainSnapshotDef) def = NULL;
virDomainSnapshotDef *ret = NULL; g_autofree xmlNodePtr *diskNodes = NULL;
xmlNodePtr *nodes = NULL;
xmlNodePtr inactiveDomNode = NULL;
size_t i; size_t i;
int n; int n;
char *state = NULL; g_autofree char *memorySnapshot = NULL;
int active; g_autofree char *memoryFile = NULL;
char *tmp;
char *memorySnapshot = NULL;
char *memoryFile = NULL;
bool offline = !!(flags & VIR_DOMAIN_SNAPSHOT_PARSE_OFFLINE); bool offline = !!(flags & VIR_DOMAIN_SNAPSHOT_PARSE_OFFLINE);
virSaveCookieCallbacks *saveCookie = virDomainXMLOptionGetSaveCookie(xmlopt); virSaveCookieCallbacks *saveCookie = virDomainXMLOptionGetSaveCookie(xmlopt);
int domainflags = VIR_DOMAIN_DEF_PARSE_INACTIVE | int domainflags = VIR_DOMAIN_DEF_PARSE_INACTIVE |
@ -240,18 +235,22 @@ virDomainSnapshotDefParse(xmlXPathContextPtr ctxt,
if (flags & VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE) { if (flags & VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("a redefined snapshot must have a name")); _("a redefined snapshot must have a name"));
goto cleanup; return NULL;
} }
} }
def->parent.description = virXPathString("string(./description)", ctxt); def->parent.description = virXPathString("string(./description)", ctxt);
if (flags & VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE) { if (flags & VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE) {
g_autofree char *state = NULL;
g_autofree char *domtype = NULL;
xmlNodePtr inactiveDomNode = NULL;
if (virXPathLongLong("string(./creationTime)", ctxt, if (virXPathLongLong("string(./creationTime)", ctxt,
&def->parent.creationTime) < 0) { &def->parent.creationTime) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("missing creationTime from existing snapshot")); _("missing creationTime from existing snapshot"));
goto cleanup; return NULL;
} }
def->parent.parent_name = virXPathString("string(./parent/name)", ctxt); def->parent.parent_name = virXPathString("string(./parent/name)", ctxt);
@ -263,14 +262,14 @@ virDomainSnapshotDefParse(xmlXPathContextPtr ctxt,
*/ */
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("missing state from existing snapshot")); _("missing state from existing snapshot"));
goto cleanup; return NULL;
} }
def->state = virDomainSnapshotStateTypeFromString(state); def->state = virDomainSnapshotStateTypeFromString(state);
if (def->state <= 0) { if (def->state <= 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("Invalid state '%s' in domain snapshot XML"), _("Invalid state '%s' in domain snapshot XML"),
state); state);
goto cleanup; return NULL;
} }
offline = (def->state == VIR_DOMAIN_SNAPSHOT_SHUTOFF || offline = (def->state == VIR_DOMAIN_SNAPSHOT_SHUTOFF ||
def->state == VIR_DOMAIN_SNAPSHOT_DISK_SNAPSHOT); def->state == VIR_DOMAIN_SNAPSHOT_DISK_SNAPSHOT);
@ -279,20 +278,19 @@ virDomainSnapshotDefParse(xmlXPathContextPtr ctxt,
* lack domain/@type. In that case, leave dom NULL, and * lack domain/@type. In that case, leave dom NULL, and
* clients will have to decide between best effort * clients will have to decide between best effort
* initialization or outright failure. */ * initialization or outright failure. */
if ((tmp = virXPathString("string(./domain/@type)", ctxt))) { if ((domtype = virXPathString("string(./domain/@type)", ctxt))) {
xmlNodePtr domainNode = virXPathNode("./domain", ctxt); xmlNodePtr domainNode = virXPathNode("./domain", ctxt);
VIR_FREE(tmp);
if (!domainNode) { if (!domainNode) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("missing domain in snapshot")); _("missing domain in snapshot"));
goto cleanup; return NULL;
} }
def->parent.dom = virDomainDefParseNode(ctxt->node->doc, domainNode, def->parent.dom = virDomainDefParseNode(ctxt->node->doc, domainNode,
xmlopt, parseOpaque, xmlopt, parseOpaque,
domainflags); domainflags);
if (!def->parent.dom) if (!def->parent.dom)
goto cleanup; return NULL;
} else { } else {
VIR_WARN("parsing older snapshot that lacks domain"); VIR_WARN("parsing older snapshot that lacks domain");
} }
@ -304,10 +302,10 @@ virDomainSnapshotDefParse(xmlXPathContextPtr ctxt,
def->parent.inactiveDom = virDomainDefParseNode(ctxt->node->doc, inactiveDomNode, def->parent.inactiveDom = virDomainDefParseNode(ctxt->node->doc, inactiveDomNode,
xmlopt, NULL, domainflags); xmlopt, NULL, domainflags);
if (!def->parent.inactiveDom) if (!def->parent.inactiveDom)
goto cleanup; return NULL;
} }
} else if (virDomainXMLOptionRunMomentPostParse(xmlopt, &def->parent) < 0) { } else if (virDomainXMLOptionRunMomentPostParse(xmlopt, &def->parent) < 0) {
goto cleanup; return NULL;
} }
memorySnapshot = virXPathString("string(./memory/@snapshot)", ctxt); memorySnapshot = virXPathString("string(./memory/@snapshot)", ctxt);
@ -318,20 +316,20 @@ virDomainSnapshotDefParse(xmlXPathContextPtr ctxt,
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("unknown memory snapshot setting '%s'"), _("unknown memory snapshot setting '%s'"),
memorySnapshot); memorySnapshot);
goto cleanup; return NULL;
} }
if (memoryFile && if (memoryFile &&
def->memory != VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL) { def->memory != VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL) {
virReportError(VIR_ERR_XML_ERROR, virReportError(VIR_ERR_XML_ERROR,
_("memory filename '%s' requires external snapshot"), _("memory filename '%s' requires external snapshot"),
memoryFile); memoryFile);
goto cleanup; return NULL;
} }
if (!memoryFile && if (!memoryFile &&
def->memory == VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL) { def->memory == VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("external memory snapshots require a filename")); _("external memory snapshots require a filename"));
goto cleanup; return NULL;
} }
} else if (memoryFile) { } else if (memoryFile) {
def->memory = VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL; def->memory = VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL;
@ -345,7 +343,7 @@ virDomainSnapshotDefParse(xmlXPathContextPtr ctxt,
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("memory state cannot be saved with offline or " _("memory state cannot be saved with offline or "
"disk-only snapshot")); "disk-only snapshot"));
goto cleanup; return NULL;
} }
def->memorysnapshotfile = g_steal_pointer(&memoryFile); def->memorysnapshotfile = g_steal_pointer(&memoryFile);
@ -354,48 +352,40 @@ virDomainSnapshotDefParse(xmlXPathContextPtr ctxt,
virReportError(VIR_ERR_XML_ERROR, virReportError(VIR_ERR_XML_ERROR,
_("memory snapshot file path (%s) must be absolute"), _("memory snapshot file path (%s) must be absolute"),
def->memorysnapshotfile); def->memorysnapshotfile);
goto cleanup; return NULL;
} }
if ((n = virXPathNodeSet("./disks/*", ctxt, &nodes)) < 0) if ((n = virXPathNodeSet("./disks/*", ctxt, &diskNodes)) < 0)
goto cleanup; return NULL;
if (n) if (n)
def->disks = g_new0(virDomainSnapshotDiskDef, n); def->disks = g_new0(virDomainSnapshotDiskDef, n);
def->ndisks = n; def->ndisks = n;
for (i = 0; i < def->ndisks; i++) { for (i = 0; i < def->ndisks; i++) {
if (virDomainSnapshotDiskDefParseXML(nodes[i], ctxt, &def->disks[i], if (virDomainSnapshotDiskDefParseXML(diskNodes[i], ctxt, &def->disks[i],
flags, xmlopt) < 0) flags, xmlopt) < 0)
goto cleanup; return NULL;
} }
VIR_FREE(nodes);
if (flags & VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL) { if (flags & VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL) {
int active;
if (!current) { if (!current) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("internal parse requested with NULL current")); _("internal parse requested with NULL current"));
goto cleanup; return NULL;
} }
if (virXPathInt("string(./active)", ctxt, &active) < 0) { if (virXPathInt("string(./active)", ctxt, &active) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Could not find 'active' element")); _("Could not find 'active' element"));
goto cleanup; return NULL;
} }
*current = active != 0; *current = active != 0;
} }
if (!offline && virSaveCookieParse(ctxt, &def->cookie, saveCookie) < 0) if (!offline && virSaveCookieParse(ctxt, &def->cookie, saveCookie) < 0)
goto cleanup; return NULL;
ret = g_steal_pointer(&def); return g_steal_pointer(&def);
cleanup:
VIR_FREE(state);
VIR_FREE(nodes);
VIR_FREE(memorySnapshot);
VIR_FREE(memoryFile);
virObjectUnref(def);
return ret;
} }
virDomainSnapshotDef * virDomainSnapshotDef *