* [Qemu-devel] [PATCH 08/15] blkdebug: fix enum comparison
@ 2010-09-05 15:06 Blue Swirl
2010-09-05 17:57 ` [Qemu-devel] " Michael S. Tsirkin
0 siblings, 1 reply; 5+ messages in thread
From: Blue Swirl @ 2010-09-05 15:06 UTC (permalink / raw)
To: qemu-devel
The signedness of enum types depend on the compiler implementation.
Therefore the check for negative values may or may not be meaningful.
Fix by explicitly casting to a signed integer.
Since the values are also checked earlier against event_names
table, this is an internal error. Change the 'if' to 'assert'.
This also fixes a warning with GCC flag -Wtype-limits.
Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
---
block/blkdebug.c | 4 +---
1 files changed, 1 insertions(+), 3 deletions(-)
diff --git a/block/blkdebug.c b/block/blkdebug.c
index 2a63df9..4d6ff0a 100644
--- a/block/blkdebug.c
+++ b/block/blkdebug.c
@@ -439,9 +439,7 @@ static void blkdebug_debug_event(BlockDriverState
*bs, BlkDebugEvent event)
struct BlkdebugRule *rule;
BlkdebugVars old_vars = s->vars;
- if (event < 0 || event >= BLKDBG_EVENT_MAX) {
- return;
- }
+ assert((int)event >= 0 && event < BLKDBG_EVENT_MAX);
QLIST_FOREACH(rule, &s->rules[event], next) {
process_rule(bs, rule, &old_vars);
--
1.6.2.4
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [Qemu-devel] Re: [PATCH 08/15] blkdebug: fix enum comparison
2010-09-05 15:06 [Qemu-devel] [PATCH 08/15] blkdebug: fix enum comparison Blue Swirl
@ 2010-09-05 17:57 ` Michael S. Tsirkin
2010-09-05 19:37 ` Blue Swirl
0 siblings, 1 reply; 5+ messages in thread
From: Michael S. Tsirkin @ 2010-09-05 17:57 UTC (permalink / raw)
To: Blue Swirl; +Cc: qemu-devel
On Sun, Sep 05, 2010 at 03:06:32PM +0000, Blue Swirl wrote:
> The signedness of enum types depend on the compiler implementation.
> Therefore the check for negative values may or may not be meaningful.
>
> Fix by explicitly casting to a signed integer.
>
> Since the values are also checked earlier against event_names
> table, this is an internal error. Change the 'if' to 'assert'.
>
> This also fixes a warning with GCC flag -Wtype-limits.
>
> Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
> ---
> block/blkdebug.c | 4 +---
> 1 files changed, 1 insertions(+), 3 deletions(-)
>
> diff --git a/block/blkdebug.c b/block/blkdebug.c
> index 2a63df9..4d6ff0a 100644
> --- a/block/blkdebug.c
> +++ b/block/blkdebug.c
> @@ -439,9 +439,7 @@ static void blkdebug_debug_event(BlockDriverState
> *bs, BlkDebugEvent event)
> struct BlkdebugRule *rule;
> BlkdebugVars old_vars = s->vars;
>
> - if (event < 0 || event >= BLKDBG_EVENT_MAX) {
> - return;
> - }
> + assert((int)event >= 0 && event < BLKDBG_EVENT_MAX);
I am not sure all compilers must generate a negative value from
a very large unsigned integer cast to int.
assert((unsigned)event < BLKDBG_EVENT_MAX);
will do the same but without integer overflow.
>
> QLIST_FOREACH(rule, &s->rules[event], next) {
> process_rule(bs, rule, &old_vars);
> --
> 1.6.2.4
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Qemu-devel] Re: [PATCH 08/15] blkdebug: fix enum comparison
2010-09-05 17:57 ` [Qemu-devel] " Michael S. Tsirkin
@ 2010-09-05 19:37 ` Blue Swirl
2010-09-05 21:00 ` [Qemu-devel] Re: [PATCH 08/15] blkdebug: fix enum comparison' Michael S. Tsirkin
0 siblings, 1 reply; 5+ messages in thread
From: Blue Swirl @ 2010-09-05 19:37 UTC (permalink / raw)
To: Michael S. Tsirkin; +Cc: qemu-devel
On Sun, Sep 5, 2010 at 5:57 PM, Michael S. Tsirkin <mst@redhat.com> wrote:
> On Sun, Sep 05, 2010 at 03:06:32PM +0000, Blue Swirl wrote:
>> The signedness of enum types depend on the compiler implementation.
>> Therefore the check for negative values may or may not be meaningful.
>>
>> Fix by explicitly casting to a signed integer.
>>
>> Since the values are also checked earlier against event_names
>> table, this is an internal error. Change the 'if' to 'assert'.
>>
>> This also fixes a warning with GCC flag -Wtype-limits.
>>
>> Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
>> ---
>> block/blkdebug.c | 4 +---
>> 1 files changed, 1 insertions(+), 3 deletions(-)
>>
>> diff --git a/block/blkdebug.c b/block/blkdebug.c
>> index 2a63df9..4d6ff0a 100644
>> --- a/block/blkdebug.c
>> +++ b/block/blkdebug.c
>> @@ -439,9 +439,7 @@ static void blkdebug_debug_event(BlockDriverState
>> *bs, BlkDebugEvent event)
>> struct BlkdebugRule *rule;
>> BlkdebugVars old_vars = s->vars;
>>
>> - if (event < 0 || event >= BLKDBG_EVENT_MAX) {
>> - return;
>> - }
>> + assert((int)event >= 0 && event < BLKDBG_EVENT_MAX);
>
> I am not sure all compilers must generate a negative value from
> a very large unsigned integer cast to int.
The enum rules seem to be vague. The type of enums may also be signed
(on GCC when the enum set includes negative values, on other compilers
in other cases). Do any machines or compilers exist (on which QEMU
runs) where this could happen?
> assert((unsigned)event < BLKDBG_EVENT_MAX);
>
> will do the same but without integer overflow.
It's not the same if BLKDBG_EVENT_MAX >= 0x80000000 and the type of
the BlkDebugEvent is unsigned. It's probably more correct, though.
>>
>> QLIST_FOREACH(rule, &s->rules[event], next) {
>> process_rule(bs, rule, &old_vars);
>> --
>> 1.6.2.4
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Qemu-devel] Re: [PATCH 08/15] blkdebug: fix enum comparison'
2010-09-05 19:37 ` Blue Swirl
@ 2010-09-05 21:00 ` Michael S. Tsirkin
0 siblings, 0 replies; 5+ messages in thread
From: Michael S. Tsirkin @ 2010-09-05 21:00 UTC (permalink / raw)
To: Blue Swirl; +Cc: qemu-devel
On Sun, Sep 05, 2010 at 07:37:54PM +0000, Blue Swirl wrote:
> On Sun, Sep 5, 2010 at 5:57 PM, Michael S. Tsirkin <mst@redhat.com> wrote:
> > On Sun, Sep 05, 2010 at 03:06:32PM +0000, Blue Swirl wrote:
> >> The signedness of enum types depend on the compiler implementation.
> >> Therefore the check for negative values may or may not be meaningful.
> >>
> >> Fix by explicitly casting to a signed integer.
> >>
> >> Since the values are also checked earlier against event_names
> >> table, this is an internal error. Change the 'if' to 'assert'.
> >>
> >> This also fixes a warning with GCC flag -Wtype-limits.
> >>
> >> Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
> >> ---
> >> block/blkdebug.c | 4 +---
> >> 1 files changed, 1 insertions(+), 3 deletions(-)
> >>
> >> diff --git a/block/blkdebug.c b/block/blkdebug.c
> >> index 2a63df9..4d6ff0a 100644
> >> --- a/block/blkdebug.c
> >> +++ b/block/blkdebug.c
> >> @@ -439,9 +439,7 @@ static void blkdebug_debug_event(BlockDriverState
> >> *bs, BlkDebugEvent event)
> >> struct BlkdebugRule *rule;
> >> BlkdebugVars old_vars = s->vars;
> >>
> >> - if (event < 0 || event >= BLKDBG_EVENT_MAX) {
> >> - return;
> >> - }
> >> + assert((int)event >= 0 && event < BLKDBG_EVENT_MAX);
> >
> > I am not sure all compilers must generate a negative value from
> > a very large unsigned integer cast to int.
>
> The enum rules seem to be vague. The type of enums may also be signed
> (on GCC when the enum set includes negative values, on other compilers
> in other cases). Do any machines or compilers exist (on which QEMU
> runs) where this could happen?
I remember reading that GCC sometimes assumes signed integers don't overflow,
and generates code behaves incorrectly if they do.
No idea whether this is ever the case for casts.
--
MST
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Qemu-devel] [PATCH 08/15] blkdebug: fix enum comparison
@ 2010-09-10 20:59 Blue Swirl
0 siblings, 0 replies; 5+ messages in thread
From: Blue Swirl @ 2010-09-10 20:59 UTC (permalink / raw)
To: qemu-devel
The signedness of enum types depend on the compiler implementation.
Therefore the check for negative values may or may not be meaningful.
Fix by explicitly casting to a signed integer.
Since the values are also checked earlier against event_names
table, this is an internal error. Change the 'if' to 'assert'.
This also avoids a warning with GCC flag -Wtype-limits.
Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
---
block/blkdebug.c | 4 +---
1 files changed, 1 insertions(+), 3 deletions(-)
diff --git a/block/blkdebug.c b/block/blkdebug.c
index 2a63df9..4d6ff0a 100644
--- a/block/blkdebug.c
+++ b/block/blkdebug.c
@@ -439,9 +439,7 @@ static void blkdebug_debug_event(BlockDriverState
*bs, BlkDebugEvent event)
struct BlkdebugRule *rule;
BlkdebugVars old_vars = s->vars;
- if (event < 0 || event >= BLKDBG_EVENT_MAX) {
- return;
- }
+ assert((int)event >= 0 && event < BLKDBG_EVENT_MAX);
QLIST_FOREACH(rule, &s->rules[event], next) {
process_rule(bs, rule, &old_vars);
--
1.6.2.4
^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2010-09-10 21:00 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-09-05 15:06 [Qemu-devel] [PATCH 08/15] blkdebug: fix enum comparison Blue Swirl
2010-09-05 17:57 ` [Qemu-devel] " Michael S. Tsirkin
2010-09-05 19:37 ` Blue Swirl
2010-09-05 21:00 ` [Qemu-devel] Re: [PATCH 08/15] blkdebug: fix enum comparison' Michael S. Tsirkin
-- strict thread matches above, loose matches on Subject: below --
2010-09-10 20:59 [Qemu-devel] [PATCH 08/15] blkdebug: fix enum comparison Blue Swirl
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).