]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
conf: domain: Refactor cleanup in virSysinfoSystemParseXML
authorPeter Krempa <pkrempa@redhat.com>
Thu, 6 Oct 2022 09:20:27 +0000 (11:20 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 2 Nov 2022 08:22:55 +0000 (09:22 +0100)
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>
src/conf/domain_conf.c
src/util/virsysinfo.h

index 14f4d22f0694ac4e9df2a13649135b171a956bc0..e44d5fdc507ee27f733e84ebd1c1f50acbfb0cf3 100644 (file)
@@ -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 <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
@@ -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
index 899193dc81aa91542316af4efc1855b4678cf5ed..d9f15b06e2f8d150bedb9a7a7eaec0a4d2721a0f 100644 (file)
@@ -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);