]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: xml: Extract implementation of xml property -> enum parsing to a common helper
authorPeter Krempa <pkrempa@redhat.com>
Thu, 6 May 2021 11:53:18 +0000 (13:53 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 7 May 2021 08:06:18 +0000 (10:06 +0200)
virXMLPropTristateBool/virXMLPropTristateSwitch/virXMLPropEnum can be
implemented using the same internal code. Extract it into a new function
called virXMLPropEnumInternal, which will also simplify adding versions
of these functions with a custom default value.

This way we'll be able to always initialize @result so that unused value
bugs can be prevented.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/util/virxml.c

index 7cc73ab9a0280f9076a89f21349fb9355b114931..9929064993d4f481259dba8bbbc73e3851c147af 100644 (file)
@@ -557,6 +557,40 @@ virXMLNodeContentString(xmlNodePtr node)
     return ret;
 }
 
+static int
+virXMLPropEnumInternal(xmlNodePtr node,
+                       const char* name,
+                       int (*strToInt)(const char*),
+                       virXMLPropFlags flags,
+                       unsigned int *result)
+
+{
+    g_autofree char *tmp = NULL;
+    int ret;
+
+    if (!(tmp = virXMLPropString(node, name))) {
+        if (!(flags & VIR_XML_PROP_REQUIRED))
+            return 0;
+
+        virReportError(VIR_ERR_XML_ERROR,
+                       _("Missing required attribute '%s' in element '%s'"),
+                       name, node->name);
+        return -1;
+    }
+
+    ret = strToInt(tmp);
+    if (ret < 0 ||
+        ((flags & VIR_XML_PROP_NONZERO) && (ret == 0))) {
+        virReportError(VIR_ERR_XML_ERROR,
+                       _("Invalid value for attribute '%s' in element '%s': '%s'."),
+                       name, node->name, NULLSTR(tmp));
+        return -1;
+    }
+
+    *result = ret;
+    return 1;
+}
+
 
 /**
  * virXMLPropTristateBool:
@@ -577,28 +611,10 @@ virXMLPropTristateBool(xmlNodePtr node,
                        virXMLPropFlags flags,
                        virTristateBool *result)
 {
-    g_autofree char *tmp = NULL;
-    int val;
-
-    if (!(tmp = virXMLPropString(node, name))) {
-        if (!(flags & VIR_XML_PROP_REQUIRED))
-            return 0;
-
-        virReportError(VIR_ERR_XML_ERROR,
-                       _("Missing required attribute '%s' in element '%s'"),
-                       name, node->name);
-        return -1;
-    }
-
-    if ((val = virTristateBoolTypeFromString(tmp)) <= 0) {
-        virReportError(VIR_ERR_XML_ERROR,
-                       _("Invalid value for attribute '%s' in element '%s': '%s'. Expected 'yes' or 'no'"),
-                       name, node->name, tmp);
-        return -1;
-    }
+    flags |= VIR_XML_PROP_NONZERO;
 
-    *result = val;
-    return 1;
+    return virXMLPropEnumInternal(node, name, virTristateBoolTypeFromString,
+                                  flags, result);
 }
 
 
@@ -621,28 +637,10 @@ virXMLPropTristateSwitch(xmlNodePtr node,
                          virXMLPropFlags flags,
                          virTristateSwitch *result)
 {
-    g_autofree char *tmp = NULL;
-    int val;
-
-    if (!(tmp = virXMLPropString(node, name))) {
-        if (!(flags & VIR_XML_PROP_REQUIRED))
-            return 0;
-
-        virReportError(VIR_ERR_XML_ERROR,
-                       _("Missing required attribute '%s' in element '%s'"),
-                       name, node->name);
-        return -1;
-    }
-
-    if ((val = virTristateSwitchTypeFromString(tmp)) <= 0) {
-        virReportError(VIR_ERR_XML_ERROR,
-                       _("Invalid value for attribute '%s' in element '%s': '%s'. Expected 'on' or 'off'"),
-                       name, node->name, tmp);
-        return -1;
-    }
+    flags |= VIR_XML_PROP_NONZERO;
 
-    *result = val;
-    return 1;
+    return virXMLPropEnumInternal(node, name, virTristateSwitchTypeFromString,
+                                  flags, result);
 }
 
 
@@ -833,32 +831,10 @@ virXMLPropEnum(xmlNodePtr node,
                virXMLPropFlags flags,
                unsigned int *result)
 {
-    g_autofree char *tmp = NULL;
-    int ret;
-
-    if (!(tmp = virXMLPropString(node, name))) {
-        if (!(flags & VIR_XML_PROP_REQUIRED))
-            return 0;
-
-        virReportError(VIR_ERR_XML_ERROR,
-                       _("Missing required attribute '%s' in element '%s'"),
-                       name, node->name);
-        return -1;
-    }
-
-    ret = strToInt(tmp);
-    if (ret < 0 ||
-        ((flags & VIR_XML_PROP_NONZERO) && (ret == 0))) {
-        virReportError(VIR_ERR_XML_ERROR,
-                       _("Invalid value for attribute '%s' in element '%s': '%s'."),
-                       name, node->name, NULLSTR(tmp));
-        return -1;
-    }
-
-    *result = ret;
-    return 1;
+    return virXMLPropEnumInternal(node, name, strToInt, flags, result);
 }
 
+
 /**
  * virXPathBoolean:
  * @xpath: the XPath string to evaluate