diff options
author | David Hildenbrand <david@redhat.com> | 2018-11-21 17:44:14 +0100 |
---|---|---|
committer | Markus Armbruster <armbru@redhat.com> | 2018-12-13 19:10:06 +0100 |
commit | af02f4c5179675ad4e26b17ba26694a8fcde17fa (patch) | |
tree | 7b316a4e89c962284f479f604f4b2515b4cc4f97 /util | |
parent | ca28f5481607e5c59481e70e429f5dd23662cb69 (diff) |
cutils: Fix qemu_strtosz() & friends to reject non-finite sizes
qemu_strtosz() & friends reject NaNs, but happily accept infinities.
They shouldn't. Fix that.
The fix makes use of qemu_strtod_finite(). To avoid ugly casts,
change the @end parameter of qemu_strtosz() & friends from char **
to const char **.
Also, add two test cases, testing that "inf" and "NaN" are properly
rejected. While at it, also fixup the function documentation.
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: David Hildenbrand <david@redhat.com>
Message-Id: <20181121164421.20780-3-david@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Diffstat (limited to 'util')
-rw-r--r-- | util/cutils.c | 18 |
1 files changed, 8 insertions, 10 deletions
diff --git a/util/cutils.c b/util/cutils.c index ec0a401d9a..e098debdc0 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -203,23 +203,21 @@ static int64_t suffix_mul(char suffix, int64_t unit) /* * Convert string to bytes, allowing either B/b for bytes, K/k for KB, * M/m for MB, G/g for GB or T/t for TB. End pointer will be returned - * in *end, if not NULL. Return -ERANGE on overflow, Return -EINVAL on + * in *end, if not NULL. Return -ERANGE on overflow, and -EINVAL on * other error. */ -static int do_strtosz(const char *nptr, char **end, +static int do_strtosz(const char *nptr, const char **end, const char default_suffix, int64_t unit, uint64_t *result) { int retval; - char *endptr; + const char *endptr; unsigned char c; int mul_required = 0; double val, mul, integral, fraction; - errno = 0; - val = strtod(nptr, &endptr); - if (isnan(val) || endptr == nptr || errno != 0) { - retval = -EINVAL; + retval = qemu_strtod_finite(nptr, &endptr, &val); + if (retval) { goto out; } fraction = modf(val, &integral); @@ -259,17 +257,17 @@ out: return retval; } -int qemu_strtosz(const char *nptr, char **end, uint64_t *result) +int qemu_strtosz(const char *nptr, const char **end, uint64_t *result) { return do_strtosz(nptr, end, 'B', 1024, result); } -int qemu_strtosz_MiB(const char *nptr, char **end, uint64_t *result) +int qemu_strtosz_MiB(const char *nptr, const char **end, uint64_t *result) { return do_strtosz(nptr, end, 'M', 1024, result); } -int qemu_strtosz_metric(const char *nptr, char **end, uint64_t *result) +int qemu_strtosz_metric(const char *nptr, const char **end, uint64_t *result) { return do_strtosz(nptr, end, 'B', 1000, result); } |