aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--hmp.c6
-rw-r--r--hw/misc/ivshmem.c7
-rw-r--r--include/qemu/cutils.h6
-rw-r--r--monitor.c5
-rw-r--r--qapi/opts-visitor.c5
-rw-r--r--qemu-img.c10
-rw-r--r--qemu-io-cmds.c10
-rw-r--r--target/i386/cpu.c5
-rw-r--r--tests/test-cutils.c120
-rw-r--r--util/cutils.c22
10 files changed, 119 insertions, 77 deletions
diff --git a/hmp.c b/hmp.c
index 2269e8b..d16761f 100644
--- a/hmp.c
+++ b/hmp.c
@@ -1348,7 +1348,7 @@ void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
long valueint = 0;
Error *err = NULL;
bool use_int_value = false;
- int i;
+ int i, ret;
for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
if (strcmp(param, MigrationParameter_lookup[i]) == 0) {
@@ -1384,8 +1384,8 @@ void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
break;
case MIGRATION_PARAMETER_MAX_BANDWIDTH:
p.has_max_bandwidth = true;
- valuebw = qemu_strtosz_MiB(valuestr, NULL);
- if (valuebw < 0 || (size_t)valuebw != valuebw) {
+ ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
+ if (ret < 0 || (size_t)valuebw != valuebw) {
error_setg(&err, "Invalid size %s", valuestr);
goto cleanup;
}
diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c
index 848bebc..80856fd 100644
--- a/hw/misc/ivshmem.c
+++ b/hw/misc/ivshmem.c
@@ -1267,8 +1267,11 @@ static void ivshmem_realize(PCIDevice *dev, Error **errp)
if (s->sizearg == NULL) {
s->legacy_size = 4 << 20; /* 4 MB default */
} else {
- int64_t size = qemu_strtosz_MiB(s->sizearg, NULL);
- if (size < 0 || (size_t)size != size || !is_power_of_2(size)) {
+ int ret;
+ int64_t size;
+
+ ret = qemu_strtosz_MiB(s->sizearg, NULL, &size);
+ if (ret < 0 || (size_t)size != size || !is_power_of_2(size)) {
error_setg(errp, "Invalid size %s", s->sizearg);
return;
}
diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h
index 3fdbb6b..4c68d5c 100644
--- a/include/qemu/cutils.h
+++ b/include/qemu/cutils.h
@@ -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);
-int64_t qemu_strtosz(const char *nptr, char **end);
-int64_t qemu_strtosz_MiB(const char *nptr, char **end);
-int64_t qemu_strtosz_metric(const char *nptr, char **end);
+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);
#define K_BYTE (1ULL << 10)
#define M_BYTE (1ULL << 20)
diff --git a/monitor.c b/monitor.c
index c2c1e42..8baeaf0 100644
--- a/monitor.c
+++ b/monitor.c
@@ -2799,6 +2799,7 @@ static QDict *monitor_parse_arguments(Monitor *mon,
break;
case 'o':
{
+ int ret;
int64_t val;
char *end;
@@ -2811,8 +2812,8 @@ static QDict *monitor_parse_arguments(Monitor *mon,
break;
}
}
- val = qemu_strtosz_MiB(p, &end);
- if (val < 0) {
+ ret = qemu_strtosz_MiB(p, &end, &val);
+ if (ret < 0) {
monitor_printf(mon, "invalid size\n");
goto fail;
}
diff --git a/qapi/opts-visitor.c b/qapi/opts-visitor.c
index 911a0ee..aac2e09 100644
--- a/qapi/opts-visitor.c
+++ b/qapi/opts-visitor.c
@@ -482,14 +482,15 @@ 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;
}
- val = qemu_strtosz(opt->str ? opt->str : "", NULL);
- if (val < 0) {
+ err = qemu_strtosz(opt->str ? opt->str : "", NULL, &val);
+ if (err < 0) {
error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
"a size value representible as a non-negative int64");
return;
diff --git a/qemu-img.c b/qemu-img.c
index 39ef581..37d1afb 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -370,10 +370,14 @@ static int add_old_style_options(const char *fmt, QemuOpts *opts,
static int64_t cvtnum(const char *s)
{
- int64_t ret;
+ int err;
+ int64_t value;
- ret = qemu_strtosz(s, NULL);
- return ret;
+ err = qemu_strtosz(s, NULL, &value);
+ if (err < 0) {
+ return err;
+ }
+ return value;
}
static int img_create(int argc, char **argv)
diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c
index 973eb94..01a6dc6 100644
--- a/qemu-io-cmds.c
+++ b/qemu-io-cmds.c
@@ -137,10 +137,14 @@ static char **breakline(char *input, int *count)
static int64_t cvtnum(const char *s)
{
- int64_t ret;
+ int err;
+ int64_t value;
- ret = qemu_strtosz(s, NULL);
- return ret;
+ err = qemu_strtosz(s, NULL, &value);
+ if (err < 0) {
+ return err;
+ }
+ return value;
}
static void print_cvtnum_err(int64_t rc, const char *arg)
diff --git a/target/i386/cpu.c b/target/i386/cpu.c
index 58a05ec..d32af97 100644
--- a/target/i386/cpu.c
+++ b/target/i386/cpu.c
@@ -2033,10 +2033,11 @@ static void x86_cpu_parse_featurestr(const char *typename, char *features,
/* Special case: */
if (!strcmp(name, "tsc-freq")) {
+ int ret;
int64_t tsc_freq;
- tsc_freq = qemu_strtosz_metric(val, NULL);
- if (tsc_freq < 0) {
+ ret = qemu_strtosz_metric(val, NULL, &tsc_freq);
+ if (ret < 0) {
error_setg(errp, "bad numerical value %s", val);
return;
}
diff --git a/tests/test-cutils.c b/tests/test-cutils.c
index f2ecb7a..d0137de 100644
--- a/tests/test-cutils.c
+++ b/tests/test-cutils.c
@@ -1373,45 +1373,54 @@ static void test_qemu_strtosz_simple(void)
{
const char *str;
char *endptr = NULL;
- int64_t res;
+ int err;
+ int64_t res = 0xbaadf00d;
str = "0";
- res = qemu_strtosz(str, &endptr);
+ err = qemu_strtosz(str, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 0);
g_assert(endptr == str + 1);
str = "12345";
- res = qemu_strtosz(str, &endptr);
+ err = qemu_strtosz(str, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 12345);
g_assert(endptr == str + 5);
- res = qemu_strtosz(str, NULL);
+ err = qemu_strtosz(str, NULL, &res);
+ g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 12345);
/* Note: precision is 53 bits since we're parsing with strtod() */
str = "9007199254740991"; /* 2^53-1 */
- res = qemu_strtosz(str, &endptr);
+ err = qemu_strtosz(str, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 0x1fffffffffffff);
g_assert(endptr == str + 16);
str = "9007199254740992"; /* 2^53 */
- res = qemu_strtosz(str, &endptr);
+ err = qemu_strtosz(str, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 0x20000000000000);
g_assert(endptr == str + 16);
str = "9007199254740993"; /* 2^53+1 */
- res = qemu_strtosz(str, &endptr);
+ err = qemu_strtosz(str, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 0x20000000000000); /* rounded to 53 bits */
g_assert(endptr == str + 16);
str = "9223372036854774784"; /* 0x7ffffffffffffc00 (53 msbs set) */
- res = qemu_strtosz(str, &endptr);
+ err = qemu_strtosz(str, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 0x7ffffffffffffc00);
g_assert(endptr == str + 19);
str = "9223372036854775295"; /* 0x7ffffffffffffdff */
- res = qemu_strtosz(str, &endptr);
+ 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);
@@ -1429,39 +1438,48 @@ static void test_qemu_strtosz_units(void)
const char *t = "1T";
const char *p = "1P";
const char *e = "1E";
+ int err;
char *endptr = NULL;
- int64_t res;
+ int64_t res = 0xbaadf00d;
/* default is M */
- res = qemu_strtosz_MiB(none, &endptr);
+ err = qemu_strtosz_MiB(none, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, M_BYTE);
g_assert(endptr == none + 1);
- res = qemu_strtosz(b, &endptr);
+ err = qemu_strtosz(b, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 1);
g_assert(endptr == b + 2);
- res = qemu_strtosz(k, &endptr);
+ err = qemu_strtosz(k, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, K_BYTE);
g_assert(endptr == k + 2);
- res = qemu_strtosz(m, &endptr);
+ err = qemu_strtosz(m, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, M_BYTE);
g_assert(endptr == m + 2);
- res = qemu_strtosz(g, &endptr);
+ err = qemu_strtosz(g, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, G_BYTE);
g_assert(endptr == g + 2);
- res = qemu_strtosz(t, &endptr);
+ err = qemu_strtosz(t, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, T_BYTE);
g_assert(endptr == t + 2);
- res = qemu_strtosz(p, &endptr);
+ err = qemu_strtosz(p, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, P_BYTE);
g_assert(endptr == p + 2);
- res = qemu_strtosz(e, &endptr);
+ err = qemu_strtosz(e, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, E_BYTE);
g_assert(endptr == e + 2);
}
@@ -1469,10 +1487,12 @@ static void test_qemu_strtosz_units(void)
static void test_qemu_strtosz_float(void)
{
const char *str = "12.345M";
+ int err;
char *endptr = NULL;
- int64_t res;
+ int64_t res = 0xbaadf00d;
- res = qemu_strtosz(str, &endptr);
+ err = qemu_strtosz(str, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 12.345 * M_BYTE);
g_assert(endptr == str + 7);
}
@@ -1481,21 +1501,22 @@ static void test_qemu_strtosz_invalid(void)
{
const char *str;
char *endptr = NULL;
- int64_t res;
+ int err;
+ int64_t res = 0xbaadf00d;
str = "";
- res = qemu_strtosz(str, &endptr);
- g_assert_cmpint(res, ==, -EINVAL);
+ err = qemu_strtosz(str, &endptr, &res);
+ g_assert_cmpint(err, ==, -EINVAL);
g_assert(endptr == str);
str = " \t ";
- res = qemu_strtosz(str, &endptr);
- g_assert_cmpint(res, ==, -EINVAL);
+ err = qemu_strtosz(str, &endptr, &res);
+ g_assert_cmpint(err, ==, -EINVAL);
g_assert(endptr == str);
str = "crap";
- res = qemu_strtosz(str, &endptr);
- g_assert_cmpint(res, ==, -EINVAL);
+ err = qemu_strtosz(str, &endptr, &res);
+ g_assert_cmpint(err, ==, -EINVAL);
g_assert(endptr == str);
}
@@ -1503,64 +1524,69 @@ static void test_qemu_strtosz_trailing(void)
{
const char *str;
char *endptr = NULL;
- int64_t res;
+ int err;
+ int64_t res = 0xbaadf00d;
str = "123xxx";
- res = qemu_strtosz_MiB(str, &endptr);
+ err = qemu_strtosz_MiB(str, &endptr, &res);
g_assert_cmpint(res, ==, 123 * M_BYTE);
g_assert(endptr == str + 3);
- res = qemu_strtosz(str, NULL);
- g_assert_cmpint(res, ==, -EINVAL);
+ err = qemu_strtosz(str, NULL, &res);
+ g_assert_cmpint(err, ==, -EINVAL);
str = "1kiB";
- res = qemu_strtosz(str, &endptr);
+ err = qemu_strtosz(str, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 1024);
g_assert(endptr == str + 2);
- res = qemu_strtosz(str, NULL);
- g_assert_cmpint(res, ==, -EINVAL);
+ err = qemu_strtosz(str, NULL, &res);
+ g_assert_cmpint(err, ==, -EINVAL);
}
static void test_qemu_strtosz_erange(void)
{
const char *str;
char *endptr = NULL;
- int64_t res;
+ int err;
+ int64_t res = 0xbaadf00d;
str = "-1";
- res = qemu_strtosz(str, &endptr);
- g_assert_cmpint(res, ==, -ERANGE);
+ err = qemu_strtosz(str, &endptr, &res);
+ g_assert_cmpint(err, ==, -ERANGE);
g_assert(endptr == str + 2);
str = "9223372036854775296"; /* 0x7ffffffffffffe00 */
- res = qemu_strtosz(str, &endptr);
- g_assert_cmpint(res, ==, -ERANGE);
+ err = qemu_strtosz(str, &endptr, &res);
+ g_assert_cmpint(err, ==, -ERANGE);
g_assert(endptr == str + 19);
str = "9223372036854775807"; /* 2^63-1 */
- res = qemu_strtosz(str, &endptr);
- g_assert_cmpint(res, ==, -ERANGE);
+ err = qemu_strtosz(str, &endptr, &res);
+ g_assert_cmpint(err, ==, -ERANGE);
g_assert(endptr == str + 19);
str = "9223372036854775808"; /* 2^63 */
- res = qemu_strtosz(str, &endptr);
- g_assert_cmpint(res, ==, -ERANGE);
+ err = qemu_strtosz(str, &endptr, &res);
+ g_assert_cmpint(err, ==, -ERANGE);
g_assert(endptr == str + 19);
str = "10E";
- res = qemu_strtosz(str, &endptr);
- g_assert_cmpint(res, ==, -ERANGE);
+ err = qemu_strtosz(str, &endptr, &res);
+ g_assert_cmpint(err, ==, -ERANGE);
g_assert(endptr == str + 3);
}
static void test_qemu_strtosz_metric(void)
{
const char *str = "12345k";
+ int err;
char *endptr = NULL;
- int64_t res;
+ int64_t res = 0xbaadf00d;
- res = qemu_strtosz_metric(str, &endptr);
+ err = qemu_strtosz_metric(str, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 12345000);
g_assert(endptr == str + 6);
}
diff --git a/util/cutils.c b/util/cutils.c
index b991623..7088ddc 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -205,10 +205,11 @@ static int64_t suffix_mul(char suffix, int64_t unit)
* in *end, if not NULL. Return -ERANGE on overflow, Return -EINVAL on
* other error.
*/
-static int64_t do_strtosz(const char *nptr, char **end,
- const char default_suffix, int64_t unit)
+static int do_strtosz(const char *nptr, char **end,
+ const char default_suffix, int64_t unit,
+ int64_t *result)
{
- int64_t retval;
+ int retval;
char *endptr;
unsigned char c;
int mul_required = 0;
@@ -240,7 +241,8 @@ static int64_t do_strtosz(const char *nptr, char **end,
retval = -ERANGE;
goto out;
}
- retval = val * mul;
+ *result = val * mul;
+ retval = 0;
out:
if (end) {
@@ -252,19 +254,19 @@ out:
return retval;
}
-int64_t qemu_strtosz(const char *nptr, char **end)
+int qemu_strtosz(const char *nptr, char **end, int64_t *result)
{
- return do_strtosz(nptr, end, 'B', 1024);
+ return do_strtosz(nptr, end, 'B', 1024, result);
}
-int64_t qemu_strtosz_MiB(const char *nptr, char **end)
+int qemu_strtosz_MiB(const char *nptr, char **end, int64_t *result)
{
- return do_strtosz(nptr, end, 'M', 1024);
+ return do_strtosz(nptr, end, 'M', 1024, result);
}
-int64_t qemu_strtosz_metric(const char *nptr, char **end)
+int qemu_strtosz_metric(const char *nptr, char **end, int64_t *result)
{
- return do_strtosz(nptr, end, 'B', 1000);
+ return do_strtosz(nptr, end, 'B', 1000, result);
}
/**