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

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