[PULL,01/34] qemu-img: initialize MapEntry object
diff mbox

Message ID 1456158772-9344-2-git-send-email-kwolf@redhat.com
State New
Headers show

Commit Message

Kevin Wolf Feb. 22, 2016, 4:32 p.m. UTC
From: John Snow <jsnow@redhat.com>

Commit 16b0d555 introduced an issue where we are not initializing
has_filename for the 'next' MapEntry object, which leads to interesting
errors in both Valgrind and Clang -fsanitize=undefined.

Zero the stack object at allocation AND make sure the utility to
populate the fields properly marks has_filename as false if applicable.

Signed-off-by: John Snow <jsnow@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 qemu-img.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

Patch
diff mbox

diff --git a/qemu-img.c b/qemu-img.c
index 7030107..fb9ab1f 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -2194,6 +2194,7 @@  static int get_block_status(BlockDriverState *bs, int64_t sector_num,
     int64_t ret;
     int depth;
     BlockDriverState *file;
+    bool has_offset;
 
     /* As an optimization, we could cache the current range of unallocated
      * clusters in each file of the chain, and avoid querying the same
@@ -2220,17 +2221,20 @@  static int get_block_status(BlockDriverState *bs, int64_t sector_num,
         depth++;
     }
 
-    e->start = sector_num * BDRV_SECTOR_SIZE;
-    e->length = nb_sectors * BDRV_SECTOR_SIZE;
-    e->data = !!(ret & BDRV_BLOCK_DATA);
-    e->zero = !!(ret & BDRV_BLOCK_ZERO);
-    e->offset = ret & BDRV_BLOCK_OFFSET_MASK;
-    e->has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
-    e->depth = depth;
-    if (file && e->has_offset) {
-        e->has_filename = true;
-        e->filename = file->filename;
-    }
+    has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
+
+    *e = (MapEntry) {
+        .start = sector_num * BDRV_SECTOR_SIZE,
+        .length = nb_sectors * BDRV_SECTOR_SIZE,
+        .data = !!(ret & BDRV_BLOCK_DATA),
+        .zero = !!(ret & BDRV_BLOCK_ZERO),
+        .offset = ret & BDRV_BLOCK_OFFSET_MASK,
+        .has_offset = has_offset,
+        .depth = depth,
+        .has_filename = file && has_offset,
+        .filename = file && has_offset ? file->filename : NULL,
+    };
+
     return 0;
 }