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>
This commit is contained in:
Anton Staaf
2014-09-02 13:19:18 -07:00
committed by chrome-internal-fetch
parent 727de44ede
commit dcf496c788

View File

@@ -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)