util: Fix MIN_NON_ZERO
PATCH 1/1 util: Fix MIN_NON_ZERO
MIN_NON_ZERO(0, 1) is evaluated to 0. Rewrite the macro to fix it.
--- include/qemu/osdep.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
include/qemu/osdep.h +2 -1
@@ -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 */
Signature · 1 line
2.7.4
Quoted 1 line · Fam Zheng
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.
Quoted 12 lines
Reported-by Miroslav Rezanina <[email protected]>Signed-off-by Fam Zheng <[email protected]>--- include/qemu/osdep.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)include/qemu/osdep.h +0
-0
include/qemu/osdep.h extern int daemon(int, int); · line 155
Quoted 6 lines
include/qemu/osdep.h +2
-1@@ -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,
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 12 lines · Eric Blake
Quoted 1 line · Fam Zheng
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
Quoted 8 lines
Maybe you meant
MIN_NON_ZERO(1, 0), which evaluates to:(((1) != 0 && (1) < (0)) ? (1) : (0)) ((1 && 0) ? 1 : 0)
(0 ? 1 : 0)
0in which case, you are correct that there is a bug.
Commit message fixed, patch queued.
Paolo
Quoted 29 lines
Quoted 12 lines
Reported-by Miroslav Rezanina <[email protected]>Signed-off-by Fam Zheng <[email protected]>--- include/qemu/osdep.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)include/qemu/osdep.h +0
-0include/qemu/osdep.h extern int daemon(int, int); · line 155
Quoted 6 lines
include/qemu/osdep.h +2
-1@@ -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 <[email protected]>
Am 12.07.2016 um 18:24 schrieb Paolo Bonzini:
Quoted 20 lines · Eric Blake
Quoted 18 lines · Fam Zheng
Quoted 1 line
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)
1Maybe you meant
MIN_NON_ZERO(1, 0), which evaluates to:(((1) != 0 && (1) < (0)) ? (1) : (0))
((1 && 0) ? 1 : 0)
(0 ? 1 : 0)
0
Quoted 4 lines
Quoted 1 line
in which case, you are correct that there is a bug.
Commit message fixed, patch queued.
Paolo
Shouldn't we Cc qemu-stable?
Peter
Quoted 1 line · Fam Zheng
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) = 1MIN_NON_ZERO(1, 0) = 0
The second case is the buggy one.
Quoted 24 lines
Reported-by Miroslav Rezanina <[email protected]>Signed-off-by Fam Zheng <[email protected]>--- include/qemu/osdep.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)include/qemu/osdep.h +2
-1@@ -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 */ Signature · 1 line
2.7.4