All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 24/24] option: Fix checking of sizes for overflow and trailing crap
Date: Thu, 23 Feb 2017 20:54:02 +0100	[thread overview]
Message-ID: <1487879642-16139-25-git-send-email-armbru@redhat.com> (raw)
In-Reply-To: <1487879642-16139-1-git-send-email-armbru@redhat.com>

parse_option_size()'s checking for overflow and trailing crap is
wrong.  Has always been that way.  qemu_strtosz() gets it right, so
use that.

This adds support for size suffixes 'P', 'E', and ignores case for all
suffixes, not just 'k'.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <1487708048-2131-25-git-send-email-armbru@redhat.com>
---
 tests/test-qemu-opts.c | 21 +++++++++------------
 util/qemu-option.c     | 41 +++++++++++++----------------------------
 2 files changed, 22 insertions(+), 40 deletions(-)

diff --git a/tests/test-qemu-opts.c b/tests/test-qemu-opts.c
index 8b92f7b..c46ef31 100644
--- a/tests/test-qemu-opts.c
+++ b/tests/test-qemu-opts.c
@@ -702,10 +702,9 @@ static void test_opts_parse_size(void)
     g_assert(!opts);
     opts = qemu_opts_parse(&opts_list_02,
                            "size1=18446744073709550592", /* fffffffffffffc00 */
-                           false, &error_abort);
-    /* BUG: should reject */
-    g_assert_cmpuint(opts_count(opts), ==, 1);
-    g_assert_cmpuint(qemu_opt_get_size(opts, "size1", 1), ==, 0);
+                           false, &err);
+    error_free_or_abort(&err);
+    g_assert(!opts);
 
     /* Suffixes */
     opts = qemu_opts_parse(&opts_list_02, "size1=8b,size2=1.5k,size3=2M",
@@ -723,19 +722,17 @@ static void test_opts_parse_size(void)
 
     /* Beyond limit with suffix */
     opts = qemu_opts_parse(&opts_list_02, "size1=16777216T",
-                           false, &error_abort);
-    /* BUG: should reject */
-    g_assert_cmpuint(opts_count(opts), ==, 1);
-    g_assert_cmpuint(qemu_opt_get_size(opts, "size1", 1), ==, 0);
+                           false, &err);
+    error_free_or_abort(&err);
+    g_assert(!opts);
 
     /* Trailing crap */
     opts = qemu_opts_parse(&opts_list_02, "size1=16E", false, &err);
     error_free_or_abort(&err);
     g_assert(!opts);
-    opts = qemu_opts_parse(&opts_list_02, "size1=16Gi", false, &error_abort);
-    /* BUG: should reject */
-    g_assert_cmpuint(opts_count(opts), ==, 1);
-    g_assert_cmpuint(qemu_opt_get_size(opts, "size1", 1), ==, 16 * G_BYTE);
+    opts = qemu_opts_parse(&opts_list_02, "size1=16Gi", false, &err);
+    error_free_or_abort(&err);
+    g_assert(!opts);
 
     qemu_opts_reset(&opts_list_02);
 }
diff --git a/util/qemu-option.c b/util/qemu-option.c
index 273d00d..419f252 100644
--- a/util/qemu-option.c
+++ b/util/qemu-option.c
@@ -174,39 +174,24 @@ static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
 void parse_option_size(const char *name, const char *value,
                        uint64_t *ret, Error **errp)
 {
-    char *postfix;
-    double sizef;
+    uint64_t size;
+    int err;
 
-    sizef = strtod(value, &postfix);
-    if (sizef < 0 || sizef > UINT64_MAX) {
+    err = qemu_strtosz(value, NULL, &size);
+    if (err == -ERANGE) {
+        error_setg(errp, "Value '%s' is too large for parameter '%s'",
+                   value, name);
+        return;
+    }
+    if (err) {
         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name,
                    "a non-negative number below 2^64");
+        error_append_hint(errp, "Optional suffix k, M, G, T, P or E means"
+                          " kilo-, mega-, giga-, tera-, peta-\n"
+                          "and exabytes, respectively.\n");
         return;
     }
-    switch (*postfix) {
-    case 'T':
-        sizef *= 1024;
-        /* fall through */
-    case 'G':
-        sizef *= 1024;
-        /* fall through */
-    case 'M':
-        sizef *= 1024;
-        /* fall through */
-    case 'K':
-    case 'k':
-        sizef *= 1024;
-        /* fall through */
-    case 'b':
-    case '\0':
-        *ret = (uint64_t) sizef;
-        break;
-    default:
-        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size");
-        error_append_hint(errp, "You may use k, M, G or T suffixes for "
-                          "kilobytes, megabytes, gigabytes and terabytes.\n");
-        return;
-    }
+    *ret = size;
 }
 
 bool has_help_option(const char *param)
-- 
2.7.4

  parent reply	other threads:[~2017-02-23 19:54 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-23 19:53 [Qemu-devel] [PULL 00/24] option cutils: Fix and clean up number conversions Markus Armbruster
2017-02-23 19:53 ` [Qemu-devel] [PULL 01/24] test-qemu-opts: Cover qemu_opts_parse() Markus Armbruster
2017-02-23 19:53 ` [Qemu-devel] [PULL 02/24] option: Assert value string isn't null Markus Armbruster
2017-02-23 19:53 ` [Qemu-devel] [PULL 03/24] test-cutils: Add missing qemu_strtol()... endptr checks Markus Armbruster
2017-02-23 19:53 ` [Qemu-devel] [PULL 04/24] test-cutils: Clean up qemu_strtoul() result checks Markus Armbruster
2017-02-23 19:53 ` [Qemu-devel] [PULL 05/24] util/cutils: Rewrite documentation of qemu_strtol() & friends Markus Armbruster
2017-02-23 19:53 ` [Qemu-devel] [PULL 06/24] util/cutils: Rename qemu_strtoll(), qemu_strtoull() Markus Armbruster
2017-02-23 19:53 ` [Qemu-devel] [PULL 07/24] util/cutils: Clean up variable names around qemu_strtol() Markus Armbruster
2017-02-23 19:53 ` [Qemu-devel] [PULL 08/24] util/cutils: Clean up control flow around qemu_strtol() a bit Markus Armbruster
2017-02-23 19:53 ` [Qemu-devel] [PULL 09/24] option: Fix to reject invalid and overflowing numbers Markus Armbruster
2017-02-23 19:53 ` [Qemu-devel] [PULL 10/24] test-cutils: Add missing qemu_strtosz()... endptr checks Markus Armbruster
2017-02-23 19:53 ` [Qemu-devel] [PULL 11/24] test-cutils: Cover qemu_strtosz() invalid input Markus Armbruster
2017-02-23 19:53 ` [Qemu-devel] [PULL 12/24] test-cutils: Cover qemu_strtosz() with trailing crap Markus Armbruster
2017-02-23 19:53 ` [Qemu-devel] [PULL 13/24] test-cutils: Cover qemu_strtosz() around range limits Markus Armbruster
2017-02-23 19:53 ` [Qemu-devel] [PULL 14/24] util/cutils: New qemu_strtosz_metric() Markus Armbruster
2017-02-23 19:53 ` [Qemu-devel] [PULL 15/24] util/cutils: Rename qemu_strtosz() to qemu_strtosz_MiB() Markus Armbruster
2017-02-23 19:53 ` [Qemu-devel] [PULL 16/24] util/cutils: New qemu_strtosz() Markus Armbruster
2017-02-23 19:53 ` [Qemu-devel] [PULL 17/24] util/cutils: Drop QEMU_STRTOSZ_DEFSUFFIX_* macros Markus Armbruster
2017-02-23 19:53 ` [Qemu-devel] [PULL 18/24] test-cutils: Use qemu_strtosz() more often Markus Armbruster
2017-02-23 19:53 ` [Qemu-devel] [PULL 19/24] test-cutils: Drop suffix from test_qemu_strtosz_simple() Markus Armbruster
2017-02-23 19:53 ` [Qemu-devel] [PULL 20/24] qemu-img: Wrap cvtnum() around qemu_strtosz() Markus Armbruster
2017-02-23 19:53 ` [Qemu-devel] [PULL 21/24] util/cutils: Let qemu_strtosz*() optionally reject trailing crap Markus Armbruster
2017-02-23 19:54 ` [Qemu-devel] [PULL 22/24] util/cutils: Return qemu_strtosz*() error and value separately Markus Armbruster
2017-02-23 19:54 ` [Qemu-devel] [PULL 23/24] util/cutils: Change qemu_strtosz*() from int64_t to uint64_t Markus Armbruster
2017-02-23 19:54 ` Markus Armbruster [this message]
2017-02-23 20:37 ` [Qemu-devel] [PULL 00/24] option cutils: Fix and clean up number conversions no-reply
2017-02-25 16:37 ` Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1487879642-16139-25-git-send-email-armbru@redhat.com \
    --to=armbru@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.