aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorEric Blake <eblake@redhat.com>2023-05-22 14:04:29 -0500
committerEric Blake <eblake@redhat.com>2023-06-02 12:27:19 -0500
commitbd1386cce1b184e4260721858d3bb4b4c888b5f0 (patch)
tree90edc96756aaea68bf85932405c4ca95459f0b36 /util
parent84760bbca9a4ef1bfb38b9c101a2604e5d429605 (diff)
cutils: Adjust signature of parse_uint[_full]
It's already confusing that we have two very similar functions for wrapping the parse of a 64-bit unsigned value, differing mainly on whether they permit leading '-'. Adjust the signature of parse_uint() and parse_uint_full() to be like all of qemu_strto*(): put the result parameter last, use the same types (uint64_t and unsigned long long have the same width, but are not always the same type), and mark endptr const (this latter change only affects the rare caller of parse_uint). Adjust all callers in the tree. While at it, note that since cutils.c already includes: QEMU_BUILD_BUG_ON(sizeof(int64_t) != sizeof(long long)); we are guaranteed that the result of parse_uint* cannot exceed UINT64_MAX (or the build would have failed), so we can drop pre-existing dead comparisons in opts-visitor.c that were never false. Reviewed-by: Hanna Czenczek <hreitz@redhat.com> Message-Id: <20230522190441.64278-8-eblake@redhat.com> [eblake: Drop dead code spotted by Markus] Signed-off-by: Eric Blake <eblake@redhat.com>
Diffstat (limited to 'util')
-rw-r--r--util/cutils.c13
-rw-r--r--util/guest-random.c4
-rw-r--r--util/qemu-sockets.c10
3 files changed, 13 insertions, 14 deletions
diff --git a/util/cutils.c b/util/cutils.c
index 36c14b769f..0e279a531a 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -722,10 +722,10 @@ const char *qemu_strchrnul(const char *s, int c)
* parse_uint:
*
* @s: String to parse
- * @value: Destination for parsed integer value
* @endptr: Destination for pointer to first character not consumed, must
* not be %NULL
* @base: integer base, between 2 and 36 inclusive, or 0
+ * @value: Destination for parsed integer value
*
* Parse unsigned integer
*
@@ -748,8 +748,7 @@ const char *qemu_strchrnul(const char *s, int c)
*
* Else, set *@value to the parsed integer, and return 0.
*/
-int parse_uint(const char *s, unsigned long long *value, char **endptr,
- int base)
+int parse_uint(const char *s, const char **endptr, int base, uint64_t *value)
{
int r = 0;
char *endp = (char *)s;
@@ -793,8 +792,8 @@ out:
* parse_uint_full:
*
* @s: String to parse
- * @value: Destination for parsed integer value
* @base: integer base, between 2 and 36 inclusive, or 0
+ * @value: Destination for parsed integer value
*
* Parse unsigned integer from entire string
*
@@ -803,12 +802,12 @@ out:
* 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)
+int parse_uint_full(const char *s, int base, uint64_t *value)
{
- char *endp;
+ const char *endp;
int r;
- r = parse_uint(s, value, &endp, base);
+ r = parse_uint(s, &endp, base, value);
if (r < 0) {
return r;
}
diff --git a/util/guest-random.c b/util/guest-random.c
index a24d27624c..9465dda085 100644
--- a/util/guest-random.c
+++ b/util/guest-random.c
@@ -89,8 +89,8 @@ void qemu_guest_random_seed_thread_part2(uint64_t seed)
int qemu_guest_random_seed_main(const char *optarg, Error **errp)
{
- unsigned long long seed;
- if (parse_uint_full(optarg, &seed, 0)) {
+ uint64_t seed;
+ if (parse_uint_full(optarg, 0, &seed)) {
error_setg(errp, "Invalid seed number: %s", optarg);
return -1;
} else {
diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c
index c06a4dce77..892d33f5e6 100644
--- a/util/qemu-sockets.c
+++ b/util/qemu-sockets.c
@@ -249,12 +249,12 @@ static int inet_listen_saddr(InetSocketAddress *saddr,
/* lookup */
if (port_offset) {
- unsigned long long baseport;
+ uint64_t baseport;
if (strlen(port) == 0) {
error_setg(errp, "port not specified");
return -1;
}
- if (parse_uint_full(port, &baseport, 10) < 0) {
+ if (parse_uint_full(port, 10, &baseport) < 0) {
error_setg(errp, "can't convert to a number: %s", port);
return -1;
}
@@ -732,19 +732,19 @@ static bool vsock_parse_vaddr_to_sockaddr(const VsockSocketAddress *vaddr,
struct sockaddr_vm *svm,
Error **errp)
{
- unsigned long long val;
+ uint64_t val;
memset(svm, 0, sizeof(*svm));
svm->svm_family = AF_VSOCK;
- if (parse_uint_full(vaddr->cid, &val, 10) < 0 ||
+ if (parse_uint_full(vaddr->cid, 10, &val) < 0 ||
val > UINT32_MAX) {
error_setg(errp, "Failed to parse cid '%s'", vaddr->cid);
return false;
}
svm->svm_cid = val;
- if (parse_uint_full(vaddr->port, &val, 10) < 0 ||
+ if (parse_uint_full(vaddr->port, 10, &val) < 0 ||
val > UINT32_MAX) {
error_setg(errp, "Failed to parse port '%s'", vaddr->port);
return false;