From 8a0bebfc3a0edc5e32b2aa3785d3d8367504a393 Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Sat, 12 Mar 2016 19:58:09 +0100 Subject: [PATCH] lib: fix MIN/MAX macros to not double-eval cf. https://gcc.gnu.org/onlinedocs/gcc/Typeof.html (Works on all compilers on Quagga's compiler support list in doc/overview.texi) Signed-off-by: David Lamparter Tested-by: NetDEF CI System Acked-by: Donald Sharp --- lib/zebra.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/zebra.h b/lib/zebra.h index f49db41d26..8cef0bfac2 100644 --- a/lib/zebra.h +++ b/lib/zebra.h @@ -384,10 +384,16 @@ struct in_pktinfo /* MAX / MIN are not commonly defined, but useful */ #ifndef MAX -#define MAX(a, b) ((a) > (b) ? (a) : (b)) -#endif +#define MAX(a, b) \ + ({ typeof (a) _a = (a); \ + typeof (b) _b = (b); \ + _a > _b ? _a : _b; }) +#endif #ifndef MIN -#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MIN(a, b) \ + ({ typeof (a) _a = (a); \ + typeof (b) _b = (b); \ + _a < _b ? _a : _b; }) #endif #define ZEBRA_NUM_OF(x) (sizeof (x) / sizeof (x[0])) -- 2.39.5