mirror of
https://github.com/yuzu-emu/unicorn.git
synced 2025-07-08 22:10:42 +00:00
util/cutils: Let qemu_strtosz*() optionally reject trailing crap
Change the qemu_strtosz() & friends to return -EINVAL when @endptr is null and the conversion doesn't consume the string completely. Matches how qemu_strtol() & friends work. Only test_qemu_strtosz_simple() passes a null @endptr. No functional change there, because its conversion consumes the string. Simplify callers that use @endptr only to fail when it doesn't point to '\0' to pass a null @endptr instead. Backports commit 4fcdf65ae2c00ae69f7625f26ed41f37d77b403c from qemu
This commit is contained in:
parent
f9c9eb7334
commit
6093e67947
|
@ -2182,10 +2182,9 @@ static void x86_cpu_parse_featurestr(CPUState *cs, char *features,
|
||||||
/* Special case: */
|
/* Special case: */
|
||||||
if (!strcmp(name, "tsc-freq")) {
|
if (!strcmp(name, "tsc-freq")) {
|
||||||
int64_t tsc_freq;
|
int64_t tsc_freq;
|
||||||
char *err;
|
|
||||||
|
|
||||||
tsc_freq = qemu_strtosz_metric(val, &err);
|
tsc_freq = qemu_strtosz_metric(val, NULL);
|
||||||
if (tsc_freq < 0 || *err) {
|
if (tsc_freq < 0) {
|
||||||
error_setg(errp, "bad numerical value %s", val);
|
error_setg(errp, "bad numerical value %s", val);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -157,7 +157,7 @@ static int64_t suffix_mul(char suffix, int64_t unit)
|
||||||
static int64_t do_strtosz(const char *nptr, char **end,
|
static int64_t do_strtosz(const char *nptr, char **end,
|
||||||
const char default_suffix, int64_t unit)
|
const char default_suffix, int64_t unit)
|
||||||
{
|
{
|
||||||
int64_t retval = -EINVAL;
|
int64_t retval;
|
||||||
char *endptr;
|
char *endptr;
|
||||||
unsigned char c;
|
unsigned char c;
|
||||||
int mul_required = 0;
|
int mul_required = 0;
|
||||||
|
@ -166,7 +166,8 @@ static int64_t do_strtosz(const char *nptr, char **end,
|
||||||
errno = 0;
|
errno = 0;
|
||||||
val = strtod(nptr, &endptr);
|
val = strtod(nptr, &endptr);
|
||||||
if (isnan(val) || endptr == nptr || errno != 0) {
|
if (isnan(val) || endptr == nptr || errno != 0) {
|
||||||
goto fail;
|
retval = -EINVAL;
|
||||||
|
goto out;
|
||||||
}
|
}
|
||||||
fraction = modf(val, &integral);
|
fraction = modf(val, &integral);
|
||||||
if (fraction != 0) {
|
if (fraction != 0) {
|
||||||
|
@ -181,17 +182,20 @@ static int64_t do_strtosz(const char *nptr, char **end,
|
||||||
assert(mul >= 0);
|
assert(mul >= 0);
|
||||||
}
|
}
|
||||||
if (mul == 1 && mul_required) {
|
if (mul == 1 && mul_required) {
|
||||||
goto fail;
|
retval = -EINVAL;
|
||||||
|
goto out;
|
||||||
}
|
}
|
||||||
if ((val * mul >= INT64_MAX) || val < 0) {
|
if ((val * mul >= INT64_MAX) || val < 0) {
|
||||||
retval = -ERANGE;
|
retval = -ERANGE;
|
||||||
goto fail;
|
goto out;
|
||||||
}
|
}
|
||||||
retval = (int64_t)(val * mul);
|
retval = (int64_t)(val * mul);
|
||||||
|
|
||||||
fail:
|
out:
|
||||||
if (end) {
|
if (end) {
|
||||||
*end = endptr;
|
*end = endptr;
|
||||||
|
} else if (*endptr) {
|
||||||
|
retval = -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
|
|
Loading…
Reference in a new issue