util/cutils: Return qemu_strtosz*() error and value separately

This makes qemu_strtosz(), qemu_strtosz_mebi() and
qemu_strtosz_metric() similar to qemu_strtoi64(), except negative
values are rejected.

Backports commit f17fd4fdf0df3d2f3444399d04c38d22b9a3e1b7 from qemu
This commit is contained in:
Markus Armbruster 2018-03-02 08:57:14 -05:00 committed by Lioncash
parent 6093e67947
commit 8650d0213c
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7
3 changed files with 18 additions and 15 deletions

View file

@ -131,9 +131,9 @@ int qemu_strtoi64(const char *nptr, const char **endptr, int base,
int qemu_strtou64(const char *nptr, const char **endptr, int base, int qemu_strtou64(const char *nptr, const char **endptr, int base,
uint64_t *result); uint64_t *result);
int64_t qemu_strtosz(const char *nptr, char **end); int qemu_strtosz(const char *nptr, char **end, int64_t *result);
int64_t qemu_strtosz_MiB(const char *nptr, char **end); int qemu_strtosz_MiB(const char *nptr, char **end, int64_t *result);
int64_t qemu_strtosz_metric(const char *nptr, char **end); int qemu_strtosz_metric(const char *nptr, char **end, int64_t *result);
#define K_BYTE (1ULL << 10) #define K_BYTE (1ULL << 10)
#define M_BYTE (1ULL << 20) #define M_BYTE (1ULL << 20)

View file

@ -2181,10 +2181,11 @@ static void x86_cpu_parse_featurestr(CPUState *cs, char *features,
/* Special case: */ /* Special case: */
if (!strcmp(name, "tsc-freq")) { if (!strcmp(name, "tsc-freq")) {
int ret;
int64_t tsc_freq; int64_t tsc_freq;
tsc_freq = qemu_strtosz_metric(val, NULL); ret = qemu_strtosz_metric(val, NULL, &tsc_freq);
if (tsc_freq < 0) { if (ret < 0) {
error_setg(errp, "bad numerical value %s", val); error_setg(errp, "bad numerical value %s", val);
return; return;
} }

View file

@ -154,10 +154,11 @@ static int64_t suffix_mul(char suffix, int64_t unit)
* in *end, if not NULL. Return -ERANGE on overflow, Return -EINVAL on * in *end, if not NULL. Return -ERANGE on overflow, Return -EINVAL on
* other error. * other error.
*/ */
static int64_t do_strtosz(const char *nptr, char **end, static int do_strtosz(const char *nptr, char **end,
const char default_suffix, int64_t unit) const char default_suffix, int64_t unit,
int64_t *result)
{ {
int64_t retval; int retval;
char *endptr; char *endptr;
unsigned char c; unsigned char c;
int mul_required = 0; int mul_required = 0;
@ -189,7 +190,8 @@ static int64_t do_strtosz(const char *nptr, char **end,
retval = -ERANGE; retval = -ERANGE;
goto out; goto out;
} }
retval = (int64_t)(val * mul); *result = (int64_t)(val * mul);
retval = 0;
out: out:
if (end) { if (end) {
@ -201,19 +203,19 @@ out:
return retval; 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);
} }
/** /**