Skip to contentSkip to conversation

qemu-img: initialize MapEntry object

PATCH Superseded John Snow 3 participants 4 messages Feb 3, 2016 – Feb 4, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

John Snow

author
#0 lore raw

PATCH 1/1 qemu-img: initialize MapEntry object

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

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

---
 qemu-img.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
qemu-img.c +4 -1
index f121980..5a85178 100644
@@ -2231,6 +2231,9 @@ static int get_block_status(BlockDriverState *bs, int64_t sector_num,
     if (file && e->has_offset) {
         e->has_filename = true;
         e->filename = file->filename;
+    } else {
+        e->has_filename = false;
+        e->filename = NULL;
     }
     return 0;
 }
@@ -2264,7 +2267,7 @@ static int img_map(int argc, char **argv)
     BlockDriverState *bs;
     const char *filename, *fmt, *output;
     int64_t length;
-    MapEntry curr = { .length = 0 }, next;
+    MapEntry curr = { .length = 0 }, next = { .length = 0 };
     int ret = 0;
 
     fmt = NULL;
Signature · 1 line
2.4.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 38 lines · John Snow

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

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

---
 qemu-img.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
qemu-img.c +4 -1
index f121980..5a85178 100644
@@ -2231,6 +2231,9 @@ static int get_block_status(BlockDriverState *bs, int64_t sector_num,
     if (file && e->has_offset) {
         e->has_filename = true;
         e->filename = file->filename;
+    } else {
+        e->has_filename = false;
+        e->filename = NULL;
     }
     return 0;
 }
@@ -2264,7 +2267,7 @@ static int img_map(int argc, char **argv)
     BlockDriverState *bs;
     const char *filename, *fmt, *output;
     int64_t length;
-    MapEntry curr = { .length = 0 }, next;
+    MapEntry curr = { .length = 0 }, next = { .length = 0 };
     int ret = 0;
 
     fmt = NULL;
Signature · 1 line
2.4.3
#2 lore raw
In reply to John Snow

Am 04.02.2016 um 00:38 hat John Snow geschrieben:

Quoted 17 lines

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

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

---
 qemu-img.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
qemu-img.c +0 -0
index f121980..5a85178 100644

qemu-img.c static int get_block_status(BlockDriverState *bs, int64_t sector_num, · line 2239

Quoted 9 lines
qemu-img.c +3 -0
@@ -2231,6 +2231,9 @@ static int get_block_status(BlockDriverState *bs, int64_t sector_num,
     if (file && e->has_offset) {
         e->has_filename = true;
         e->filename = file->filename;
+    } else {
+        e->has_filename = false;
+        e->filename = NULL;
     }
     return 0;
 }

I guess this fixes the bug, but wouldn't it actually be nicer to just reinitialise the whole object? As everyone knows, I love compound literals, so I'd make it one big assignment that zeroes everything that isn't specified:

*e = (MapEntry) {
    ...
};

qemu-img.c static int img_map(int argc, char **argv) · line 2271

Quoted 7 lines
qemu-img.c +1 -1
@@ -2264,7 +2267,7 @@ static int img_map(int argc, char **argv)
     BlockDriverState *bs;
     const char *filename, *fmt, *output;
     int64_t length;
-    MapEntry curr = { .length = 0 }, next;
+    MapEntry curr = { .length = 0 }, next = { .length = 0 };
     int ret = 0;

At first I didn't quite understand what this was for, but I think you tried to cover newly added fields. If you overwrite the whole struct above, you wouldn't need to initialise it here any more.

Kevin

author
#3 lore raw
Quoted 33 lines · Kevin Wolf

Am 04.02.2016 um 00:38 hat John Snow geschrieben:

Quoted 17 lines

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

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

---
 qemu-img.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
qemu-img.c +0 -0
index f121980..5a85178 100644

qemu-img.c static int get_block_status(BlockDriverState *bs, int64_t sector_num, · line 2239

Quoted 9 lines
qemu-img.c +3 -0
@@ -2231,6 +2231,9 @@ static int get_block_status(BlockDriverState *bs, int64_t sector_num,
     if (file && e->has_offset) {
         e->has_filename = true;
         e->filename = file->filename;
+    } else {
+        e->has_filename = false;
+        e->filename = NULL;
     }
     return 0;
 }

I guess this fixes the bug, but wouldn't it actually be nicer to just reinitialise the whole object? As everyone knows, I love compound literals, so I'd make it one big assignment that zeroes everything that isn't specified:

Quoted 4 lines
*e = (MapEntry) {
    ...
};

Yeah, that's more future proof isn't it.

Quoted 12 lines

qemu-img.c static int img_map(int argc, char **argv) · line 2271

Quoted 7 lines
qemu-img.c +1 -1
@@ -2264,7 +2267,7 @@ static int img_map(int argc, char **argv)
     BlockDriverState *bs;
     const char *filename, *fmt, *output;
     int64_t length;
-    MapEntry curr = { .length = 0 }, next;
+    MapEntry curr = { .length = 0 }, next = { .length = 0 };
     int ret = 0;

At first I didn't quite understand what this was for, but I think you tried to cover newly added fields. If you overwrite the whole struct above, you wouldn't need to initialise it here any more.

Quoted 2 lines

Kevin

Yeah, the intent was:

(1) Always make sure it starts out zeroed.
(2) In case someone tries to call get_block_status with an object that isn't zeroed in the future, make sure has_filename is toggled back to false.

I'll send you a new one.