[libvirt PATCH 02/10] virDomainAudioCommonParse: Use virXMLProp*

Tim Wiederhake twiederh at redhat.com
Tue Apr 27 15:04:33 UTC 2021


This strictens the parser to disallow negative values (interpreted as
`UINT_MAX + value + 1`) for attributes `voices` (typically 1),
`bufferLength` (measured in milliseconds), `frequency` (in Hz, typically
44100), and `channels` (typically 2 for stereo).

None of these properties benefit from or have a sensible use-case for
wrap-around behavior.

Signed-off-by: Tim Wiederhake <twiederh at redhat.com>
---
 src/conf/domain_conf.c | 73 ++++++++++++------------------------------
 1 file changed, 20 insertions(+), 53 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index d8e34e79b0..371a9dead7 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -13419,31 +13419,19 @@ virDomainAudioCommonParse(virDomainAudioIOCommon *def,
                           xmlNodePtr node,
                           xmlXPathContextPtr ctxt)
 {
-    g_autofree char *mixingEngine = virXMLPropString(node, "mixingEngine");
-    g_autofree char *fixedSettings = virXMLPropString(node, "fixedSettings");
-    g_autofree char *voices = virXMLPropString(node, "voices");
-    g_autofree char *bufferLength = virXMLPropString(node, "bufferLength");
     xmlNodePtr settings;
     VIR_XPATH_NODE_AUTORESTORE(ctxt);
 
     ctxt->node = node;
     settings = virXPathNode("./settings", ctxt);
 
-    if (mixingEngine &&
-        ((def->mixingEngine =
-          virTristateBoolTypeFromString(mixingEngine)) <= 0)) {
-        virReportError(VIR_ERR_XML_ERROR,
-                       _("unknown 'mixingEngine' value '%s'"), mixingEngine);
+    if (virXMLPropTristateBool(node, "mixingEngine", VIR_XML_PROP_NONE,
+                               &def->mixingEngine) < 0)
         return -1;
-    }
 
-    if (fixedSettings &&
-        ((def->fixedSettings =
-          virTristateBoolTypeFromString(fixedSettings)) <= 0)) {
-        virReportError(VIR_ERR_XML_ERROR,
-                       _("unknown 'fixedSettings' value '%s'"), fixedSettings);
+    if (virXMLPropTristateBool(node, "fixedSettings", VIR_XML_PROP_NONE,
+                               &def->fixedSettings) < 0)
         return -1;
-    }
 
     if (def->fixedSettings == VIR_TRISTATE_BOOL_YES &&
         def->mixingEngine != VIR_TRISTATE_BOOL_YES) {
@@ -13452,58 +13440,37 @@ virDomainAudioCommonParse(virDomainAudioIOCommon *def,
         return -1;
     }
 
-    if (voices &&
-        (virStrToLong_ui(voices, NULL, 10, &def->voices) < 0 ||
-         !def->voices)) {
-        virReportError(VIR_ERR_XML_ERROR,
-                       _("cannot parse 'voices' value '%s'"), voices);
+    if (virXMLPropUInt(node, "voices", 10,
+                       VIR_XML_PROP_NONE | VIR_XML_PROP_NONZERO,
+                       &def->voices) < 0)
         return -1;
-    }
 
-    if (bufferLength &&
-        (virStrToLong_ui(bufferLength, NULL, 10, &def->bufferLength) < 0 ||
-         !def->bufferLength)) {
-        virReportError(VIR_ERR_XML_ERROR,
-                       _("cannot parse 'bufferLength' value '%s'"), bufferLength);
+    if (virXMLPropUInt(node, "bufferLength", 10,
+                       VIR_XML_PROP_NONE | VIR_XML_PROP_NONZERO,
+                       &def->bufferLength) < 0)
         return -1;
-    }
 
     if (settings) {
-        g_autofree char *frequency = virXMLPropString(settings, "frequency");
-        g_autofree char *channels = virXMLPropString(settings, "channels");
-        g_autofree char *format = virXMLPropString(settings, "format");
-
         if (def->fixedSettings != VIR_TRISTATE_BOOL_YES) {
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                            _("audio settings specified without fixed settings flag"));
             return -1;
         }
 
-        if (frequency &&
-            (virStrToLong_ui(frequency, NULL, 10, &def->frequency) < 0 ||
-             !def->frequency)) {
-            virReportError(VIR_ERR_XML_ERROR,
-                           _("cannot parse 'frequency' value '%s'"), frequency);
+        if (virXMLPropUInt(settings, "frequency", 10,
+                           VIR_XML_PROP_NONE | VIR_XML_PROP_NONZERO,
+                           &def->frequency) < 0)
             return -1;
-        }
 
-        if (channels &&
-            (virStrToLong_ui(channels, NULL, 10, &def->channels) < 0 ||
-             !def->channels)) {
-            virReportError(VIR_ERR_XML_ERROR,
-                           _("cannot parse 'channels' value '%s'"), channels);
+        if (virXMLPropUInt(settings, "channels", 10,
+                           VIR_XML_PROP_NONE | VIR_XML_PROP_NONZERO,
+                           &def->channels) < 0)
             return -1;
-        }
 
-        if (format) {
-            int value;
-            if ((value = virDomainAudioFormatTypeFromString(format)) <= 0) {
-                virReportError(VIR_ERR_XML_ERROR,
-                               _("cannot parse 'format' value '%s'"), format);
-                return -1;
-            }
-            def->format = value;
-        }
+        if (virXMLPropEnum(settings, "format",
+                           virDomainAudioFormatTypeFromString,
+                           VIR_XML_PROP_NONE, &def->format) < 0)
+            return -1;
     }
 
     return 0;
-- 
2.26.3




More information about the libvir-list mailing list