Re: [PATCH v2 07/19] cutils: Adjust signature of parse_uint[_full]

2023-05-19 Thread Hanna Czenczek

On 12.05.23 04:10, Eric Blake wrote:

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 is not always the same as
unsigned long long, and mark endptr const (only latter affects the
rare caller of parse_uint).  Adjust all callers in the tree.

Signed-off-by: Eric Blake 
---
  include/qemu/cutils.h |   5 +-
  audio/audio_legacy.c  |   4 +-
  block/gluster.c   |   4 +-
  block/nfs.c   |   4 +-
  blockdev.c|   4 +-
  contrib/ivshmem-server/main.c |   4 +-
  qapi/opts-visitor.c   |  10 +--
  tests/unit/test-cutils.c  | 113 +++---
  ui/vnc.c  |   4 +-
  util/cutils.c |  13 ++--
  util/guest-random.c   |   4 +-
  util/qemu-sockets.c   |  10 +--
  12 files changed, 82 insertions(+), 97 deletions(-)


[...]


diff --git a/tests/unit/test-cutils.c b/tests/unit/test-cutils.c
index 08989d1d3ac..0c7d07b3297 100644
--- a/tests/unit/test-cutils.c
+++ b/tests/unit/test-cutils.c


[...]


@@ -186,32 +176,31 @@ static void test_parse_uint_max(void)

  static void test_parse_uint_overflow(void)
  {
-unsigned long long i;
-char f = 'X';
-char *endptr;
+uint64_t i;
+const char *endptr = "somewhere";


The initialization here is technically not necessary because it’s reset 
above the parse_uint() call below.


Anyway:

Reviewed-by: Hanna Czenczek 


  const char *str;
  int r;

  i = 999;
-endptr = &f;
+endptr = "somewhere";
  str = "99";
-r = parse_uint(str, &i, &endptr, 0);
+r = parse_uint(str, &endptr, 0, &i);
  g_assert_cmpint(r, ==, -ERANGE);
  g_assert_cmpuint(i, ==, ULLONG_MAX);
  g_assert_true(endptr == str + strlen(str));





Re: [PATCH v2 07/19] cutils: Adjust signature of parse_uint[_full]

2023-05-12 Thread Eric Blake


On Thu, May 11, 2023 at 09:10:21PM -0500, Eric Blake wrote:
> 
> 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 is not always the same as
> unsigned long long, and mark endptr const (only latter affects the

I blame my late night editing.  Looks better as:

...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.
> 
> Signed-off-by: Eric Blake 
> ---
>  include/qemu/cutils.h |   5 +-
>  audio/audio_legacy.c  |   4 +-
>  block/gluster.c   |   4 +-
>  block/nfs.c   |   4 +-
>  blockdev.c|   4 +-
>  contrib/ivshmem-server/main.c |   4 +-
>  qapi/opts-visitor.c   |  10 +--
>  tests/unit/test-cutils.c  | 113 +++---
>  ui/vnc.c  |   4 +-
>  util/cutils.c |  13 ++--
>  util/guest-random.c   |   4 +-
>  util/qemu-sockets.c   |  10 +--
>  12 files changed, 82 insertions(+), 97 deletions(-)
> 

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.   +1-919-301-3266
Virtualization:  qemu.org | libvirt.org




[PATCH v2 07/19] cutils: Adjust signature of parse_uint[_full]

2023-05-11 Thread Eric Blake
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 is not always the same as
unsigned long long, and mark endptr const (only latter affects the
rare caller of parse_uint).  Adjust all callers in the tree.

Signed-off-by: Eric Blake 
---
 include/qemu/cutils.h |   5 +-
 audio/audio_legacy.c  |   4 +-
 block/gluster.c   |   4 +-
 block/nfs.c   |   4 +-
 blockdev.c|   4 +-
 contrib/ivshmem-server/main.c |   4 +-
 qapi/opts-visitor.c   |  10 +--
 tests/unit/test-cutils.c  | 113 +++---
 ui/vnc.c  |   4 +-
 util/cutils.c |  13 ++--
 util/guest-random.c   |   4 +-
 util/qemu-sockets.c   |  10 +--
 12 files changed, 82 insertions(+), 97 deletions(-)

diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h
index 92c436d8c70..92c927a6a35 100644
--- a/include/qemu/cutils.h
+++ b/include/qemu/cutils.h
@@ -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);
diff --git a/audio/audio_legacy.c b/audio/audio_legacy.c
index b848001ff70..dc72ba55e9a 100644
--- a/audio/audio_legacy.c
+++ b/audio/audio_legacy.c
@@ -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);
 }
diff --git a/block/gluster.c b/block/gluster.c
index 185a83e5e53..ad5fadbe793 100644
--- a/block/gluster.c
+++ b/block/gluster.c
@@ -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) {
@@ -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);
diff --git a/block/nfs.c b/block/nfs.c
index 006045d71a6..fabea0386a3 100644
--- a/block/nfs.c
+++ b/block/nfs.c
@@ -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;
diff --git a/blockdev.c b/blockdev.c
index d141ca7a2d5..162eaffae16 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -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 {
diff --git a/contrib/ivshmem-server/main.c b/contrib/ivshmem-server/main.c
index 224dbeb547e..5901f17707e 100644
-