]> xenbits.xensource.com Git - libvirt.git/commitdiff
virXMLPropEnum: Always initialize '@result'
authorPeter Krempa <pkrempa@redhat.com>
Thu, 6 May 2021 12:35:22 +0000 (14:35 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 7 May 2021 08:06:19 +0000 (10:06 +0200)
Compilers aren't able to see whether @result is set or not and thus
don't warn of a potential use of uninitialized value. Always set @result
to prevent uninitialized use.

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

index 3ad596b3e2c7298e40e750a894a1b9f2c51844e4..8dcece704aba0dc20a19b89a5b74fd2ba728e24b 100644 (file)
@@ -562,12 +562,15 @@ virXMLPropEnumInternal(xmlNodePtr node,
                        const char* name,
                        int (*strToInt)(const char*),
                        virXMLPropFlags flags,
-                       unsigned int *result)
+                       unsigned int *result,
+                       unsigned int defaultResult)
 
 {
     g_autofree char *tmp = NULL;
     int ret;
 
+    *result = defaultResult;
+
     if (!(tmp = virXMLPropString(node, name))) {
         if (!(flags & VIR_XML_PROP_REQUIRED))
             return 0;
@@ -615,10 +618,8 @@ virXMLPropTristateBool(xmlNodePtr node,
 {
     flags |= VIR_XML_PROP_NONZERO;
 
-    *result = VIR_TRISTATE_BOOL_ABSENT;
-
     return virXMLPropEnumInternal(node, name, virTristateBoolTypeFromString,
-                                  flags, result);
+                                  flags, result, VIR_TRISTATE_BOOL_ABSENT);
 }
 
 
@@ -645,10 +646,8 @@ virXMLPropTristateSwitch(xmlNodePtr node,
 {
     flags |= VIR_XML_PROP_NONZERO;
 
-    *result = VIR_TRISTATE_SWITCH_ABSENT;
-
     return virXMLPropEnumInternal(node, name, virTristateSwitchTypeFromString,
-                                  flags, result);
+                                  flags, result, VIR_TRISTATE_SWITCH_ABSENT);
 }
 
 
@@ -851,9 +850,7 @@ virXMLPropEnumDefault(xmlNodePtr node,
                       unsigned int *result,
                       unsigned int defaultResult)
 {
-    *result = defaultResult;
-
-    return virXMLPropEnumInternal(node, name, strToInt, flags, result);
+    return virXMLPropEnumInternal(node, name, strToInt, flags, result, defaultResult);
 }
 
 
@@ -867,6 +864,7 @@ virXMLPropEnumDefault(xmlNodePtr node,
  * @result: The returned value
  *
  * Convenience function to return value of an enum attribute.
+ * @result is initialized to 0 on error or if the element is not found.
  *
  * Returns 1 in case of success in which case @result is set,
  *         or 0 if the attribute is not present,
@@ -879,7 +877,7 @@ virXMLPropEnum(xmlNodePtr node,
                virXMLPropFlags flags,
                unsigned int *result)
 {
-    return virXMLPropEnumInternal(node, name, strToInt, flags, result);
+    return virXMLPropEnumInternal(node, name, strToInt, flags, result, 0);
 }