[libvirt] [PATCH] conf: forbid negative values in virDomainParseScaledValue

Martin Kletzander mkletzan at redhat.com
Wed Oct 29 16:35:39 UTC 2014


It makes sense for none of the callers to have negative value as an
output and, fortunately, if anyone tried defining domain with negative
memory or any other value parsed by virDomainParseScaledValue(), the
resulting value was 0.  That means we can error out during parsing as
it won't break anything.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1155843

Signed-off-by: Martin Kletzander <mkletzan at redhat.com>
---
"it won't break anything" -- famous last words?

 src/conf/domain_conf.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 39befb0..a351382 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -6334,28 +6334,34 @@ virDomainParseScaledValue(const char *xpath,
 {
     char *xpath_full = NULL;
     char *unit = NULL;
+    char *bytes_str = NULL;
     int ret = -1;
     unsigned long long bytes;

     *val = 0;
     if (virAsprintf(&xpath_full, "string(%s)", xpath) < 0)
         goto cleanup;
-    ret = virXPathULongLong(xpath_full, ctxt, &bytes);
-    if (ret < 0) {
-        if (ret == -2)
-            virReportError(VIR_ERR_XML_ERROR,
-                           _("could not parse element %s"),
-                           xpath);
-        else if (required)
+
+    bytes_str = virXPathString(xpath_full, ctxt);
+    if (!bytes_str) {
+        if (!required) {
+            ret = 0;
+        } else {
             virReportError(VIR_ERR_XML_ERROR,
                            _("missing element %s"),
                            xpath);
-        else
-            ret = 0;
+        }
         goto cleanup;
     }
     VIR_FREE(xpath_full);

+    if (virStrToLong_ullp(bytes_str, NULL, 10, &bytes) < 0) {
+        virReportError(VIR_ERR_XML_ERROR,
+                       _("Invalid value '%s' for element '%s'"),
+                       bytes_str, xpath);
+        goto cleanup;
+    }
+
     if (virAsprintf(&xpath_full, "string(%s/@unit)", xpath) < 0)
         goto cleanup;
     unit = virXPathString(xpath_full, ctxt);
@@ -6366,6 +6372,7 @@ virDomainParseScaledValue(const char *xpath,
     *val = bytes;
     ret = 1;
  cleanup:
+    VIR_FREE(bytes_str);
     VIR_FREE(xpath_full);
     VIR_FREE(unit);
     return ret;
--
2.1.2




More information about the libvir-list mailing list