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

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