提交 75cdcd15 编写于 作者: M Markus Armbruster

option: Fix checking of sizes for overflow and trailing crap

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: NMarkus Armbruster <armbru@redhat.com>
Reviewed-by: NEric Blake <eblake@redhat.com>
Message-Id: <1487708048-2131-25-git-send-email-armbru@redhat.com>
上级 f46bfdbf
...@@ -702,10 +702,9 @@ static void test_opts_parse_size(void) ...@@ -702,10 +702,9 @@ static void test_opts_parse_size(void)
g_assert(!opts); g_assert(!opts);
opts = qemu_opts_parse(&opts_list_02, opts = qemu_opts_parse(&opts_list_02,
"size1=18446744073709550592", /* fffffffffffffc00 */ "size1=18446744073709550592", /* fffffffffffffc00 */
false, &error_abort); false, &err);
/* BUG: should reject */ error_free_or_abort(&err);
g_assert_cmpuint(opts_count(opts), ==, 1); g_assert(!opts);
g_assert_cmpuint(qemu_opt_get_size(opts, "size1", 1), ==, 0);
/* Suffixes */ /* Suffixes */
opts = qemu_opts_parse(&opts_list_02, "size1=8b,size2=1.5k,size3=2M", 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) ...@@ -723,19 +722,17 @@ static void test_opts_parse_size(void)
/* Beyond limit with suffix */ /* Beyond limit with suffix */
opts = qemu_opts_parse(&opts_list_02, "size1=16777216T", opts = qemu_opts_parse(&opts_list_02, "size1=16777216T",
false, &error_abort); false, &err);
/* BUG: should reject */ error_free_or_abort(&err);
g_assert_cmpuint(opts_count(opts), ==, 1); g_assert(!opts);
g_assert_cmpuint(qemu_opt_get_size(opts, "size1", 1), ==, 0);
/* Trailing crap */ /* Trailing crap */
opts = qemu_opts_parse(&opts_list_02, "size1=16E", false, &err); opts = qemu_opts_parse(&opts_list_02, "size1=16E", false, &err);
error_free_or_abort(&err); error_free_or_abort(&err);
g_assert(!opts); g_assert(!opts);
opts = qemu_opts_parse(&opts_list_02, "size1=16Gi", false, &error_abort); opts = qemu_opts_parse(&opts_list_02, "size1=16Gi", false, &err);
/* BUG: should reject */ error_free_or_abort(&err);
g_assert_cmpuint(opts_count(opts), ==, 1); g_assert(!opts);
g_assert_cmpuint(qemu_opt_get_size(opts, "size1", 1), ==, 16 * G_BYTE);
qemu_opts_reset(&opts_list_02); qemu_opts_reset(&opts_list_02);
} }
......
...@@ -174,39 +174,24 @@ static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc, ...@@ -174,39 +174,24 @@ static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
void parse_option_size(const char *name, const char *value, void parse_option_size(const char *name, const char *value,
uint64_t *ret, Error **errp) uint64_t *ret, Error **errp)
{ {
char *postfix; uint64_t size;
double sizef; int err;
sizef = strtod(value, &postfix); err = qemu_strtosz(value, NULL, &size);
if (sizef < 0 || sizef > UINT64_MAX) { if (err == -ERANGE) {
error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, error_setg(errp, "Value '%s' is too large for parameter '%s'",
"a non-negative number below 2^64"); value, name);
return; return;
} }
switch (*postfix) { if (err) {
case 'T': error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name,
sizef *= 1024; "a non-negative number below 2^64");
/* fall through */ error_append_hint(errp, "Optional suffix k, M, G, T, P or E means"
case 'G': " kilo-, mega-, giga-, tera-, peta-\n"
sizef *= 1024; "and exabytes, respectively.\n");
/* 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; return;
} }
*ret = size;
} }
bool has_help_option(const char *param) bool has_help_option(const char *param)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册