[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 2/3] conf: tighten up XML integer parsing



https://bugzilla.redhat.com/show_bug.cgi?id=617711 reported that
even with my recent patched to allow <memory unit='G'>1</memory>,
people can still get away with trying <memory>1G</memory> and
silently get <memory unit='KiB'>1</memory> instead.  While
virt-xml-validate catches the error, our C parser was not.

I always love it when I can reduce lines of code while fixing bugs.

* src/conf/domain_conf.c (virDomainDefParseXML): Avoid strtoll.
* src/conf/storage_conf.c (virStorageDefParsePerms): Likewise.
* src/util/xml.c (virXPathLongBase, virXPathULongBase)
(virXPathULongLong, virXPathLongLong): Likewise.
---
 src/conf/domain_conf.c  |   12 +++++-------
 src/conf/storage_conf.c |    6 +++---
 src/util/xml.c          |   36 ++++--------------------------------
 3 files changed, 12 insertions(+), 42 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 5ab052a..9d61448 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -8086,12 +8086,11 @@ static virDomainDefPtr virDomainDefParseXML(virCapsPtr caps,
             if (STREQ(tmp, "reset")) {
                 def->clock.data.utc_reset = true;
             } else {
-                char *conv = NULL;
-                unsigned long long val;
-                val = strtoll(tmp, &conv, 10);
-                if (conv == tmp || *conv != '\0') {
-                    virDomainReportError(VIR_ERR_INTERNAL_ERROR,
-                                         _("unknown clock adjustment '%s'"), tmp);
+                if (virStrToLong_ll(tmp, NULL, 10,
+                                    &def->clock.data.variable.adjustment) < 0) {
+                    virDomainReportError(VIR_ERR_XML_ERROR,
+                                         _("unknown clock adjustment '%s'"),
+                                         tmp);
                     goto error;
                 }
                 switch (def->clock.offset) {
@@ -8103,7 +8102,6 @@ static virDomainDefPtr virDomainDefParseXML(virCapsPtr caps,
                     break;
                 }
                 def->clock.offset = VIR_DOMAIN_CLOCK_OFFSET_VARIABLE;
-                def->clock.data.variable.adjustment = val;
             }
             VIR_FREE(tmp);
         } else {
diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c
index 2330fa1..64cc0cd 100644
--- a/src/conf/storage_conf.c
+++ b/src/conf/storage_conf.c
@@ -570,14 +570,14 @@ virStorageDefParsePerms(xmlXPathContextPtr ctxt,
     if (!mode) {
         perms->mode = defaultmode;
     } else {
-        char *end = NULL;
-        perms->mode = strtol(mode, &end, 8);
-        if (*end || (perms->mode & ~0777)) {
+        int tmp;
+        if (virStrToLong_i(mode, NULL, 8, &tmp) < 0 || (tmp & ~0777)) {
             VIR_FREE(mode);
             virStorageReportError(VIR_ERR_XML_ERROR,
                                   "%s", _("malformed octal mode"));
             goto error;
         }
+        perms->mode = tmp;
         VIR_FREE(mode);
     }

diff --git a/src/util/xml.c b/src/util/xml.c
index 79a9d27..7411968 100644
--- a/src/util/xml.c
+++ b/src/util/xml.c
@@ -174,15 +174,8 @@ virXPathLongBase(const char *xpath,
     ctxt->node = relnode;
     if ((obj != NULL) && (obj->type == XPATH_STRING) &&
         (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
-        char *conv = NULL;
-        long val;
-
-        val = strtol((const char *) obj->stringval, &conv, base);
-        if (conv == (const char *) obj->stringval) {
+        if (virStrToLong_l((char *) obj->stringval, NULL, base, value) < 0)
             ret = -2;
-        } else {
-            *value = val;
-        }
     } else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
                (!(isnan(obj->floatval)))) {
         *value = (long) obj->floatval;
@@ -287,15 +280,8 @@ virXPathULongBase(const char *xpath,
     ctxt->node = relnode;
     if ((obj != NULL) && (obj->type == XPATH_STRING) &&
         (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
-        char *conv = NULL;
-        long val;
-
-        val = strtoul((const char *) obj->stringval, &conv, base);
-        if (conv == (const char *) obj->stringval) {
+        if (virStrToLong_ul((char *) obj->stringval, NULL, base, value) < 0)
             ret = -2;
-        } else {
-            *value = val;
-        }
     } else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
                (!(isnan(obj->floatval)))) {
         *value = (unsigned long) obj->floatval;
@@ -411,15 +397,8 @@ virXPathULongLong(const char *xpath,
     ctxt->node = relnode;
     if ((obj != NULL) && (obj->type == XPATH_STRING) &&
         (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
-        char *conv = NULL;
-        unsigned long long val;
-
-        val = strtoull((const char *) obj->stringval, &conv, 10);
-        if (conv == (const char *) obj->stringval) {
+        if (virStrToLong_ull((char *) obj->stringval, NULL, 10, value) < 0)
             ret = -2;
-        } else {
-            *value = val;
-        }
     } else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
                (!(isnan(obj->floatval)))) {
         *value = (unsigned long long) obj->floatval;
@@ -465,15 +444,8 @@ virXPathLongLong(const char *xpath,
     ctxt->node = relnode;
     if ((obj != NULL) && (obj->type == XPATH_STRING) &&
         (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
-        char *conv = NULL;
-        unsigned long long val;
-
-        val = strtoll((const char *) obj->stringval, &conv, 10);
-        if (conv == (const char *) obj->stringval) {
+        if (virStrToLong_ll((char *) obj->stringval, NULL, 10, value) < 0)
             ret = -2;
-        } else {
-            *value = val;
-        }
     } else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
                (!(isnan(obj->floatval)))) {
         *value = (long long) obj->floatval;
-- 
1.7.7.6


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]