]> xenbits.xensource.com Git - libvirt.git/commitdiff
virDomainDiskDefGeometryParse: Use virXMLProp*
authorTim Wiederhake <twiederh@redhat.com>
Tue, 18 May 2021 15:04:46 +0000 (17:04 +0200)
committerLaine Stump <laine@redhat.com>
Tue, 18 May 2021 16:27:12 +0000 (12:27 -0400)
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@redhat.com>
Reviewed-by: Laine Stump <laine@redhat.com>
src/conf/domain_conf.c

index f55117e849ef50af0fc5a290f5b0dcac1d73ccce..bfcc56ca9ef5153bc36bcdac2a537edd138c4d6b 100644 (file)
@@ -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;
 }