[libvirt] [PATCH 1/3] conf/domain_conf: use virStringParseYesNo helper

Mao Zhongyi maozhongyi at cmss.chinamobile.com
Wed Oct 16 02:39:48 UTC 2019


This helper performs a conversion from a "yes|no" string
to a corresponding boolean, and several conversions were
already done, but there are still some omissions.

For most of the remaining usages in domain_conf.c only
"yes" is explicitly checked for. This means all other
values are implicitly handled as 'false'. In this case,
use virStringParseYesNo, but if it returns -1, don't
raise an error but set the bool value to false.

Cc: crobinso at redhat.com
Cc: berrange at redhat.com
Cc: abologna at redhat.com
Cc: laine at laine.org
Cc: phrdina at redhat.com
Cc: mkletzan at redhat.com
Cc: g.sho1500 at gmail.com

Signed-off-by: Mao Zhongyi <maozhongyi at cmss.chinamobile.com>
Signed-off-by: Zhang Shengju <zhangshengju at cmss.chinamobile.com>
---
 src/conf/domain_conf.c | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 10d6bf0eea..7420658726 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -7601,8 +7601,8 @@ virDomainHostdevSubsysUSBDefParseXML(xmlNodePtr node,
     }
 
     if ((autoAddress = virXMLPropString(node, "autoAddress"))) {
-        if (STREQ(autoAddress, "yes"))
-            usbsrc->autoAddress = true;
+        if (virStringParseYesNo(autoAddress, &usbsrc->autoAddress) < 0)
+            usbsrc->autoAddress = false;
     }
 
     /* Product can validly be 0, so we need some extra help to determine
@@ -8168,8 +8168,8 @@ virDomainHostdevDefParseXMLSubsys(xmlNodePtr node,
      * (e.g. <interface>) with type='hostdev')
      */
     if ((managed = virXMLPropString(node, "managed")) != NULL) {
-        if (STREQ(managed, "yes"))
-            def->managed = true;
+        if (virStringParseYesNo(managed, &def->managed) < 0)
+            def->managed = false;
     }
 
     sgio = virXMLPropString(node, "sgio");
@@ -13807,9 +13807,7 @@ virDomainGraphicsListenDefParseXML(virDomainGraphicsListenDefPtr def,
 
     if (autoGenerated &&
         flags & VIR_DOMAIN_DEF_PARSE_STATUS) {
-        if (STREQ(autoGenerated, "yes")) {
-            def->autoGenerated = true;
-        } else if (STRNEQ(autoGenerated, "no")) {
+        if (virStringParseYesNo(autoGenerated, &def->autoGenerated) < 0) {
             virReportError(VIR_ERR_XML_ERROR,
                            _("Invalid autoGenerated value: %s"),
                            autoGenerated);
@@ -13939,12 +13937,11 @@ virDomainGraphicsDefParseXMLVNC(virDomainGraphicsDefPtr def,
     }
 
     if (autoport) {
-        if (STREQ(autoport, "yes")) {
-            if (flags & VIR_DOMAIN_DEF_PARSE_INACTIVE)
-                def->data.vnc.port = 0;
-            def->data.vnc.autoport = true;
-        } else {
+        if (virStringParseYesNo(autoport, &def->data.vnc.autoport) < 0) {
             def->data.vnc.autoport = false;
+        } else {
+            if (def->data.vnc.autoport && flags & VIR_DOMAIN_DEF_PARSE_INACTIVE)
+                def->data.vnc.port = 0;
         }
     }
 
@@ -13958,8 +13955,9 @@ virDomainGraphicsDefParseXMLVNC(virDomainGraphicsDefPtr def,
         }
     }
 
-    if (websocketGenerated && STREQ(websocketGenerated, "yes"))
-        def->data.vnc.websocketGenerated = true;
+    if (websocketGenerated &&
+        virStringParseYesNo(websocketGenerated, &def->data.vnc.websocketGenerated))
+        def->data.vnc.websocketGenerated = false;
 
     if (sharePolicy) {
         int policy =
@@ -15479,8 +15477,8 @@ virDomainVideoDefParseXML(virDomainXMLOptionPtr xmlopt,
                 heads = virXMLPropString(cur, "heads");
 
                 if ((primary = virXMLPropString(cur, "primary")) != NULL) {
-                    if (STREQ(primary, "yes"))
-                        def->primary = true;
+                    if (virStringParseYesNo(primary, &def->primary) < 0)
+                        def->primary = false;
                     VIR_FREE(primary);
                 }
 
-- 
2.17.1






More information about the libvir-list mailing list