[libvirt PATCH 05/10] virDomainDiskDefGeometryParse: Use virXMLProp*

Tim Wiederhake twiederh at redhat.com
Tue May 18 15:04:46 UTC 2021


This strictens the parser to disallow negative values (interpreted as
`UINT_MAX + value + 1`) for attributes `cyls`, `heads` and `secs`.
Allowing negative numbers to be interpreted this way makes no sense for
these attributes.

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

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index f55117e849..bfcc56ca9e 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -8815,45 +8815,21 @@ static int
 virDomainDiskDefGeometryParse(virDomainDiskDef *def,
                               xmlNodePtr cur)
 {
-    g_autofree char *tmp = NULL;
-
-    if ((tmp = virXMLPropString(cur, "cyls"))) {
-        if (virStrToLong_ui(tmp, NULL, 10, &def->geometry.cylinders) < 0) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("invalid geometry settings (cyls)"));
-            return -1;
-        }
-        VIR_FREE(tmp);
-    }
+    if (virXMLPropUInt(cur, "cyls", 10, VIR_XML_PROP_NONE,
+                       &def->geometry.cylinders) < 0)
+        return -1;
 
-    if ((tmp = virXMLPropString(cur, "heads"))) {
-        if (virStrToLong_ui(tmp, NULL, 10, &def->geometry.heads) < 0) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("invalid geometry settings (heads)"));
-            return -1;
-        }
-        VIR_FREE(tmp);
-    }
+    if (virXMLPropUInt(cur, "heads", 10, VIR_XML_PROP_NONE,
+                       &def->geometry.heads) < 0)
+        return -1;
 
-    if ((tmp = virXMLPropString(cur, "secs"))) {
-        if (virStrToLong_ui(tmp, NULL, 10, &def->geometry.sectors) < 0) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("invalid geometry settings (secs)"));
-            return -1;
-        }
-        VIR_FREE(tmp);
-    }
+    if (virXMLPropUInt(cur, "secs", 10, VIR_XML_PROP_NONE,
+                       &def->geometry.sectors) < 0)
+        return -1;
 
-    if ((tmp = virXMLPropString(cur, "trans"))) {
-        int value;
-        if ((value = virDomainDiskGeometryTransTypeFromString(tmp)) <= 0) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                           _("invalid translation value '%s'"),
-                           tmp);
-            return -1;
-        }
-        def->geometry.trans = value;
-    }
+    if (virXMLPropEnum(cur, "trans", virDomainDiskGeometryTransTypeFromString,
+                       VIR_XML_PROP_NONZERO, &def->geometry.trans) < 0)
+        return -1;
 
     return 0;
 }
-- 
2.26.3




More information about the libvir-list mailing list