cutils: Use parse_uint in qemu_strtosz for negative rejection

Rather than open-coding two different ways to check for an unwanted
negative sign, reuse the same code in both functions.  That way, if we
decide down the road to accept "-0" instead of rejecting it, we have
fewer places to change.  Also, it means we now get ERANGE instead of
EINVAL for negative values in qemu_strtosz, which is reasonable for
what it represents.  This in turn changes the expected output of a
couple of iotests.

The change is not quite complete: negative fractional scaled values
can trip us up.  This will be fixed in a later patch addressing other
issues with fractional scaled values.

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Hanna Czenczek <hreitz@redhat.com>
Message-Id: <20230522190441.64278-18-eblake@redhat.com>
This commit is contained in:
Eric Blake 2023-05-22 14:04:39 -05:00
parent 3c5f246798
commit b87ac96651
5 changed files with 9 additions and 19 deletions

View file

@ -201,6 +201,7 @@ static int64_t suffix_mul(char suffix, int64_t unit)
* - hex with scaling suffix, such as 0x20M
* - octal, such as 08
* - fractional hex, such as 0x1.8
* - negative values, including -0
* - floating point exponents, such as 1e3
*
* The end pointer will be returned in *end, if not NULL. If there is
@ -226,15 +227,10 @@ static int do_strtosz(const char *nptr, const char **end,
int64_t mul;
/* Parse integral portion as decimal. */
retval = qemu_strtou64(nptr, &endptr, 10, &val);
retval = parse_uint(nptr, &endptr, 10, &val);
if (retval) {
goto out;
}
if (memchr(nptr, '-', endptr - nptr) != NULL) {
endptr = nptr;
retval = -EINVAL;
goto out;
}
if (val == 0 && (*endptr == 'x' || *endptr == 'X')) {
/* Input looks like hex; reparse, and insist on no fraction or suffix. */
retval = qemu_strtou64(nptr, &endptr, 16, &val);