qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] util: Fix MIN_NON_ZERO
@ 2016-07-12  6:48 Fam Zheng
  2016-07-12 15:54 ` Eric Blake
  2016-07-14 12:17 ` Stefan Hajnoczi
  0 siblings, 2 replies; 5+ messages in thread
From: Fam Zheng @ 2016-07-12  6:48 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Lieven, famz, Miroslav Rezanina, Max Reitz, Stefan Hajnoczi

MIN_NON_ZERO(0, 1) is evaluated to 0. Rewrite the macro to fix it.

Reported-by: Miroslav Rezanina <mrezanin@redhat.com>
Signed-off-by: Fam Zheng <famz@redhat.com>
---
 include/qemu/osdep.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index e63da28..e4c6ae6 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -151,7 +151,8 @@ extern int daemon(int, int);
 /* Minimum function that returns zero only iff both values are zero.
  * Intended for use with unsigned values only. */
 #ifndef MIN_NON_ZERO
-#define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b))
+#define MIN_NON_ZERO(a, b) ((a) == 0 ? (b) : \
+                                ((b) == 0 ? (a) : (MIN(a, b))))
 #endif
 
 /* Round number down to multiple */
-- 
2.7.4

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

* Re: [Qemu-devel] [PATCH] util: Fix MIN_NON_ZERO
  2016-07-12  6:48 [Qemu-devel] [PATCH] util: Fix MIN_NON_ZERO Fam Zheng
@ 2016-07-12 15:54 ` Eric Blake
  2016-07-12 16:24   ` Paolo Bonzini
  2016-07-14 12:17 ` Stefan Hajnoczi
  1 sibling, 1 reply; 5+ messages in thread
From: Eric Blake @ 2016-07-12 15:54 UTC (permalink / raw)
  To: Fam Zheng, qemu-devel
  Cc: Miroslav Rezanina, Peter Lieven, Stefan Hajnoczi, Max Reitz

[-- Attachment #1: Type: text/plain, Size: 1519 bytes --]

On 07/12/2016 12:48 AM, Fam Zheng wrote:
> MIN_NON_ZERO(0, 1) is evaluated to 0. Rewrite the macro to fix it.

Huh?

Old expansion, in various stages:

(((0) != 0 && (0) < (1)) ? (0) : (1))
((0 && 1) ? 0 : 1)
(0 ? 0 : 1)
1

Maybe you meant MIN_NON_ZERO(1, 0), which evaluates to:

(((1) != 0 && (1) < (0)) ? (1) : (0))
((1 && 0) ? 1 : 0)
(0 ? 1 : 0)
0

in which case, you are correct that there is a bug.

> 
> Reported-by: Miroslav Rezanina <mrezanin@redhat.com>
> Signed-off-by: Fam Zheng <famz@redhat.com>
> ---
>  include/qemu/osdep.h | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
> index e63da28..e4c6ae6 100644
> --- a/include/qemu/osdep.h
> +++ b/include/qemu/osdep.h
> @@ -151,7 +151,8 @@ extern int daemon(int, int);
>  /* Minimum function that returns zero only iff both values are zero.
>   * Intended for use with unsigned values only. */
>  #ifndef MIN_NON_ZERO
> -#define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b))
> +#define MIN_NON_ZERO(a, b) ((a) == 0 ? (b) : \
> +                                ((b) == 0 ? (a) : (MIN(a, b))))

Another way might be:

(!(a) == !(b) ? MIN(a, b) : (a) + (b))

but you have to put more mental thought into that.  I'm just fine with
your version.

With the commit message fixed,
Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]

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

* Re: [Qemu-devel] [PATCH] util: Fix MIN_NON_ZERO
  2016-07-12 15:54 ` Eric Blake
@ 2016-07-12 16:24   ` Paolo Bonzini
  2016-07-18  8:00     ` Peter Lieven
  0 siblings, 1 reply; 5+ messages in thread
From: Paolo Bonzini @ 2016-07-12 16:24 UTC (permalink / raw)
  To: Eric Blake, Fam Zheng, qemu-devel
  Cc: Miroslav Rezanina, Peter Lieven, Stefan Hajnoczi, Max Reitz



On 12/07/2016 17:54, Eric Blake wrote:
> On 07/12/2016 12:48 AM, Fam Zheng wrote:
>> MIN_NON_ZERO(0, 1) is evaluated to 0. Rewrite the macro to fix it.
> 
> Huh?
> 
> Old expansion, in various stages:
> 
> (((0) != 0 && (0) < (1)) ? (0) : (1))
> ((0 && 1) ? 0 : 1)
> (0 ? 0 : 1)
> 1
> 
> Maybe you meant MIN_NON_ZERO(1, 0), which evaluates to:
> 
> (((1) != 0 && (1) < (0)) ? (1) : (0))
> ((1 && 0) ? 1 : 0)
> (0 ? 1 : 0)
> 0
> 
> in which case, you are correct that there is a bug.

Commit message fixed, patch queued.

Paolo

>>
>> Reported-by: Miroslav Rezanina <mrezanin@redhat.com>
>> Signed-off-by: Fam Zheng <famz@redhat.com>
>> ---
>>  include/qemu/osdep.h | 3 ++-
>>  1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
>> index e63da28..e4c6ae6 100644
>> --- a/include/qemu/osdep.h
>> +++ b/include/qemu/osdep.h
>> @@ -151,7 +151,8 @@ extern int daemon(int, int);
>>  /* Minimum function that returns zero only iff both values are zero.
>>   * Intended for use with unsigned values only. */
>>  #ifndef MIN_NON_ZERO
>> -#define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b))
>> +#define MIN_NON_ZERO(a, b) ((a) == 0 ? (b) : \
>> +                                ((b) == 0 ? (a) : (MIN(a, b))))
> 
> Another way might be:
> 
> (!(a) == !(b) ? MIN(a, b) : (a) + (b))
> 
> but you have to put more mental thought into that.  I'm just fine with
> your version.
> 
> With the commit message fixed,
> Reviewed-by: Eric Blake <eblake@redhat.com>
> 

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

* Re: [Qemu-devel] [PATCH] util: Fix MIN_NON_ZERO
  2016-07-12  6:48 [Qemu-devel] [PATCH] util: Fix MIN_NON_ZERO Fam Zheng
  2016-07-12 15:54 ` Eric Blake
@ 2016-07-14 12:17 ` Stefan Hajnoczi
  1 sibling, 0 replies; 5+ messages in thread
From: Stefan Hajnoczi @ 2016-07-14 12:17 UTC (permalink / raw)
  To: Fam Zheng; +Cc: qemu-devel, Peter Lieven, Miroslav Rezanina, Max Reitz

[-- Attachment #1: Type: text/plain, Size: 1088 bytes --]

On Tue, Jul 12, 2016 at 02:48:33PM +0800, Fam Zheng wrote:
> MIN_NON_ZERO(0, 1) is evaluated to 0. Rewrite the macro to fix it.

This is incorrect.  The actual results are:

MIN_NON_ZERO(0, 1) = 1
MIN_NON_ZERO(1, 0) = 0

The second case is the buggy one.

> 
> Reported-by: Miroslav Rezanina <mrezanin@redhat.com>
> Signed-off-by: Fam Zheng <famz@redhat.com>
> ---
>  include/qemu/osdep.h | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
> index e63da28..e4c6ae6 100644
> --- a/include/qemu/osdep.h
> +++ b/include/qemu/osdep.h
> @@ -151,7 +151,8 @@ extern int daemon(int, int);
>  /* Minimum function that returns zero only iff both values are zero.
>   * Intended for use with unsigned values only. */
>  #ifndef MIN_NON_ZERO
> -#define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b))
> +#define MIN_NON_ZERO(a, b) ((a) == 0 ? (b) : \
> +                                ((b) == 0 ? (a) : (MIN(a, b))))
>  #endif
>  
>  /* Round number down to multiple */
> -- 
> 2.7.4
> 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

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

* Re: [Qemu-devel] [PATCH] util: Fix MIN_NON_ZERO
  2016-07-12 16:24   ` Paolo Bonzini
@ 2016-07-18  8:00     ` Peter Lieven
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Lieven @ 2016-07-18  8:00 UTC (permalink / raw)
  To: Paolo Bonzini, Eric Blake, Fam Zheng, qemu-devel
  Cc: Miroslav Rezanina, Stefan Hajnoczi, Max Reitz, qemu-stable

Am 12.07.2016 um 18:24 schrieb Paolo Bonzini:
>
> On 12/07/2016 17:54, Eric Blake wrote:
>> On 07/12/2016 12:48 AM, Fam Zheng wrote:
>>> MIN_NON_ZERO(0, 1) is evaluated to 0. Rewrite the macro to fix it.
>> Huh?
>>
>> Old expansion, in various stages:
>>
>> (((0) != 0 && (0) < (1)) ? (0) : (1))
>> ((0 && 1) ? 0 : 1)
>> (0 ? 0 : 1)
>> 1
>>
>> Maybe you meant MIN_NON_ZERO(1, 0), which evaluates to:
>>
>> (((1) != 0 && (1) < (0)) ? (1) : (0))
>> ((1 && 0) ? 1 : 0)
>> (0 ? 1 : 0)
>> 0
>>
>> in which case, you are correct that there is a bug.
> Commit message fixed, patch queued.
>
> Paolo

Shouldn't we Cc qemu-stable?

Peter

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

end of thread, other threads:[~2016-07-18  8:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-07-12  6:48 [Qemu-devel] [PATCH] util: Fix MIN_NON_ZERO Fam Zheng
2016-07-12 15:54 ` Eric Blake
2016-07-12 16:24   ` Paolo Bonzini
2016-07-18  8:00     ` Peter Lieven
2016-07-14 12:17 ` Stefan Hajnoczi

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).