conf: domain: Refactor cleanup in virSysinfoSystemParseXML

Register automatic cleanup for virSysinfoSystemDef and use it to
refactor the cleanup code paths in virSysinfoSystemParseXML.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2022-10-06 11:20:27 +02:00
parent a574b8cd87
commit e40f8649bf
2 changed files with 9 additions and 16 deletions

View File

@ -12128,8 +12128,7 @@ virSysinfoSystemParseXML(xmlNodePtr node,
bool uuid_generated) bool uuid_generated)
{ {
VIR_XPATH_NODE_AUTORESTORE(ctxt) VIR_XPATH_NODE_AUTORESTORE(ctxt)
int ret = -1; g_autoptr(virSysinfoSystemDef) def = g_new0(virSysinfoSystemDef, 1);
virSysinfoSystemDef *def;
g_autofree char *tmpUUID = NULL; g_autofree char *tmpUUID = NULL;
ctxt->node = node; ctxt->node = node;
@ -12137,11 +12136,9 @@ virSysinfoSystemParseXML(xmlNodePtr node,
if (!virXMLNodeNameEqual(node, "system")) { if (!virXMLNodeNameEqual(node, "system")) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("XML does not contain expected 'system' element")); _("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->manufacturer = virXPathString("string(entry[@name='manufacturer'])", ctxt);
def->product = virXPathString("string(entry[@name='product'])", ctxt); def->product = virXPathString("string(entry[@name='product'])", ctxt);
def->version = virXPathString("string(entry[@name='version'])", ctxt); def->version = virXPathString("string(entry[@name='version'])", ctxt);
@ -12153,15 +12150,14 @@ virSysinfoSystemParseXML(xmlNodePtr node,
if (virUUIDParse(tmpUUID, uuidbuf) < 0) { if (virUUIDParse(tmpUUID, uuidbuf) < 0) {
virReportError(VIR_ERR_XML_DETAIL, virReportError(VIR_ERR_XML_DETAIL,
"%s", _("malformed <sysinfo> uuid element")); "%s", _("malformed <sysinfo> uuid element"));
goto cleanup; return -1;
} }
if (uuid_generated) { if (uuid_generated) {
memcpy(domUUID, uuidbuf, VIR_UUID_BUFLEN); memcpy(domUUID, uuidbuf, VIR_UUID_BUFLEN);
} else if (memcmp(domUUID, uuidbuf, VIR_UUID_BUFLEN) != 0) { } else if (memcmp(domUUID, uuidbuf, VIR_UUID_BUFLEN) != 0) {
virReportError(VIR_ERR_XML_DETAIL, "%s", virReportError(VIR_ERR_XML_DETAIL, "%s",
_("UUID mismatch between <uuid> and " _("UUID mismatch between <uuid> and <sysinfo>"));
"<sysinfo>")); return -1;
goto cleanup;
} }
/* Although we've validated the UUID as good, virUUIDParse() is /* Although we've validated the UUID as good, virUUIDParse() is
* lax with respect to allowing extraneous "-" and " ", but the * lax with respect to allowing extraneous "-" and " ", but the
@ -12176,15 +12172,11 @@ virSysinfoSystemParseXML(xmlNodePtr node,
def->family = virXPathString("string(entry[@name='family'])", ctxt); def->family = virXPathString("string(entry[@name='family'])", ctxt);
if (!def->manufacturer && !def->product && !def->version && if (!def->manufacturer && !def->product && !def->version &&
!def->serial && !def->uuid && !def->sku && !def->family) { !def->serial && !def->uuid && !def->sku && !def->family)
g_clear_pointer(&def, virSysinfoSystemDefFree); return 0;
}
*sysdef = g_steal_pointer(&def); *sysdef = g_steal_pointer(&def);
ret = 0; return 0;
cleanup:
virSysinfoSystemDefFree(def);
return ret;
} }
static int static int

View File

@ -144,6 +144,7 @@ virSysinfoDef *virSysinfoRead(void);
void virSysinfoBIOSDefFree(virSysinfoBIOSDef *def); void virSysinfoBIOSDefFree(virSysinfoBIOSDef *def);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virSysinfoBIOSDef, virSysinfoBIOSDefFree); G_DEFINE_AUTOPTR_CLEANUP_FUNC(virSysinfoBIOSDef, virSysinfoBIOSDefFree);
void virSysinfoSystemDefFree(virSysinfoSystemDef *def); void virSysinfoSystemDefFree(virSysinfoSystemDef *def);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virSysinfoSystemDef, virSysinfoSystemDefFree);
void virSysinfoBaseBoardDefClear(virSysinfoBaseBoardDef *def); void virSysinfoBaseBoardDefClear(virSysinfoBaseBoardDef *def);
void virSysinfoChassisDefFree(virSysinfoChassisDef *def); void virSysinfoChassisDefFree(virSysinfoChassisDef *def);
void virSysinfoOEMStringsDefFree(virSysinfoOEMStringsDef *def); void virSysinfoOEMStringsDefFree(virSysinfoOEMStringsDef *def);