From dcf496c788d09cfc1969c2a3608964de299d7a13 Mon Sep 17 00:00:00 2001 From: Anton Staaf Date: Tue, 2 Sep 2014 13:19:18 -0700 Subject: [PATCH] Util: Make MAX and MIN macros side effect safe Previously the MAX and MIN macros evaluated their arguments twice. This can cause problems with parameters that have side effects, or parameters that are volatile. Signed-off-by: Anton Staaf BRANCH=none TEST=make buildall -j Change-Id: I51c6c6c207d9cd4d11a3b4d237eb9e491a9c4935 Reviewed-on: https://chromium-review.googlesource.com/215990 Tested-by: Anton Staaf Reviewed-by: Bill Richardson Commit-Queue: Anton Staaf --- include/util.h | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/include/util.h b/include/util.h index 89e07deb1e..e952123e37 100644 --- a/include/util.h +++ b/include/util.h @@ -33,13 +33,24 @@ #define ASSERT(cond) #endif - /* Standard macros / definitions */ #ifndef MAX -#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MAX(a, b) \ + ({ \ + __typeof__(a) temp_a = (a); \ + __typeof__(b) temp_b = (b); \ + \ + temp_a > temp_b ? temp_a : temp_b; \ + }) #endif #ifndef MIN -#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MIN(a, b) \ + ({ \ + __typeof__(a) temp_a = (a); \ + __typeof__(b) temp_b = (b); \ + \ + temp_a < temp_b ? temp_a : temp_b; \ + }) #endif #ifndef NULL #define NULL ((void *)0)