[PATCH 28/28] qemu-img: extend cvtnum() and use it in more places

Michael Tokarev posted 28 patches 9 months, 1 week ago
Maintainers: Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>
There is a newer version of this series
[PATCH 28/28] qemu-img: extend cvtnum() and use it in more places
Posted by Michael Tokarev 9 months, 1 week ago
cvtnum() expects input string to specify some sort of size
(optionally with KMG... suffix).  However, there are a lot
of other number conversions in there (using qemu_strtol &Co),
also, not all conversions which use cvtnum, actually expects
size, - like dd count=nn.

Add bool issize argument to cvtnum() to specify if it should
treat the argument as a size or something else, - this changes
conversion routine in use and error text.

Use the new cvtnum() in more places (like where strtol were used),
since it never return negative number in successful conversion.
When it makes sense, also specify upper or lower bounds at the
same time.  This simplifies option processing in multiple places,
removing the need of local temporary variables and longer error
reporting code.

While at it, fix errors, like depth in measure must be >= 1,
while the previous code allowed it to be 0.

In a few places, change unsigned variables (like of type size_t)
to be signed instead, - to avoid the need of temporary conversion
variable.  All these variables are okay to be signed, we never
assign <0 value to them except of the cases of conversion error,
where we return immediately.

Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
---
 qemu-img.c | 118 ++++++++++++++++++++---------------------------------
 1 file changed, 44 insertions(+), 74 deletions(-)

diff --git a/qemu-img.c b/qemu-img.c
index 299e34e470..a066c4cfc4 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -397,18 +397,23 @@ static int add_old_style_options(const char *fmt, QemuOpts *opts,
     return 0;
 }
 
-static int64_t cvtnum_full(const char *name, const char *value, int64_t min,
-                           int64_t max)
+static int64_t cvtnum_full(const char *name, const char *value,
+                           bool issize, int64_t min, int64_t max)
 {
     int err;
     uint64_t res;
 
-    err = qemu_strtosz(value, NULL, &res);
+    err = issize ? qemu_strtosz(value, NULL, &res) :
+                   qemu_strtou64(value, NULL, 0, &res);
     if (err < 0 && err != -ERANGE) {
-        error_report("Invalid %s specified. You may use "
-                     "k, M, G, T, P or E suffixes for", name);
-        error_report("kilobytes, megabytes, gigabytes, terabytes, "
-                     "petabytes and exabytes.");
+        if (issize) {
+            error_report("Invalid %s specified. You may use "
+                         "k, M, G, T, P or E suffixes for", name);
+            error_report("kilobytes, megabytes, gigabytes, terabytes, "
+                         "petabytes and exabytes.");
+        } else {
+            error_report("Invalid %s specified.", name);
+        }
         return err;
     }
     if (err == -ERANGE || res > max || res < min) {
@@ -419,9 +424,9 @@ static int64_t cvtnum_full(const char *name, const char *value, int64_t min,
     return res;
 }
 
-static int64_t cvtnum(const char *name, const char *value)
+static int64_t cvtnum(const char *name, const char *value, bool issize)
 {
-    return cvtnum_full(name, value, 0, INT64_MAX);
+    return cvtnum_full(name, value, issize, 0, INT64_MAX);
 }
 
 static int img_create(const img_cmd_t *ccmd, int argc, char **argv)
@@ -525,7 +530,7 @@ static int img_create(const img_cmd_t *ccmd, int argc, char **argv)
 
     /* Get image size, if specified */
     if (optind < argc) {
-        img_size = cvtnum("image size", argv[optind++]);
+        img_size = cvtnum("image size", argv[optind++], true);
         if (img_size < 0) {
             goto fail;
         }
@@ -987,7 +992,7 @@ static int img_commit(const img_cmd_t *ccmd, int argc, char **argv)
             quiet = true;
             break;
         case 'r':
-            rate_limit = cvtnum("rate limit", optarg);
+            rate_limit = cvtnum("rate limit", optarg, true);
             if (rate_limit < 0) {
                 return 1;
             }
@@ -2412,7 +2417,7 @@ static int img_convert(const img_cmd_t *ccmd, int argc, char **argv)
         {
             int64_t sval;
 
-            sval = cvtnum("buffer size for sparse output", optarg);
+            sval = cvtnum("buffer size for sparse output", optarg, true);
             if (sval < 0) {
                 goto fail_getopt;
             } else if (!QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) ||
@@ -2444,10 +2449,9 @@ static int img_convert(const img_cmd_t *ccmd, int argc, char **argv)
             skip_create = true;
             break;
         case 'm':
-            if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
-                s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
-                error_report("Invalid number of coroutines. Allowed number of"
-                             " coroutines is between 1 and %d", MAX_COROUTINES);
+            s.num_coroutines = cvtnum_full("number of coroutines", optarg,
+                                           false, 1, MAX_COROUTINES);
+            if (s.num_coroutines < 0) {
                 goto fail_getopt;
             }
             break;
@@ -2458,7 +2462,7 @@ static int img_convert(const img_cmd_t *ccmd, int argc, char **argv)
             force_share = true;
             break;
         case 'r':
-            rate_limit = cvtnum("rate limit", optarg);
+            rate_limit = cvtnum("rate limit", optarg, true);
             if (rate_limit < 0) {
                 goto fail_getopt;
             }
@@ -3377,13 +3381,13 @@ static int img_map(const img_cmd_t *ccmd, int argc, char **argv)
             output_format = parse_output_format(argv[0], optarg);
             break;
         case 's':
-            start_offset = cvtnum("start offset", optarg);
+            start_offset = cvtnum("start offset", optarg, true);
             if (start_offset < 0) {
                 return 1;
             }
             break;
         case 'l':
-            max_length = cvtnum("max length", optarg);
+            max_length = cvtnum("max length", optarg, true);
             if (max_length < 0) {
                 return 1;
             }
@@ -4704,9 +4708,9 @@ static int img_bench(const img_cmd_t *ccmd, int argc, char **argv)
     int count = 75000;
     int depth = 64;
     int64_t offset = 0;
-    size_t bufsize = 4096;
+    ssize_t bufsize = 4096;
     int pattern = 0;
-    size_t step = 0;
+    ssize_t step = 0;
     int flush_interval = 0;
     bool drain_on_flush = true;
     int64_t image_size;
@@ -4788,27 +4792,17 @@ static int img_bench(const img_cmd_t *ccmd, int argc, char **argv)
 );
             break;
         case 'c':
-        {
-            unsigned long res;
-
-            if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
-                error_report("Invalid request count specified");
+            count = cvtnum_full("request count", optarg, false, 1, INT_MAX);
+            if (count < 0) {
                 return 1;
             }
-            count = res;
             break;
-        }
         case 'd':
-        {
-            unsigned long res;
-
-            if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
-                error_report("Invalid queue depth specified");
+            depth = cvtnum_full("queue depth", optarg, false, 1, INT_MAX);
+            if (depth < 0) {
                 return 1;
             }
-            depth = res;
             break;
-        }
         case 'f':
             fmt = optarg;
             break;
@@ -4824,41 +4818,26 @@ static int img_bench(const img_cmd_t *ccmd, int argc, char **argv)
             }
             break;
         case 'o':
-        {
-            offset = cvtnum("offset", optarg);
+            offset = cvtnum("offset", optarg, true);
             if (offset < 0) {
                 return 1;
             }
             break;
-        }
-            break;
         case 'q':
             quiet = true;
             break;
         case 's':
-        {
-            int64_t sval;
-
-            sval = cvtnum_full("buffer size", optarg, 0, INT_MAX);
-            if (sval < 0) {
+            bufsize = cvtnum_full("buffer size", optarg, true, 1, INT_MAX);
+            if (bufsize < 0) {
                 return 1;
             }
-
-            bufsize = sval;
             break;
-        }
         case 'S':
-        {
-            int64_t sval;
-
-            sval = cvtnum_full("step_size", optarg, 0, INT_MAX);
-            if (sval < 0) {
+            step = cvtnum_full("step size", optarg, true, 0, INT_MAX);
+            if (step < 0) {
                 return 1;
             }
-
-            step = sval;
             break;
-        }
         case 't':
             ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
             if (ret < 0) {
@@ -4875,27 +4854,18 @@ static int img_bench(const img_cmd_t *ccmd, int argc, char **argv)
             force_share = true;
             break;
         case OPTION_PATTERN:
-        {
-            unsigned long res;
-
-            if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
-                error_report("Invalid pattern byte specified");
+            pattern = cvtnum_full("pattern byte", optarg, false, 0, 0xff);
+            if (pattern < 0) {
                 return 1;
             }
-            pattern = res;
             break;
-        }
         case OPTION_FLUSH_INTERVAL:
-        {
-            unsigned long res;
-
-            if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
-                error_report("Invalid flush interval specified");
+            flush_interval = cvtnum_full("flush interval", optarg,
+                                         false, 0, INT_MAX);
+            if (flush_interval < 0) {
                 return 1;
             }
-            flush_interval = res;
             break;
-        }
         case OPTION_NO_DRAIN:
             drain_on_flush = false;
             break;
@@ -5090,7 +5060,7 @@ static int img_bitmap(const img_cmd_t *ccmd, int argc, char **argv)
             src_fmt = optarg;
             break;
         case 'g':
-            granularity = cvtnum("granularity", optarg);
+            granularity = cvtnum("granularity", optarg, false);
             if (granularity < 0) {
                 return 1;
             }
@@ -5278,7 +5248,7 @@ static int img_dd_bs(const char *arg,
 {
     int64_t res;
 
-    res = cvtnum_full("bs", arg, 1, INT_MAX);
+    res = cvtnum_full("bs", arg, true, 1, INT_MAX);
 
     if (res < 0) {
         return 1;
@@ -5292,7 +5262,7 @@ static int img_dd_count(const char *arg,
                         struct DdIo *in, struct DdIo *out,
                         struct DdInfo *dd)
 {
-    dd->count = cvtnum("count", arg);
+    dd->count = cvtnum("count", arg, false);
 
     if (dd->count < 0) {
         return 1;
@@ -5323,7 +5293,7 @@ static int img_dd_skip(const char *arg,
                        struct DdIo *in, struct DdIo *out,
                        struct DdInfo *dd)
 {
-    in->offset = cvtnum("skip", arg);
+    in->offset = cvtnum("skip", arg, false);
 
     if (in->offset < 0) {
         return 1;
@@ -5718,7 +5688,7 @@ static int img_measure(const img_cmd_t *ccmd, int argc, char **argv)
             output_format = parse_output_format(argv[0], optarg);
             break;
         case 's':
-            img_size = cvtnum("image size", optarg);
+            img_size = cvtnum("image size", optarg, true);
             if (img_size < 0) {
                 goto out;
             }
-- 
2.39.2
Re: [PATCH 28/28] qemu-img: extend cvtnum() and use it in more places
Posted by Michael Tokarev 9 months ago
22.02.2024 00:16, Michael Tokarev wrote:

> -static int64_t cvtnum_full(const char *name, const char *value, int64_t min,
> -                           int64_t max)
> +static int64_t cvtnum_full(const char *name, const char *value,
> +                           bool issize, int64_t min, int64_t max)
>   {
>       int err;
>       uint64_t res;
>   
> -    err = qemu_strtosz(value, NULL, &res);
> +    err = issize ? qemu_strtosz(value, NULL, &res) :
> +                   qemu_strtou64(value, NULL, 0, &res);
>       if (err < 0 && err != -ERANGE) {
> -        error_report("Invalid %s specified. You may use "
> -                     "k, M, G, T, P or E suffixes for", name);
> -        error_report("kilobytes, megabytes, gigabytes, terabytes, "
> -                     "petabytes and exabytes.");
> +        if (issize) {
> +            error_report("Invalid %s specified. You may use "
> +                         "k, M, G, T, P or E suffixes for", name);
> +            error_report("kilobytes, megabytes, gigabytes, terabytes, "
> +                         "petabytes and exabytes.");
> +        } else {
> +            error_report("Invalid %s specified.", name);
> +        }

I've added actual value supplied to these error messages now.
And I think the list of possible suffixes makes little sense here.


> @@ -5090,7 +5060,7 @@ static int img_bitmap(const img_cmd_t *ccmd, int argc, char **argv)
>               src_fmt = optarg;
>               break;
>           case 'g':
> -            granularity = cvtnum("granularity", optarg);
> +            granularity = cvtnum("granularity", optarg, false);

Here, this is a size, so last arg should be true.  In the tests (190),
we already use -g 2M.  I didn't really knew what a granularity is while
converting it.

/mjt
Re: [PATCH 28/28] qemu-img: extend cvtnum() and use it in more places
Posted by Daniel P. Berrangé 9 months ago
On Thu, Feb 22, 2024 at 12:16:09AM +0300, Michael Tokarev wrote:
> cvtnum() expects input string to specify some sort of size
> (optionally with KMG... suffix).  However, there are a lot
> of other number conversions in there (using qemu_strtol &Co),
> also, not all conversions which use cvtnum, actually expects
> size, - like dd count=nn.
> 
> Add bool issize argument to cvtnum() to specify if it should
> treat the argument as a size or something else, - this changes
> conversion routine in use and error text.
> 
> Use the new cvtnum() in more places (like where strtol were used),
> since it never return negative number in successful conversion.
> When it makes sense, also specify upper or lower bounds at the
> same time.  This simplifies option processing in multiple places,
> removing the need of local temporary variables and longer error
> reporting code.
> 
> While at it, fix errors, like depth in measure must be >= 1,
> while the previous code allowed it to be 0.
> 
> In a few places, change unsigned variables (like of type size_t)
> to be signed instead, - to avoid the need of temporary conversion
> variable.  All these variables are okay to be signed, we never
> assign <0 value to them except of the cases of conversion error,
> where we return immediately.
> 
> Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
> ---
>  qemu-img.c | 118 ++++++++++++++++++++---------------------------------
>  1 file changed, 44 insertions(+), 74 deletions(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|