Skip to content

Commit

Permalink
util/cutils: Change qemu_strtosz*() from int64_t to uint64_t
Browse files Browse the repository at this point in the history
This will permit its use in parse_option_size().

Cc: Dr. David Alan Gilbert <dgilbert@redhat.com>
Cc: Eduardo Habkost <ehabkost@redhat.com> (maintainer:X86)
Cc: Kevin Wolf <kwolf@redhat.com> (supporter:Block layer core)
Cc: Max Reitz <mreitz@redhat.com> (supporter:Block layer core)
Cc: qemu-block@nongnu.org (open list:Block layer core)
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Message-Id: <1487708048-2131-24-git-send-email-armbru@redhat.com>
  • Loading branch information
Markus Armbruster committed Feb 23, 2017
1 parent f17fd4f commit f46bfdb
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 41 deletions.
5 changes: 3 additions & 2 deletions hmp.c
Expand Up @@ -1344,7 +1344,7 @@ void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
{
const char *param = qdict_get_str(qdict, "parameter");
const char *valuestr = qdict_get_str(qdict, "value");
int64_t valuebw = 0;
uint64_t valuebw = 0;
long valueint = 0;
Error *err = NULL;
bool use_int_value = false;
Expand Down Expand Up @@ -1385,7 +1385,8 @@ void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
case MIGRATION_PARAMETER_MAX_BANDWIDTH:
p.has_max_bandwidth = true;
ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
if (ret < 0 || (size_t)valuebw != valuebw) {
if (ret < 0 || valuebw > INT64_MAX
|| (size_t)valuebw != valuebw) {
error_setg(&err, "Invalid size %s", valuestr);
goto cleanup;
}
Expand Down
2 changes: 1 addition & 1 deletion hw/misc/ivshmem.c
Expand Up @@ -1268,7 +1268,7 @@ static void ivshmem_realize(PCIDevice *dev, Error **errp)
s->legacy_size = 4 << 20; /* 4 MB default */
} else {
int ret;
int64_t size;
uint64_t size;

ret = qemu_strtosz_MiB(s->sizearg, NULL, &size);
if (ret < 0 || (size_t)size != size || !is_power_of_2(size)) {
Expand Down
6 changes: 3 additions & 3 deletions include/qemu/cutils.h
Expand Up @@ -139,9 +139,9 @@ int parse_uint(const char *s, unsigned long long *value, char **endptr,
int base);
int parse_uint_full(const char *s, unsigned long long *value, int base);

int qemu_strtosz(const char *nptr, char **end, int64_t *result);
int qemu_strtosz_MiB(const char *nptr, char **end, int64_t *result);
int qemu_strtosz_metric(const char *nptr, char **end, int64_t *result);
int qemu_strtosz(const char *nptr, char **end, uint64_t *result);
int qemu_strtosz_MiB(const char *nptr, char **end, uint64_t *result);
int qemu_strtosz_metric(const char *nptr, char **end, uint64_t *result);

#define K_BYTE (1ULL << 10)
#define M_BYTE (1ULL << 20)
Expand Down
4 changes: 2 additions & 2 deletions monitor.c
Expand Up @@ -2800,7 +2800,7 @@ static QDict *monitor_parse_arguments(Monitor *mon,
case 'o':
{
int ret;
int64_t val;
uint64_t val;
char *end;

while (qemu_isspace(*p)) {
Expand All @@ -2813,7 +2813,7 @@ static QDict *monitor_parse_arguments(Monitor *mon,
}
}
ret = qemu_strtosz_MiB(p, &end, &val);
if (ret < 0) {
if (ret < 0 || val > INT64_MAX) {
monitor_printf(mon, "invalid size\n");
goto fail;
}
Expand Down
6 changes: 2 additions & 4 deletions qapi/opts-visitor.c
Expand Up @@ -481,22 +481,20 @@ opts_type_size(Visitor *v, const char *name, uint64_t *obj, Error **errp)
{
OptsVisitor *ov = to_ov(v);
const QemuOpt *opt;
int64_t val;
int err;

opt = lookup_scalar(ov, name, errp);
if (!opt) {
return;
}

err = qemu_strtosz(opt->str ? opt->str : "", NULL, &val);
err = qemu_strtosz(opt->str ? opt->str : "", NULL, obj);
if (err < 0) {
error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
"a size value representible as a non-negative int64");
"a size value");
return;
}

*obj = val;
processed(ov, name);
}

Expand Down
5 changes: 4 additions & 1 deletion qemu-img.c
Expand Up @@ -371,12 +371,15 @@ static int add_old_style_options(const char *fmt, QemuOpts *opts,
static int64_t cvtnum(const char *s)
{
int err;
int64_t value;
uint64_t value;

err = qemu_strtosz(s, NULL, &value);
if (err < 0) {
return err;
}
if (value > INT64_MAX) {
return -ERANGE;
}
return value;
}

Expand Down
5 changes: 4 additions & 1 deletion qemu-io-cmds.c
Expand Up @@ -138,12 +138,15 @@ static char **breakline(char *input, int *count)
static int64_t cvtnum(const char *s)
{
int err;
int64_t value;
uint64_t value;

err = qemu_strtosz(s, NULL, &value);
if (err < 0) {
return err;
}
if (value > INT64_MAX) {
return -ERANGE;
}
return value;
}

Expand Down
4 changes: 2 additions & 2 deletions target/i386/cpu.c
Expand Up @@ -2034,10 +2034,10 @@ static void x86_cpu_parse_featurestr(const char *typename, char *features,
/* Special case: */
if (!strcmp(name, "tsc-freq")) {
int ret;
int64_t tsc_freq;
uint64_t tsc_freq;

ret = qemu_strtosz_metric(val, NULL, &tsc_freq);
if (ret < 0) {
if (ret < 0 || tsc_freq > INT64_MAX) {
error_setg(errp, "bad numerical value %s", val);
return;
}
Expand Down
40 changes: 20 additions & 20 deletions tests/test-cutils.c
Expand Up @@ -1374,7 +1374,7 @@ static void test_qemu_strtosz_simple(void)
const char *str;
char *endptr = NULL;
int err;
int64_t res = 0xbaadf00d;
uint64_t res = 0xbaadf00d;

str = "0";
err = qemu_strtosz(str, &endptr, &res);
Expand Down Expand Up @@ -1412,17 +1412,17 @@ static void test_qemu_strtosz_simple(void)
g_assert_cmpint(res, ==, 0x20000000000000); /* rounded to 53 bits */
g_assert(endptr == str + 16);

str = "9223372036854774784"; /* 0x7ffffffffffffc00 (53 msbs set) */
str = "18446744073709549568"; /* 0xfffffffffffff800 (53 msbs set) */
err = qemu_strtosz(str, &endptr, &res);
g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 0x7ffffffffffffc00);
g_assert(endptr == str + 19);
g_assert_cmpint(res, ==, 0xfffffffffffff800);
g_assert(endptr == str + 20);

str = "9223372036854775295"; /* 0x7ffffffffffffdff */
str = "18446744073709550591"; /* 0xfffffffffffffbff */
err = qemu_strtosz(str, &endptr, &res);
g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 0x7ffffffffffffc00); /* rounded to 53 bits */
g_assert(endptr == str + 19);
g_assert_cmpint(res, ==, 0xfffffffffffff800); /* rounded to 53 bits */
g_assert(endptr == str + 20);

/* 0x7ffffffffffffe00..0x7fffffffffffffff get rounded to
* 0x8000000000000000, thus -ERANGE; see test_qemu_strtosz_erange() */
Expand All @@ -1440,7 +1440,7 @@ static void test_qemu_strtosz_units(void)
const char *e = "1E";
int err;
char *endptr = NULL;
int64_t res = 0xbaadf00d;
uint64_t res = 0xbaadf00d;

/* default is M */
err = qemu_strtosz_MiB(none, &endptr, &res);
Expand Down Expand Up @@ -1489,7 +1489,7 @@ static void test_qemu_strtosz_float(void)
const char *str = "12.345M";
int err;
char *endptr = NULL;
int64_t res = 0xbaadf00d;
uint64_t res = 0xbaadf00d;

err = qemu_strtosz(str, &endptr, &res);
g_assert_cmpint(err, ==, 0);
Expand All @@ -1502,7 +1502,7 @@ static void test_qemu_strtosz_invalid(void)
const char *str;
char *endptr = NULL;
int err;
int64_t res = 0xbaadf00d;
uint64_t res = 0xbaadf00d;

str = "";
err = qemu_strtosz(str, &endptr, &res);
Expand All @@ -1525,7 +1525,7 @@ static void test_qemu_strtosz_trailing(void)
const char *str;
char *endptr = NULL;
int err;
int64_t res = 0xbaadf00d;
uint64_t res = 0xbaadf00d;

str = "123xxx";
err = qemu_strtosz_MiB(str, &endptr, &res);
Expand All @@ -1550,29 +1550,29 @@ static void test_qemu_strtosz_erange(void)
const char *str;
char *endptr = NULL;
int err;
int64_t res = 0xbaadf00d;
uint64_t res = 0xbaadf00d;

str = "-1";
err = qemu_strtosz(str, &endptr, &res);
g_assert_cmpint(err, ==, -ERANGE);
g_assert(endptr == str + 2);

str = "9223372036854775296"; /* 0x7ffffffffffffe00 */
str = "18446744073709550592"; /* 0xfffffffffffffc00 */
err = qemu_strtosz(str, &endptr, &res);
g_assert_cmpint(err, ==, -ERANGE);
g_assert(endptr == str + 19);
g_assert(endptr == str + 20);

str = "9223372036854775807"; /* 2^63-1 */
str = "18446744073709551615"; /* 2^64-1 */
err = qemu_strtosz(str, &endptr, &res);
g_assert_cmpint(err, ==, -ERANGE);
g_assert(endptr == str + 19);
g_assert(endptr == str + 20);

str = "9223372036854775808"; /* 2^63 */
str = "18446744073709551616"; /* 2^64 */
err = qemu_strtosz(str, &endptr, &res);
g_assert_cmpint(err, ==, -ERANGE);
g_assert(endptr == str + 19);
g_assert(endptr == str + 20);

str = "10E";
str = "20E";
err = qemu_strtosz(str, &endptr, &res);
g_assert_cmpint(err, ==, -ERANGE);
g_assert(endptr == str + 3);
Expand All @@ -1583,7 +1583,7 @@ static void test_qemu_strtosz_metric(void)
const char *str = "12345k";
int err;
char *endptr = NULL;
int64_t res = 0xbaadf00d;
uint64_t res = 0xbaadf00d;

err = qemu_strtosz_metric(str, &endptr, &res);
g_assert_cmpint(err, ==, 0);
Expand Down
14 changes: 9 additions & 5 deletions util/cutils.c
Expand Up @@ -207,7 +207,7 @@ static int64_t suffix_mul(char suffix, int64_t unit)
*/
static int do_strtosz(const char *nptr, char **end,
const char default_suffix, int64_t unit,
int64_t *result)
uint64_t *result)
{
int retval;
char *endptr;
Expand Down Expand Up @@ -237,7 +237,11 @@ static int do_strtosz(const char *nptr, char **end,
retval = -EINVAL;
goto out;
}
if ((val * mul >= INT64_MAX) || val < 0) {
/*
* Values >= 0xfffffffffffffc00 overflow uint64_t after their trip
* through double (53 bits of precision).
*/
if ((val * mul >= 0xfffffffffffffc00) || val < 0) {
retval = -ERANGE;
goto out;
}
Expand All @@ -254,17 +258,17 @@ static int do_strtosz(const char *nptr, char **end,
return retval;
}

int qemu_strtosz(const char *nptr, char **end, int64_t *result)
int qemu_strtosz(const char *nptr, char **end, uint64_t *result)
{
return do_strtosz(nptr, end, 'B', 1024, result);
}

int qemu_strtosz_MiB(const char *nptr, char **end, int64_t *result)
int qemu_strtosz_MiB(const char *nptr, char **end, uint64_t *result)
{
return do_strtosz(nptr, end, 'M', 1024, result);
}

int qemu_strtosz_metric(const char *nptr, char **end, int64_t *result)
int qemu_strtosz_metric(const char *nptr, char **end, uint64_t *result)
{
return do_strtosz(nptr, end, 'B', 1000, result);
}
Expand Down

0 comments on commit f46bfdb

Please sign in to comment.