Skip to content

Commit

Permalink
cutils: Adjust signature of parse_uint[_full]
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ebblake committed Jun 2, 2023
1 parent 84760bb commit bd1386c
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 101 deletions.
4 changes: 2 additions & 2 deletions audio/audio_legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@

static uint32_t toui32(const char *str)
{
unsigned long long ret;
if (parse_uint_full(str, &ret, 10) || ret > UINT32_MAX) {
uint64_t ret;
if (parse_uint_full(str, 10, &ret) || ret > UINT32_MAX) {
dolog("Invalid integer value `%s'\n", str);
exit(1);
}
Expand Down
4 changes: 2 additions & 2 deletions block/gluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ static struct glfs *qemu_gluster_glfs_init(BlockdevOptionsGluster *gconf,
int ret;
int old_errno;
SocketAddressList *server;
unsigned long long port;
uint64_t port;

glfs = glfs_find_preopened(gconf->volume);
if (glfs) {
Expand All @@ -445,7 +445,7 @@ static struct glfs *qemu_gluster_glfs_init(BlockdevOptionsGluster *gconf,
server->value->u.q_unix.path, 0);
break;
case SOCKET_ADDRESS_TYPE_INET:
if (parse_uint_full(server->value->u.inet.port, &port, 10) < 0 ||
if (parse_uint_full(server->value->u.inet.port, 10, &port) < 0 ||
port > 65535) {
error_setg(errp, "'%s' is not a valid port number",
server->value->u.inet.port);
Expand Down
4 changes: 2 additions & 2 deletions block/nfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ static int nfs_parse_uri(const char *filename, QDict *options, Error **errp)
qdict_put_str(options, "path", uri->path);

for (i = 0; i < qp->n; i++) {
unsigned long long val;
uint64_t val;
if (!qp->p[i].value) {
error_setg(errp, "Value for NFS parameter expected: %s",
qp->p[i].name);
goto out;
}
if (parse_uint_full(qp->p[i].value, &val, 0)) {
if (parse_uint_full(qp->p[i].value, 0, &val)) {
error_setg(errp, "Illegal value for NFS parameter: %s",
qp->p[i].name);
goto out;
Expand Down
4 changes: 2 additions & 2 deletions blockdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,10 @@ static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
switch (qobject_type(entry->value)) {

case QTYPE_QSTRING: {
unsigned long long length;
uint64_t length;
const char *str = qstring_get_str(qobject_to(QString,
entry->value));
if (parse_uint_full(str, &length, 10) == 0 &&
if (parse_uint_full(str, 10, &length) == 0 &&
length > 0 && length <= UINT_MAX) {
block_acct_add_interval(stats, (unsigned) length);
} else {
Expand Down
4 changes: 2 additions & 2 deletions contrib/ivshmem-server/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static void
ivshmem_server_parse_args(IvshmemServerArgs *args, int argc, char *argv[])
{
int c;
unsigned long long v;
uint64_t v;
Error *err = NULL;

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

case 'n': /* number of vectors */
if (parse_uint_full(optarg, &v, 0) < 0) {
if (parse_uint_full(optarg, 0, &v) < 0) {
fprintf(stderr, "cannot parse n_vectors\n");
ivshmem_server_help(argv[0]);
exit(1);
Expand Down
5 changes: 2 additions & 3 deletions include/qemu/cutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,8 @@ int qemu_strtou64(const char *nptr, const char **endptr, int base,
int qemu_strtod(const char *nptr, const char **endptr, double *result);
int qemu_strtod_finite(const char *nptr, const char **endptr, double *result);

int parse_uint(const char *s, unsigned long long *value, char **endptr,
int base);
int parse_uint_full(const char *s, unsigned long long *value, int base);
int parse_uint(const char *s, const char **endptr, int base, uint64_t *value);
int parse_uint_full(const char *s, int base, uint64_t *value);

int qemu_strtosz(const char *nptr, const char **end, uint64_t *result);
int qemu_strtosz_MiB(const char *nptr, const char **end, uint64_t *result);
Expand Down
12 changes: 6 additions & 6 deletions qapi/opts-visitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,8 @@ opts_type_uint64(Visitor *v, const char *name, uint64_t *obj, Error **errp)
OptsVisitor *ov = to_ov(v);
const QemuOpt *opt;
const char *str;
unsigned long long val;
char *endptr;
uint64_t val;
const char *endptr;

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

if (parse_uint(str, &val, &endptr, 0) == 0 && val <= UINT64_MAX) {
if (parse_uint(str, &endptr, 0, &val) == 0) {
if (*endptr == '\0') {
*obj = val;
processed(ov, name);
return true;
}
if (*endptr == '-' && ov->list_mode == LM_IN_PROGRESS) {
unsigned long long val2;
uint64_t val2;

str = endptr + 1;
if (parse_uint_full(str, &val2, 0) == 0 &&
val2 <= UINT64_MAX && val <= val2 &&
if (parse_uint_full(str, 0, &val2) == 0 &&
val <= val2 &&
val2 - val < OPTS_VISITOR_RANGE_MAX) {
ov->range_next.u = val;
ov->range_limit.u = val2;
Expand Down
Loading

0 comments on commit bd1386c

Please sign in to comment.