[libvirt PATCH v6 3/8] util: add virXMLPropUIntDefault() function

Jonathon Jongsma jjongsma at redhat.com
Wed Jan 25 20:16:02 UTC 2023


This function allows you to specify a default value to return if the
property is not found rather than always setting *result to 0.

Signed-off-by: Jonathon Jongsma <jjongsma at redhat.com>
---
 src/conf/cpu_conf.c    |  9 +++------
 src/conf/domain_conf.c |  6 +-----
 src/conf/numa_conf.c   |  7 +------
 src/util/virxml.c      | 29 ++++++++++++++++++++++++++++-
 src/util/virxml.h      |  9 +++++++++
 5 files changed, 42 insertions(+), 18 deletions(-)

diff --git a/src/conf/cpu_conf.c b/src/conf/cpu_conf.c
index a33f39ef31..d1d11e3257 100644
--- a/src/conf/cpu_conf.c
+++ b/src/conf/cpu_conf.c
@@ -570,7 +570,6 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
     }
 
     if ((topology = virXPathNode("./topology[1]", ctxt))) {
-        int rc;
 
         if (virXMLPropUInt(topology, "sockets", 10,
                            VIR_XML_PROP_REQUIRED | VIR_XML_PROP_NONZERO,
@@ -578,12 +577,10 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
             return -1;
         }
 
-        if ((rc = virXMLPropUInt(topology, "dies", 10,
-                                 VIR_XML_PROP_NONZERO,
-                                 &def->dies)) < 0) {
+        if (virXMLPropUIntDefault(topology, "dies", 10,
+                                  VIR_XML_PROP_NONZERO,
+                                  &def->dies, 1) < 0) {
             return -1;
-        } else if (rc == 0) {
-            def->dies = 1;
         }
 
         if (virXMLPropUInt(topology, "cores", 10,
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 951c15ac58..04c9b1d174 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -16971,7 +16971,6 @@ virDomainVcpuParse(virDomainDef *def,
     unsigned int vcpus;
     g_autofree char *tmp = NULL;
     g_autofree xmlNodePtr *nodes = NULL;
-    int rc;
 
     vcpus = maxvcpus = 1;
 
@@ -16986,11 +16985,8 @@ virDomainVcpuParse(virDomainDef *def,
         }
         VIR_FREE(tmp);
 
-        if ((rc = virXMLPropUInt(vcpuNode, "current", 10, VIR_XML_PROP_NONE, &vcpus)) < 0) {
+        if (virXMLPropUIntDefault(vcpuNode, "current", 10, VIR_XML_PROP_NONE, &vcpus, maxvcpus) < 0)
             return -1;
-        } else if (rc == 0) {
-            vcpus = maxvcpus;
-        }
 
         if (virXMLPropEnumDefault(vcpuNode, "placement",
                                   virDomainCpuPlacementModeTypeFromString,
diff --git a/src/conf/numa_conf.c b/src/conf/numa_conf.c
index b55bb3ffcb..7ef7aa5138 100644
--- a/src/conf/numa_conf.c
+++ b/src/conf/numa_conf.c
@@ -916,16 +916,11 @@ virDomainNumaDefParseXML(virDomainNuma *def,
     for (i = 0; i < n; i++) {
         VIR_XPATH_NODE_AUTORESTORE(ctxt)
         g_autofree char *tmp = NULL;
-        int rc;
         unsigned int cur_cell;
 
-        if ((rc = virXMLPropUInt(cell[i], "id", 10, VIR_XML_PROP_NONE,
-                                 &cur_cell)) < 0)
+        if (virXMLPropUIntDefault(cell[i], "id", 10, VIR_XML_PROP_NONE, &cur_cell, i) < 0)
             return -1;
 
-        if (rc == 0)
-            cur_cell = i;
-
         /* cells are in order of parsing or explicitly numbered */
         if (cur_cell >= n) {
             virReportError(VIR_ERR_XML_ERROR, "%s",
diff --git a/src/util/virxml.c b/src/util/virxml.c
index 9b6ccfd6c9..af4be4e443 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -560,12 +560,39 @@ virXMLPropUInt(xmlNodePtr node,
                int base,
                virXMLPropFlags flags,
                unsigned int *result)
+{
+    return virXMLPropUIntDefault(node, name, base, flags, result, 0);
+}
+
+
+/**
+ * virXMLPropUIntDefault:
+ * @node: XML dom node pointer
+ * @name: Name of the property (attribute) to get
+ * @base: Number base, see strtol
+ * @flags: Bitwise-OR of virXMLPropFlags
+ * @result: The returned value
+ * @defaultResult: Default value of @result in case the property is not found
+ *
+ * Convenience function to return value of an unsigned integer attribute.
+ *
+ * Returns 1 in case of success in which case @result is set,
+ *         or 0 if the attribute is not present,
+ *         or -1 and reports an error on failure.
+ */
+int
+virXMLPropUIntDefault(xmlNodePtr node,
+                      const char *name,
+                      int base,
+                      virXMLPropFlags flags,
+                      unsigned int *result,
+                      unsigned int defaultResult)
 {
     g_autofree char *tmp = NULL;
     int ret;
     unsigned int val;
 
-    *result = 0;
+    *result = defaultResult;
 
     if (!(tmp = virXMLPropString(node, name))) {
         if (!(flags & VIR_XML_PROP_REQUIRED))
diff --git a/src/util/virxml.h b/src/util/virxml.h
index d5b998263c..cca9f222ab 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -132,6 +132,15 @@ virXMLPropUInt(xmlNodePtr node,
                unsigned int *result)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(5);
 
+int
+virXMLPropUIntDefault(xmlNodePtr node,
+                      const char *name,
+                      int base,
+                      virXMLPropFlags flags,
+                      unsigned int *result,
+                      unsigned int defaultResult)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(5);
+
 int
 virXMLPropLongLong(xmlNodePtr node,
                    const char *name,
-- 
2.39.0



More information about the libvir-list mailing list