summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Staaf <robotboy@chromium.org>2014-09-02 13:19:18 -0700
committerchrome-internal-fetch <chrome-internal-fetch@google.com>2014-09-09 20:59:41 +0000
commitdcf496c788d09cfc1969c2a3608964de299d7a13 (patch)
tree569ffaf663da4d9fe98a648e87b520067033b1c4
parent727de44ede4af166cb4a389ed1222026b64108aa (diff)
downloadchrome-ec-dcf496c788d09cfc1969c2a3608964de299d7a13.tar.gz
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 <robotboy@chromium.org> BRANCH=none TEST=make buildall -j Change-Id: I51c6c6c207d9cd4d11a3b4d237eb9e491a9c4935 Reviewed-on: https://chromium-review.googlesource.com/215990 Tested-by: Anton Staaf <robotboy@chromium.org> Reviewed-by: Bill Richardson <wfrichar@chromium.org> Commit-Queue: Anton Staaf <robotboy@chromium.org>
-rw-r--r--include/util.h17
1 files 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)