[PATCH 12/17] virXMLPropTristateSwitch: Always initialize '@result'

Peter Krempa pkrempa at redhat.com
Thu May 6 15:31:04 UTC 2021


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.

In two cases the code needed to be adjusted to preserve functionality.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/conf/domain_conf.c | 10 ++++++++--
 src/util/virxml.c      |  4 ++++
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index ea99a4c40a..ce0ffc60ef 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -17443,12 +17443,15 @@ virDomainFeaturesDefParse(virDomainDef *def,
         case VIR_DOMAIN_FEATURE_PVSPINLOCK:
         case VIR_DOMAIN_FEATURE_VMPORT:
         case VIR_DOMAIN_FEATURE_SMM: {
-            virTristateSwitch state = VIR_TRISTATE_SWITCH_ON;
+            virTristateSwitch state;

             if (virXMLPropTristateSwitch(nodes[i], "state",
                                          VIR_XML_PROP_NONE, &state) < 0)
                 return -1;

+            if (state == VIR_TRISTATE_SWITCH_ABSENT)
+                state = VIR_TRISTATE_SWITCH_ON;
+
             def->features[val] = state;
             break;
         }
@@ -17770,7 +17773,7 @@ virDomainFeaturesDefParse(virDomainDef *def,
         return -1;

     for (i = 0; i < n; i++) {
-        virTristateSwitch state = VIR_TRISTATE_SWITCH_ON;
+        virTristateSwitch state;
         int val = virDomainProcessCapsFeatureTypeFromString((const char *)nodes[i]->name);
         if (val < 0) {
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
@@ -17783,6 +17786,9 @@ virDomainFeaturesDefParse(virDomainDef *def,
                                      &state) < 0)
             return -1;

+        if (state == VIR_TRISTATE_SWITCH_ABSENT)
+            state = VIR_TRISTATE_SWITCH_ON;
+
         def->caps_features[val] = state;
     }
     VIR_FREE(nodes);
diff --git a/src/util/virxml.c b/src/util/virxml.c
index d9bc5199b3..8e28629e9a 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -630,6 +630,8 @@ virXMLPropTristateBool(xmlNodePtr node,
  * @result: The returned value
  *
  * Convenience function to return value of an on / off attribute.
+ * In case when the property is missing @result is initialized to
+ * VIR_TRISTATE_SWITCH_ABSENT.
  *
  * Returns 1 in case of success in which case @result is set,
  *         or 0 if the attribute is not present,
@@ -643,6 +645,8 @@ virXMLPropTristateSwitch(xmlNodePtr node,
 {
     flags |= VIR_XML_PROP_NONZERO;

+    *result = VIR_TRISTATE_SWITCH_ABSENT;
+
     return virXMLPropEnumInternal(node, name, virTristateSwitchTypeFromString,
                                   flags, result);
 }
-- 
2.30.2




More information about the libvir-list mailing list