Register automatic cleanup for virSysinfoSystemDef and use it to
refactor the cleanup code paths in virSysinfoSystemParseXML.
Signed-off-by: Peter Krempa <pkrempa(a)redhat.com>
---
src/conf/domain_conf.c | 24 ++++++++----------------
src/util/virsysinfo.h | 1 +
2 files changed, 9 insertions(+), 16 deletions(-)
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index b0a978cc62..8e9f415070 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -12184,8 +12184,7 @@ virSysinfoSystemParseXML(xmlNodePtr node,
bool uuid_generated)
{
VIR_XPATH_NODE_AUTORESTORE(ctxt)
- int ret = -1;
- virSysinfoSystemDef *def;
+ g_autoptr(virSysinfoSystemDef) def = g_new0(virSysinfoSystemDef, 1);
g_autofree char *tmpUUID = NULL;
ctxt->node = node;
@@ -12193,11 +12192,9 @@ virSysinfoSystemParseXML(xmlNodePtr node,
if (!virXMLNodeNameEqual(node, "system")) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("XML does not contain expected 'system'
element"));
- return ret;
+ return -1;
}
- def = g_new0(virSysinfoSystemDef, 1);
-
def->manufacturer =
virXPathString("string(entry[@name='manufacturer'])", ctxt);
def->product = virXPathString("string(entry[@name='product'])",
ctxt);
def->version = virXPathString("string(entry[@name='version'])",
ctxt);
@@ -12209,15 +12206,14 @@ virSysinfoSystemParseXML(xmlNodePtr node,
if (virUUIDParse(tmpUUID, uuidbuf) < 0) {
virReportError(VIR_ERR_XML_DETAIL,
"%s", _("malformed <sysinfo> uuid
element"));
- goto cleanup;
+ return -1;
}
if (uuid_generated) {
memcpy(domUUID, uuidbuf, VIR_UUID_BUFLEN);
} else if (memcmp(domUUID, uuidbuf, VIR_UUID_BUFLEN) != 0) {
virReportError(VIR_ERR_XML_DETAIL, "%s",
- _("UUID mismatch between <uuid> and "
- "<sysinfo>"));
- goto cleanup;
+ _("UUID mismatch between <uuid> and
<sysinfo>"));
+ return -1;
}
/* Although we've validated the UUID as good, virUUIDParse() is
* lax with respect to allowing extraneous "-" and " ", but
the
@@ -12232,15 +12228,11 @@ virSysinfoSystemParseXML(xmlNodePtr node,
def->family = virXPathString("string(entry[@name='family'])",
ctxt);
if (!def->manufacturer && !def->product && !def->version
&&
- !def->serial && !def->uuid && !def->sku &&
!def->family) {
- g_clear_pointer(&def, virSysinfoSystemDefFree);
- }
+ !def->serial && !def->uuid && !def->sku &&
!def->family)
+ return 0;
*sysdef = g_steal_pointer(&def);
- ret = 0;
- cleanup:
- virSysinfoSystemDefFree(def);
- return ret;
+ return 0;
}
static int
diff --git a/src/util/virsysinfo.h b/src/util/virsysinfo.h
index 899193dc81..d9f15b06e2 100644
--- a/src/util/virsysinfo.h
+++ b/src/util/virsysinfo.h
@@ -144,6 +144,7 @@ virSysinfoDef *virSysinfoRead(void);
void virSysinfoBIOSDefFree(virSysinfoBIOSDef *def);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virSysinfoBIOSDef, virSysinfoBIOSDefFree);
void virSysinfoSystemDefFree(virSysinfoSystemDef *def);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(virSysinfoSystemDef, virSysinfoSystemDefFree);
void virSysinfoBaseBoardDefClear(virSysinfoBaseBoardDef *def);
void virSysinfoChassisDefFree(virSysinfoChassisDef *def);
void virSysinfoOEMStringsDefFree(virSysinfoOEMStringsDef *def);
--
2.37.3