Skip to content

Commit bd1386c

Browse files
committed
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 <[email protected]> Message-Id: <[email protected]> [eblake: Drop dead code spotted by Markus] Signed-off-by: Eric Blake <[email protected]>
1 parent 84760bb commit bd1386c

File tree

12 files changed

+86
-101
lines changed

12 files changed

+86
-101
lines changed

audio/audio_legacy.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535

3636
static uint32_t toui32(const char *str)
3737
{
38-
unsigned long long ret;
39-
if (parse_uint_full(str, &ret, 10) || ret > UINT32_MAX) {
38+
uint64_t ret;
39+
if (parse_uint_full(str, 10, &ret) || ret > UINT32_MAX) {
4040
dolog("Invalid integer value `%s'\n", str);
4141
exit(1);
4242
}

block/gluster.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ static struct glfs *qemu_gluster_glfs_init(BlockdevOptionsGluster *gconf,
424424
int ret;
425425
int old_errno;
426426
SocketAddressList *server;
427-
unsigned long long port;
427+
uint64_t port;
428428

429429
glfs = glfs_find_preopened(gconf->volume);
430430
if (glfs) {
@@ -445,7 +445,7 @@ static struct glfs *qemu_gluster_glfs_init(BlockdevOptionsGluster *gconf,
445445
server->value->u.q_unix.path, 0);
446446
break;
447447
case SOCKET_ADDRESS_TYPE_INET:
448-
if (parse_uint_full(server->value->u.inet.port, &port, 10) < 0 ||
448+
if (parse_uint_full(server->value->u.inet.port, 10, &port) < 0 ||
449449
port > 65535) {
450450
error_setg(errp, "'%s' is not a valid port number",
451451
server->value->u.inet.port);

block/nfs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ static int nfs_parse_uri(const char *filename, QDict *options, Error **errp)
114114
qdict_put_str(options, "path", uri->path);
115115

116116
for (i = 0; i < qp->n; i++) {
117-
unsigned long long val;
117+
uint64_t val;
118118
if (!qp->p[i].value) {
119119
error_setg(errp, "Value for NFS parameter expected: %s",
120120
qp->p[i].name);
121121
goto out;
122122
}
123-
if (parse_uint_full(qp->p[i].value, &val, 0)) {
123+
if (parse_uint_full(qp->p[i].value, 0, &val)) {
124124
error_setg(errp, "Illegal value for NFS parameter: %s",
125125
qp->p[i].name);
126126
goto out;

blockdev.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,10 @@ static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
341341
switch (qobject_type(entry->value)) {
342342

343343
case QTYPE_QSTRING: {
344-
unsigned long long length;
344+
uint64_t length;
345345
const char *str = qstring_get_str(qobject_to(QString,
346346
entry->value));
347-
if (parse_uint_full(str, &length, 10) == 0 &&
347+
if (parse_uint_full(str, 10, &length) == 0 &&
348348
length > 0 && length <= UINT_MAX) {
349349
block_acct_add_interval(stats, (unsigned) length);
350350
} else {

contrib/ivshmem-server/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static void
6969
ivshmem_server_parse_args(IvshmemServerArgs *args, int argc, char *argv[])
7070
{
7171
int c;
72-
unsigned long long v;
72+
uint64_t v;
7373
Error *err = NULL;
7474

7575
while ((c = getopt(argc, argv, "hvFp:S:m:M:l:n:")) != -1) {
@@ -112,7 +112,7 @@ ivshmem_server_parse_args(IvshmemServerArgs *args, int argc, char *argv[])
112112
break;
113113

114114
case 'n': /* number of vectors */
115-
if (parse_uint_full(optarg, &v, 0) < 0) {
115+
if (parse_uint_full(optarg, 0, &v) < 0) {
116116
fprintf(stderr, "cannot parse n_vectors\n");
117117
ivshmem_server_help(argv[0]);
118118
exit(1);

include/qemu/cutils.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,8 @@ int qemu_strtou64(const char *nptr, const char **endptr, int base,
163163
int qemu_strtod(const char *nptr, const char **endptr, double *result);
164164
int qemu_strtod_finite(const char *nptr, const char **endptr, double *result);
165165

166-
int parse_uint(const char *s, unsigned long long *value, char **endptr,
167-
int base);
168-
int parse_uint_full(const char *s, unsigned long long *value, int base);
166+
int parse_uint(const char *s, const char **endptr, int base, uint64_t *value);
167+
int parse_uint_full(const char *s, int base, uint64_t *value);
169168

170169
int qemu_strtosz(const char *nptr, const char **end, uint64_t *result);
171170
int qemu_strtosz_MiB(const char *nptr, const char **end, uint64_t *result);

qapi/opts-visitor.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,8 @@ opts_type_uint64(Visitor *v, const char *name, uint64_t *obj, Error **errp)
454454
OptsVisitor *ov = to_ov(v);
455455
const QemuOpt *opt;
456456
const char *str;
457-
unsigned long long val;
458-
char *endptr;
457+
uint64_t val;
458+
const char *endptr;
459459

460460
if (ov->list_mode == LM_UNSIGNED_INTERVAL) {
461461
*obj = ov->range_next.u;
@@ -471,18 +471,18 @@ opts_type_uint64(Visitor *v, const char *name, uint64_t *obj, Error **errp)
471471
/* we've gotten past lookup_scalar() */
472472
assert(ov->list_mode == LM_NONE || ov->list_mode == LM_IN_PROGRESS);
473473

474-
if (parse_uint(str, &val, &endptr, 0) == 0 && val <= UINT64_MAX) {
474+
if (parse_uint(str, &endptr, 0, &val) == 0) {
475475
if (*endptr == '\0') {
476476
*obj = val;
477477
processed(ov, name);
478478
return true;
479479
}
480480
if (*endptr == '-' && ov->list_mode == LM_IN_PROGRESS) {
481-
unsigned long long val2;
481+
uint64_t val2;
482482

483483
str = endptr + 1;
484-
if (parse_uint_full(str, &val2, 0) == 0 &&
485-
val2 <= UINT64_MAX && val <= val2 &&
484+
if (parse_uint_full(str, 0, &val2) == 0 &&
485+
val <= val2 &&
486486
val2 - val < OPTS_VISITOR_RANGE_MAX) {
487487
ov->range_next.u = val;
488488
ov->range_limit.u = val2;

0 commit comments

Comments
 (0)