34b321
From a5226789eaaedf06f50f2faf14b506c17deb5435 Mon Sep 17 00:00:00 2001
34b321
From: John Snow <jsnow@redhat.com>
34b321
Date: Mon, 23 Nov 2015 17:38:37 +0100
34b321
Subject: [PATCH 18/27] qemu-io: fix cvtnum lval types
34b321
34b321
RH-Author: John Snow <jsnow@redhat.com>
34b321
Message-id: <1448300320-7772-19-git-send-email-jsnow@redhat.com>
34b321
Patchwork-id: 68445
34b321
O-Subject: [RHEL-7.3 qemu-kvm PATCH v2 18/21] qemu-io: fix cvtnum lval types
34b321
Bugzilla: 1272523
34b321
RH-Acked-by: Thomas Huth <thuth@redhat.com>
34b321
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
34b321
RH-Acked-by: Max Reitz <mreitz@redhat.com>
34b321
34b321
cvtnum() returns int64_t: we should not be storing this
34b321
result inside of an int.
34b321
34b321
In a few cases, we need an extra sprinkling of error handling
34b321
where we expect to pass this number on towards a function that
34b321
expects something smaller than int64_t.
34b321
34b321
Reported-by: Max Reitz <mreitz@redhat.com>
34b321
Signed-off-by: John Snow <jsnow@redhat.com>
34b321
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
34b321
(cherry picked from commit 9b0beaf3de1396a23d5c287283e6f36c4b5d4385)
34b321
Signed-off-by: John Snow <jsnow@redhat.com>
34b321
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
34b321
34b321
Conflicts:
34b321
  qemu-io-cmds.c:
34b321
    - Upstream uses blk_xxx commands, while downstream uses bdrv_xxx
34b321
    - Fixes to sigraise are not backported.
34b321
34b321
Signed-off-by: John Snow <jsnow@redhat.com>
34b321
---
34b321
 qemu-io-cmds.c | 119 ++++++++++++++++++++++++++++++++++++++++-----------------
34b321
 1 file changed, 84 insertions(+), 35 deletions(-)
34b321
34b321
diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c
34b321
index 1f21ce9..95345fe 100644
34b321
--- a/qemu-io-cmds.c
34b321
+++ b/qemu-io-cmds.c
34b321
@@ -282,9 +282,10 @@ static void qemu_io_free(void *p)
34b321
     qemu_vfree(p);
34b321
 }
34b321
 
34b321
-static void dump_buffer(const void *buffer, int64_t offset, int len)
34b321
+static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
34b321
 {
34b321
-    int i, j;
34b321
+    uint64_t i;
34b321
+    int j;
34b321
     const uint8_t *p;
34b321
 
34b321
     for (i = 0, p = buffer; i < len; i += 16) {
34b321
@@ -307,7 +308,7 @@ static void dump_buffer(const void *buffer, int64_t offset, int len)
34b321
 }
34b321
 
34b321
 static void print_report(const char *op, struct timeval *t, int64_t offset,
34b321
-                         int count, int total, int cnt, int Cflag)
34b321
+                         int64_t count, int64_t total, int cnt, int Cflag)
34b321
 {
34b321
     char s1[64], s2[64], ts[64];
34b321
 
34b321
@@ -315,12 +316,12 @@ static void print_report(const char *op, struct timeval *t, int64_t offset,
34b321
     if (!Cflag) {
34b321
         cvtstr((double)total, s1, sizeof(s1));
34b321
         cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
34b321
-        printf("%s %d/%d bytes at offset %" PRId64 "\n",
34b321
+        printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n",
34b321
                op, total, count, offset);
34b321
         printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
34b321
                s1, cnt, ts, s2, tdiv((double)cnt, *t));
34b321
     } else {/* bytes,ops,time,bytes/sec,ops/sec */
34b321
-        printf("%d,%d,%s,%.3f,%.3f\n",
34b321
+        printf("%"PRId64",%d,%s,%.3f,%.3f\n",
34b321
             total, cnt, ts,
34b321
             tdiv((double)total, *t),
34b321
             tdiv((double)cnt, *t));
34b321
@@ -381,11 +382,15 @@ fail:
34b321
     return buf;
34b321
 }
34b321
 
34b321
-static int do_read(BlockDriverState *bs, char *buf, int64_t offset, int count,
34b321
-                   int *total)
34b321
+static int do_read(BlockDriverState *bs, char *buf, int64_t offset,
34b321
+                   int64_t count, int64_t *total)
34b321
 {
34b321
     int ret;
34b321
 
34b321
+    if (count >> 9 > INT_MAX) {
34b321
+        return -ERANGE;
34b321
+    }
34b321
+
34b321
     ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
34b321
     if (ret < 0) {
34b321
         return ret;
34b321
@@ -394,11 +399,15 @@ static int do_read(BlockDriverState *bs, char *buf, int64_t offset, int count,
34b321
     return 1;
34b321
 }
34b321
 
34b321
-static int do_write(BlockDriverState *bs, char *buf, int64_t offset, int count,
34b321
-                    int *total)
34b321
+static int do_write(BlockDriverState *bs, char *buf, int64_t offset,
34b321
+                    int64_t count, int64_t *total)
34b321
 {
34b321
     int ret;
34b321
 
34b321
+    if (count >> 9 > INT_MAX) {
34b321
+        return -ERANGE;
34b321
+    }
34b321
+
34b321
     ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
34b321
     if (ret < 0) {
34b321
         return ret;
34b321
@@ -407,9 +416,13 @@ static int do_write(BlockDriverState *bs, char *buf, int64_t offset, int count,
34b321
     return 1;
34b321
 }
34b321
 
34b321
-static int do_pread(BlockDriverState *bs, char *buf, int64_t offset, int count,
34b321
-                    int *total)
34b321
+static int do_pread(BlockDriverState *bs, char *buf, int64_t offset,
34b321
+                    int64_t count, int64_t *total)
34b321
 {
34b321
+    if (count > INT_MAX) {
34b321
+        return -ERANGE;
34b321
+    }
34b321
+
34b321
     *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
34b321
     if (*total < 0) {
34b321
         return *total;
34b321
@@ -417,9 +430,13 @@ static int do_pread(BlockDriverState *bs, char *buf, int64_t offset, int count,
34b321
     return 1;
34b321
 }
34b321
 
34b321
-static int do_pwrite(BlockDriverState *bs, char *buf, int64_t offset, int count,
34b321
-                     int *total)
34b321
+static int do_pwrite(BlockDriverState *bs, char *buf, int64_t offset,
34b321
+                     int64_t count, int64_t *total)
34b321
 {
34b321
+    if (count > INT_MAX) {
34b321
+        return -ERANGE;
34b321
+    }
34b321
+
34b321
     *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
34b321
     if (*total < 0) {
34b321
         return *total;
34b321
@@ -430,8 +447,8 @@ static int do_pwrite(BlockDriverState *bs, char *buf, int64_t offset, int count,
34b321
 typedef struct {
34b321
     BlockDriverState *bs;
34b321
     int64_t offset;
34b321
-    int count;
34b321
-    int *total;
34b321
+    int64_t count;
34b321
+    int64_t *total;
34b321
     int ret;
34b321
     bool done;
34b321
 } CoWriteZeroes;
34b321
@@ -451,8 +468,8 @@ static void coroutine_fn co_write_zeroes_entry(void *opaque)
34b321
     *data->total = data->count;
34b321
 }
34b321
 
34b321
-static int do_co_write_zeroes(BlockDriverState *bs, int64_t offset, int count,
34b321
-                              int *total)
34b321
+static int do_co_write_zeroes(BlockDriverState *bs, int64_t offset,
34b321
+                              int64_t count, int64_t *total)
34b321
 {
34b321
     Coroutine *co;
34b321
     CoWriteZeroes data = {
34b321
@@ -463,6 +480,10 @@ static int do_co_write_zeroes(BlockDriverState *bs, int64_t offset, int count,
34b321
         .done   = false,
34b321
     };
34b321
 
34b321
+    if (count >> BDRV_SECTOR_BITS > INT_MAX) {
34b321
+        return -ERANGE;
34b321
+    }
34b321
+
34b321
     co = qemu_coroutine_create(co_write_zeroes_entry);
34b321
     qemu_coroutine_enter(co, &data);
34b321
     while (!data.done) {
34b321
@@ -476,10 +497,14 @@ static int do_co_write_zeroes(BlockDriverState *bs, int64_t offset, int count,
34b321
 }
34b321
 
34b321
 static int do_write_compressed(BlockDriverState *bs, char *buf, int64_t offset,
34b321
-                               int count, int *total)
34b321
+                               int64_t count, int64_t *total)
34b321
 {
34b321
     int ret;
34b321
 
34b321
+    if (count >> 9 > INT_MAX) {
34b321
+        return -ERANGE;
34b321
+    }
34b321
+
34b321
     ret = bdrv_write_compressed(bs, offset >> 9, (uint8_t *)buf, count >> 9);
34b321
     if (ret < 0) {
34b321
         return ret;
34b321
@@ -489,8 +514,12 @@ static int do_write_compressed(BlockDriverState *bs, char *buf, int64_t offset,
34b321
 }
34b321
 
34b321
 static int do_load_vmstate(BlockDriverState *bs, char *buf, int64_t offset,
34b321
-                           int count, int *total)
34b321
+                           int64_t count, int64_t *total)
34b321
 {
34b321
+    if (count > INT_MAX) {
34b321
+        return -ERANGE;
34b321
+    }
34b321
+
34b321
     *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
34b321
     if (*total < 0) {
34b321
         return *total;
34b321
@@ -499,8 +528,12 @@ static int do_load_vmstate(BlockDriverState *bs, char *buf, int64_t offset,
34b321
 }
34b321
 
34b321
 static int do_save_vmstate(BlockDriverState *bs, char *buf, int64_t offset,
34b321
-                           int count, int *total)
34b321
+                           int64_t count, int64_t *total)
34b321
 {
34b321
+    if (count > INT_MAX) {
34b321
+        return -ERANGE;
34b321
+    }
34b321
+
34b321
     *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
34b321
     if (*total < 0) {
34b321
         return *total;
34b321
@@ -630,10 +663,11 @@ static int read_f(BlockDriverState *bs, int argc, char **argv)
34b321
     int c, cnt;
34b321
     char *buf;
34b321
     int64_t offset;
34b321
-    int count;
34b321
+    int64_t count;
34b321
     /* Some compilers get confused and warn if this is not initialized.  */
34b321
-    int total = 0;
34b321
-    int pattern = 0, pattern_offset = 0, pattern_count = 0;
34b321
+    int64_t total = 0;
34b321
+    int pattern = 0;
34b321
+    int64_t pattern_offset = 0, pattern_count = 0;
34b321
 
34b321
     while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
34b321
         switch (c) {
34b321
@@ -700,6 +734,10 @@ static int read_f(BlockDriverState *bs, int argc, char **argv)
34b321
     if (count < 0) {
34b321
         printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
         return 0;
34b321
+    } else if (count > SIZE_MAX) {
34b321
+        printf("length cannot exceed %" PRIu64 ", given %s\n",
34b321
+               (uint64_t) SIZE_MAX, argv[optind]);
34b321
+        return 0;
34b321
     }
34b321
 
34b321
     if (!Pflag && (lflag || sflag)) {
34b321
@@ -722,7 +760,7 @@ static int read_f(BlockDriverState *bs, int argc, char **argv)
34b321
             return 0;
34b321
         }
34b321
         if (count & 0x1ff) {
34b321
-            printf("count %d is not sector aligned\n",
34b321
+            printf("count %"PRId64" is not sector aligned\n",
34b321
                    count);
34b321
             return 0;
34b321
         }
34b321
@@ -750,7 +788,7 @@ static int read_f(BlockDriverState *bs, int argc, char **argv)
34b321
         memset(cmp_buf, pattern, pattern_count);
34b321
         if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
34b321
             printf("Pattern verification failed at offset %"
34b321
-                   PRId64 ", %d bytes\n",
34b321
+                   PRId64 ", %"PRId64" bytes\n",
34b321
                    offset + pattern_offset, pattern_count);
34b321
         }
34b321
         g_free(cmp_buf);
34b321
@@ -945,9 +983,9 @@ static int write_f(BlockDriverState *bs, int argc, char **argv)
34b321
     int c, cnt;
34b321
     char *buf = NULL;
34b321
     int64_t offset;
34b321
-    int count;
34b321
+    int64_t count;
34b321
     /* Some compilers get confused and warn if this is not initialized.  */
34b321
-    int total = 0;
34b321
+    int64_t total = 0;
34b321
     int pattern = 0xcd;
34b321
 
34b321
     while ((c = getopt(argc, argv, "bcCpP:qz")) != EOF) {
34b321
@@ -1007,6 +1045,10 @@ static int write_f(BlockDriverState *bs, int argc, char **argv)
34b321
     if (count < 0) {
34b321
         printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
         return 0;
34b321
+    } else if (count > SIZE_MAX) {
34b321
+        printf("length cannot exceed %" PRIu64 ", given %s\n",
34b321
+               (uint64_t) SIZE_MAX, argv[optind]);
34b321
+        return 0;
34b321
     }
34b321
 
34b321
     if (!pflag) {
34b321
@@ -1017,7 +1059,7 @@ static int write_f(BlockDriverState *bs, int argc, char **argv)
34b321
         }
34b321
 
34b321
         if (count & 0x1ff) {
34b321
-            printf("count %d is not sector aligned\n",
34b321
+            printf("count %"PRId64" is not sector aligned\n",
34b321
                    count);
34b321
             return 0;
34b321
         }
34b321
@@ -1752,8 +1794,7 @@ static int discard_f(BlockDriverState *bs, int argc, char **argv)
34b321
     struct timeval t1, t2;
34b321
     int Cflag = 0, qflag = 0;
34b321
     int c, ret;
34b321
-    int64_t offset;
34b321
-    int count;
34b321
+    int64_t offset, count;
34b321
 
34b321
     while ((c = getopt(argc, argv, "Cq")) != EOF) {
34b321
         switch (c) {
34b321
@@ -1783,6 +1824,11 @@ static int discard_f(BlockDriverState *bs, int argc, char **argv)
34b321
     if (count < 0) {
34b321
         printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
         return 0;
34b321
+    } else if (count >> BDRV_SECTOR_BITS > INT_MAX) {
34b321
+        printf("length cannot exceed %"PRIu64", given %s\n",
34b321
+               (uint64_t)INT_MAX << BDRV_SECTOR_BITS,
34b321
+               argv[optind]);
34b321
+        return 0;
34b321
     }
34b321
 
34b321
     gettimeofday(&t1, NULL);
34b321
@@ -1807,11 +1853,10 @@ out:
34b321
 
34b321
 static int alloc_f(BlockDriverState *bs, int argc, char **argv)
34b321
 {
34b321
-    int64_t offset, sector_num;
34b321
-    int nb_sectors, remaining;
34b321
+    int64_t offset, sector_num, nb_sectors, remaining;
34b321
     char s1[64];
34b321
-    int num, sum_alloc;
34b321
-    int ret;
34b321
+    int num, ret;
34b321
+    int64_t sum_alloc;
34b321
 
34b321
     offset = cvtnum(argv[1]);
34b321
     if (offset < 0) {
34b321
@@ -1828,6 +1873,10 @@ static int alloc_f(BlockDriverState *bs, int argc, char **argv)
34b321
         if (nb_sectors < 0) {
34b321
             printf("non-numeric length argument -- %s\n", argv[2]);
34b321
             return 0;
34b321
+        } else if (nb_sectors > INT_MAX) {
34b321
+            printf("length argument cannot exceed %d, given %s\n",
34b321
+                   INT_MAX, argv[2]);
34b321
+            return 0;
34b321
         }
34b321
     } else {
34b321
         nb_sectors = 1;
34b321
@@ -1855,7 +1904,7 @@ static int alloc_f(BlockDriverState *bs, int argc, char **argv)
34b321
 
34b321
     cvtstr(offset, s1, sizeof(s1));
34b321
 
34b321
-    printf("%d/%d sectors allocated at offset %s\n",
34b321
+    printf("%"PRId64"/%"PRId64" sectors allocated at offset %s\n",
34b321
            sum_alloc, nb_sectors, s1);
34b321
     return 0;
34b321
 }
34b321
-- 
34b321
1.8.3.1
34b321