yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-qemu_img-add-cvtnum_full-to-print-error-reports.patch

902636
From 1a8a4ece5def912e7cfa5ef8565fc8ecef6e72c3 Mon Sep 17 00:00:00 2001
902636
From: Eric Blake <eblake@redhat.com>
902636
Date: Tue, 2 Jun 2020 02:34:11 +0100
902636
Subject: [PATCH 06/26] qemu_img: add cvtnum_full to print error reports
902636
902636
RH-Author: Eric Blake <eblake@redhat.com>
902636
Message-id: <20200602023420.2133649-4-eblake@redhat.com>
902636
Patchwork-id: 97067
902636
O-Subject: [RHEL-AV-8.2.1 qemu-kvm PATCH 03/12] qemu_img: add cvtnum_full to print error reports
902636
Bugzilla: 1779893 1779904
902636
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
902636
RH-Acked-by: Max Reitz <mreitz@redhat.com>
902636
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
902636
902636
From: Eyal Moscovici <eyal.moscovici@oracle.com>
902636
902636
All calls to cvtnum check the return value and print the same error
902636
message more or less. And so error reporting moved to cvtnum_full to
902636
reduce code duplication and provide a single error
902636
message. Additionally, cvtnum now wraps cvtnum_full with the existing
902636
default range of 0 to MAX_INT64.
902636
902636
Acked-by: Mark Kanda <mark.kanda@oracle.com>
902636
Signed-off-by: Eyal Moscovici <eyal.moscovici@oracle.com>
902636
Message-Id: <20200513133629.18508-2-eyal.moscovici@oracle.com>
902636
Reviewed-by: Eric Blake <eblake@redhat.com>
902636
[eblake: fix printf formatting, avoid trailing space, change error wording,
902636
reformat commit message]
902636
Signed-off-by: Eric Blake <eblake@redhat.com>
902636
(cherry picked from commit 43d589b074370ebc9b340340b5f641b385da9df8)
902636
Signed-off-by: Eric Blake <eblake@redhat.com>
902636
902636
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
902636
---
902636
 qemu-img.c                 | 76 +++++++++++++++++++++-------------------------
902636
 tests/qemu-iotests/049.out |  8 ++---
902636
 2 files changed, 38 insertions(+), 46 deletions(-)
902636
902636
diff --git a/qemu-img.c b/qemu-img.c
902636
index 95a24b9..e69529b 100644
902636
--- a/qemu-img.c
902636
+++ b/qemu-img.c
902636
@@ -422,19 +422,31 @@ static int add_old_style_options(const char *fmt, QemuOpts *opts,
902636
     return 0;
902636
 }
902636
 
902636
-static int64_t cvtnum(const char *s)
902636
+static int64_t cvtnum_full(const char *name, const char *value, int64_t min,
902636
+                           int64_t max)
902636
 {
902636
     int err;
902636
-    uint64_t value;
902636
-
902636
-    err = qemu_strtosz(s, NULL, &value);
902636
-    if (err < 0) {
902636
+    uint64_t res;
902636
+
902636
+    err = qemu_strtosz(value, NULL, &res;;
902636
+    if (err < 0 && err != -ERANGE) {
902636
+        error_report("Invalid %s specified. You may use "
902636
+                     "k, M, G, T, P or E suffixes for", name);
902636
+        error_report("kilobytes, megabytes, gigabytes, terabytes, "
902636
+                     "petabytes and exabytes.");
902636
         return err;
902636
     }
902636
-    if (value > INT64_MAX) {
902636
+    if (err == -ERANGE || res > max || res < min) {
902636
+        error_report("Invalid %s specified. Must be between %" PRId64
902636
+                     " and %" PRId64 ".", name, min, max);
902636
         return -ERANGE;
902636
     }
902636
-    return value;
902636
+    return res;
902636
+}
902636
+
902636
+static int64_t cvtnum(const char *name, const char *value)
902636
+{
902636
+    return cvtnum_full(name, value, 0, INT64_MAX);
902636
 }
902636
 
902636
 static int img_create(int argc, char **argv)
902636
@@ -532,16 +544,8 @@ static int img_create(int argc, char **argv)
902636
     if (optind < argc) {
902636
         int64_t sval;
902636
 
902636
-        sval = cvtnum(argv[optind++]);
902636
+        sval = cvtnum("image size", argv[optind++]);
902636
         if (sval < 0) {
902636
-            if (sval == -ERANGE) {
902636
-                error_report("Image size must be less than 8 EiB!");
902636
-            } else {
902636
-                error_report("Invalid image size specified! You may use k, M, "
902636
-                      "G, T, P or E suffixes for ");
902636
-                error_report("kilobytes, megabytes, gigabytes, terabytes, "
902636
-                             "petabytes and exabytes.");
902636
-            }
902636
             goto fail;
902636
         }
902636
         img_size = (uint64_t)sval;
902636
@@ -2148,8 +2152,10 @@ static int img_convert(int argc, char **argv)
902636
         {
902636
             int64_t sval;
902636
 
902636
-            sval = cvtnum(optarg);
902636
-            if (sval < 0 || !QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) ||
902636
+            sval = cvtnum("buffer size for sparse output", optarg);
902636
+            if (sval < 0) {
902636
+                goto fail_getopt;
902636
+            } else if (!QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) ||
902636
                 sval / BDRV_SECTOR_SIZE > MAX_BUF_SECTORS) {
902636
                 error_report("Invalid buffer size for sparse output specified. "
902636
                     "Valid sizes are multiples of %llu up to %llu. Select "
902636
@@ -4229,9 +4235,8 @@ static int img_bench(int argc, char **argv)
902636
             break;
902636
         case 'o':
902636
         {
902636
-            offset = cvtnum(optarg);
902636
+            offset = cvtnum("offset", optarg);
902636
             if (offset < 0) {
902636
-                error_report("Invalid offset specified");
902636
                 return 1;
902636
             }
902636
             break;
902636
@@ -4244,9 +4249,8 @@ static int img_bench(int argc, char **argv)
902636
         {
902636
             int64_t sval;
902636
 
902636
-            sval = cvtnum(optarg);
902636
-            if (sval < 0 || sval > INT_MAX) {
902636
-                error_report("Invalid buffer size specified");
902636
+            sval = cvtnum_full("buffer size", optarg, 0, INT_MAX);
902636
+            if (sval < 0) {
902636
                 return 1;
902636
             }
902636
 
902636
@@ -4257,9 +4261,8 @@ static int img_bench(int argc, char **argv)
902636
         {
902636
             int64_t sval;
902636
 
902636
-            sval = cvtnum(optarg);
902636
-            if (sval < 0 || sval > INT_MAX) {
902636
-                error_report("Invalid step size specified");
902636
+            sval = cvtnum_full("step_size", optarg, 0, INT_MAX);
902636
+            if (sval < 0) {
902636
                 return 1;
902636
             }
902636
 
902636
@@ -4429,10 +4432,9 @@ static int img_dd_bs(const char *arg,
902636
 {
902636
     int64_t res;
902636
 
902636
-    res = cvtnum(arg);
902636
+    res = cvtnum_full("bs", arg, 1, INT_MAX);
902636
 
902636
-    if (res <= 0 || res > INT_MAX) {
902636
-        error_report("invalid number: '%s'", arg);
902636
+    if (res < 0) {
902636
         return 1;
902636
     }
902636
     in->bsz = out->bsz = res;
902636
@@ -4444,10 +4446,9 @@ static int img_dd_count(const char *arg,
902636
                         struct DdIo *in, struct DdIo *out,
902636
                         struct DdInfo *dd)
902636
 {
902636
-    dd->count = cvtnum(arg);
902636
+    dd->count = cvtnum("count", arg);
902636
 
902636
     if (dd->count < 0) {
902636
-        error_report("invalid number: '%s'", arg);
902636
         return 1;
902636
     }
902636
 
902636
@@ -4476,10 +4477,9 @@ static int img_dd_skip(const char *arg,
902636
                        struct DdIo *in, struct DdIo *out,
902636
                        struct DdInfo *dd)
902636
 {
902636
-    in->offset = cvtnum(arg);
902636
+    in->offset = cvtnum("skip", arg);
902636
 
902636
     if (in->offset < 0) {
902636
-        error_report("invalid number: '%s'", arg);
902636
         return 1;
902636
     }
902636
 
902636
@@ -4869,16 +4869,8 @@ static int img_measure(int argc, char **argv)
902636
         {
902636
             int64_t sval;
902636
 
902636
-            sval = cvtnum(optarg);
902636
+            sval = cvtnum("image size", optarg);
902636
             if (sval < 0) {
902636
-                if (sval == -ERANGE) {
902636
-                    error_report("Image size must be less than 8 EiB!");
902636
-                } else {
902636
-                    error_report("Invalid image size specified! You may use "
902636
-                                 "k, M, G, T, P or E suffixes for ");
902636
-                    error_report("kilobytes, megabytes, gigabytes, terabytes, "
902636
-                                 "petabytes and exabytes.");
902636
-                }
902636
                 goto out;
902636
             }
902636
             img_size = (uint64_t)sval;
902636
diff --git a/tests/qemu-iotests/049.out b/tests/qemu-iotests/049.out
902636
index 6b50540..8b35f3d 100644
902636
--- a/tests/qemu-iotests/049.out
902636
+++ b/tests/qemu-iotests/049.out
902636
@@ -92,19 +92,19 @@ Formatting 'TEST_DIR/t.qcow2', fmt=qcow2 size=1649267441664 cluster_size=65536 l
902636
 == 3. Invalid sizes ==
902636
 
902636
 qemu-img create -f qcow2 TEST_DIR/t.qcow2 -- -1024
902636
-qemu-img: Image size must be less than 8 EiB!
902636
+qemu-img: Invalid image size specified. Must be between 0 and 9223372036854775807.
902636
 
902636
 qemu-img create -f qcow2 -o size=-1024 TEST_DIR/t.qcow2
902636
 qemu-img: TEST_DIR/t.qcow2: Value '-1024' is out of range for parameter 'size'
902636
 
902636
 qemu-img create -f qcow2 TEST_DIR/t.qcow2 -- -1k
902636
-qemu-img: Image size must be less than 8 EiB!
902636
+qemu-img: Invalid image size specified. Must be between 0 and 9223372036854775807.
902636
 
902636
 qemu-img create -f qcow2 -o size=-1k TEST_DIR/t.qcow2
902636
 qemu-img: TEST_DIR/t.qcow2: Value '-1k' is out of range for parameter 'size'
902636
 
902636
 qemu-img create -f qcow2 TEST_DIR/t.qcow2 -- 1kilobyte
902636
-qemu-img: Invalid image size specified! You may use k, M, G, T, P or E suffixes for
902636
+qemu-img: Invalid image size specified. You may use k, M, G, T, P or E suffixes for
902636
 qemu-img: kilobytes, megabytes, gigabytes, terabytes, petabytes and exabytes.
902636
 
902636
 qemu-img create -f qcow2 -o size=1kilobyte TEST_DIR/t.qcow2
902636
@@ -113,7 +113,7 @@ Optional suffix k, M, G, T, P or E means kilo-, mega-, giga-, tera-, peta-
902636
 and exabytes, respectively.
902636
 
902636
 qemu-img create -f qcow2 TEST_DIR/t.qcow2 -- foobar
902636
-qemu-img: Invalid image size specified! You may use k, M, G, T, P or E suffixes for
902636
+qemu-img: Invalid image size specified. You may use k, M, G, T, P or E suffixes for
902636
 qemu-img: kilobytes, megabytes, gigabytes, terabytes, petabytes and exabytes.
902636
 
902636
 qemu-img create -f qcow2 -o size=foobar TEST_DIR/t.qcow2
902636
-- 
902636
1.8.3.1
902636