[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH] util: Fix MIN_NON_ZERO
From: |
Eric Blake |
Subject: |
Re: [Qemu-devel] [PATCH] util: Fix MIN_NON_ZERO |
Date: |
Tue, 12 Jul 2016 09:54:35 -0600 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 |
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 <address@hidden>
> Signed-off-by: Fam Zheng <address@hidden>
> ---
> 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 <address@hidden>
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature