From e40f8649bfa5e5e75579039e572c41e425438c38 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Thu, 6 Oct 2022 11:20:27 +0200 Subject: [PATCH] conf: domain: Refactor cleanup in virSysinfoSystemParseXML MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Register automatic cleanup for virSysinfoSystemDef and use it to refactor the cleanup code paths in virSysinfoSystemParseXML. Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- 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 14f4d22f06..e44d5fdc50 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -12128,8 +12128,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; @@ -12137,11 +12136,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); @@ -12153,15 +12150,14 @@ virSysinfoSystemParseXML(xmlNodePtr node, if (virUUIDParse(tmpUUID, uuidbuf) < 0) { virReportError(VIR_ERR_XML_DETAIL, "%s", _("malformed 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 and " - "")); - goto cleanup; + _("UUID mismatch between and ")); + return -1; } /* Although we've validated the UUID as good, virUUIDParse() is * lax with respect to allowing extraneous "-" and " ", but the @@ -12176,15 +12172,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);