All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] qemu-img: initialize MapEntry object
@ 2016-02-03 23:38 John Snow
  2016-02-04  1:38 ` Fam Zheng
  2016-02-04 12:43 ` Kevin Wolf
  0 siblings, 2 replies; 4+ messages in thread
From: John Snow @ 2016-02-03 23:38 UTC (permalink / raw)
  To: qemu-block; +Cc: kwolf, John Snow, famz, qemu-devel

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.

Signed-off-by: John Snow <jsnow@redhat.com>
---
 qemu-img.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/qemu-img.c b/qemu-img.c
index f121980..5a85178 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -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;
-- 
2.4.3

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [Qemu-devel] [PATCH] qemu-img: initialize MapEntry object
  2016-02-03 23:38 [Qemu-devel] [PATCH] qemu-img: initialize MapEntry object John Snow
@ 2016-02-04  1:38 ` Fam Zheng
  2016-02-04 12:43 ` Kevin Wolf
  1 sibling, 0 replies; 4+ messages in thread
From: Fam Zheng @ 2016-02-04  1:38 UTC (permalink / raw)
  To: John Snow; +Cc: kwolf, qemu-devel, qemu-block

On Wed, 02/03 18:38, John Snow wrote:
> 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.
> 
> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
>  qemu-img.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/qemu-img.c b/qemu-img.c
> index f121980..5a85178 100644
> --- a/qemu-img.c
> +++ b/qemu-img.c
> @@ -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;
> -- 
> 2.4.3
> 

Reviewed-by: Fam Zheng <famz@redhat.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Qemu-devel] [PATCH] qemu-img: initialize MapEntry object
  2016-02-03 23:38 [Qemu-devel] [PATCH] qemu-img: initialize MapEntry object John Snow
  2016-02-04  1:38 ` Fam Zheng
@ 2016-02-04 12:43 ` Kevin Wolf
  2016-02-04 15:52   ` John Snow
  1 sibling, 1 reply; 4+ messages in thread
From: Kevin Wolf @ 2016-02-04 12:43 UTC (permalink / raw)
  To: John Snow; +Cc: famz, qemu-devel, qemu-block

Am 04.02.2016 um 00:38 hat John Snow geschrieben:
> 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.
> 
> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
>  qemu-img.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/qemu-img.c b/qemu-img.c
> index f121980..5a85178 100644
> --- a/qemu-img.c
> +++ b/qemu-img.c
> @@ -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) {
    ...
};

> @@ -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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Qemu-devel] [PATCH] qemu-img: initialize MapEntry object
  2016-02-04 12:43 ` Kevin Wolf
@ 2016-02-04 15:52   ` John Snow
  0 siblings, 0 replies; 4+ messages in thread
From: John Snow @ 2016-02-04 15:52 UTC (permalink / raw)
  To: Kevin Wolf; +Cc: famz, qemu-devel, qemu-block



On 02/04/2016 07:43 AM, Kevin Wolf wrote:
> Am 04.02.2016 um 00:38 hat John Snow geschrieben:
>> 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.
>>
>> Signed-off-by: John Snow <jsnow@redhat.com>
>> ---
>>  qemu-img.c | 5 ++++-
>>  1 file changed, 4 insertions(+), 1 deletion(-)
>>
>> diff --git a/qemu-img.c b/qemu-img.c
>> index f121980..5a85178 100644
>> --- a/qemu-img.c
>> +++ b/qemu-img.c
>> @@ -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) {
>     ...
> };
> 

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

>> @@ -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
> 

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.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-02-04 15:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-02-03 23:38 [Qemu-devel] [PATCH] qemu-img: initialize MapEntry object John Snow
2016-02-04  1:38 ` Fam Zheng
2016-02-04 12:43 ` Kevin Wolf
2016-02-04 15:52   ` John Snow

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.