Skip to content

Commit

Permalink
cutils: Document differences between parse_uint and qemu_strtou64
Browse files Browse the repository at this point in the history
These two functions are subtly different, and not just because of
swapped parameter order.  It took me adding better unit tests to
figure out why.  Document the differences to make it more obvious to
developers trying to pick which one to use, as well as to aid in
upcoming semantic changes.

While touching the documentation, adjust a mis-statement: parse_uint
does not return -EINVAL on invalid base, but assert()s, like all the
other qemu_strto* functions that take a base argument.

Signed-off-by: Eric Blake <[email protected]>
Reviewed-by: Hanna Czenczek <[email protected]>
Message-Id: <[email protected]>
  • Loading branch information
ebblake committed Jun 2, 2023
1 parent 56ddafd commit 84760bb
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions util/cutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,8 @@ int qemu_strtoi64(const char *nptr, const char **endptr, int base,
* Convert string @nptr to an uint64_t.
*
* Works like qemu_strtoul(), except it stores UINT64_MAX on overflow.
* (If you want to prohibit negative numbers that wrap around to
* positive, use parse_uint()).
*/
int qemu_strtou64(const char *nptr, const char **endptr, int base,
uint64_t *result)
Expand Down Expand Up @@ -721,23 +723,25 @@ const char *qemu_strchrnul(const char *s, int c)
*
* @s: String to parse
* @value: Destination for parsed integer value
* @endptr: Destination for pointer to first character not consumed
* @endptr: Destination for pointer to first character not consumed, must
* not be %NULL
* @base: integer base, between 2 and 36 inclusive, or 0
*
* Parse unsigned integer
*
* Parsed syntax is like strtoull()'s: arbitrary whitespace, a single optional
* '+' or '-', an optional "0x" if @base is 0 or 16, one or more digits.
*
* If @s is null, or @base is invalid, or @s doesn't start with an
* integer in the syntax above, set *@value to 0, *@endptr to @s, and
* return -EINVAL.
* If @s is null, or @s doesn't start with an integer in the syntax
* above, set *@value to 0, *@endptr to @s, and return -EINVAL.
*
* Set *@endptr to point right beyond the parsed integer (even if the integer
* overflows or is negative, all digits will be parsed and *@endptr will
* point right beyond them).
*
* If the integer is negative, set *@value to 0, and return -ERANGE.
* (If you want to allow negative numbers that wrap around within
* bounds, use qemu_strtou64()).
*
* If the integer overflows unsigned long long, set *@value to
* ULLONG_MAX, and return -ERANGE.
Expand Down Expand Up @@ -794,10 +798,10 @@ int parse_uint(const char *s, unsigned long long *value, char **endptr,
*
* Parse unsigned integer from entire string
*
* Have the same behavior of parse_uint(), but with an additional check
* for additional data after the parsed number. If extra characters are present
* after the parsed number, the function will return -EINVAL, and *@v will
* be set to 0.
* Have the same behavior of parse_uint(), but with an additional
* check for additional data after the parsed number. If extra
* characters are present after a non-overflowing parsed number, the
* function will return -EINVAL, and *@v will be set to 0.
*/
int parse_uint_full(const char *s, unsigned long long *value, int base)
{
Expand Down

0 comments on commit 84760bb

Please sign in to comment.