]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: compat: make the MIN/MAX macros more reliable
authorWilly Tarreau <w@1wt.eu>
Fri, 17 May 2024 13:25:26 +0000 (15:25 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 17 May 2024 13:57:18 +0000 (15:57 +0200)
After every release we say that MIN/MAX should be changed to be an
expression that only evaluates each operand once, and before every
version we forget to change it and we recheck that the code doesn't
misuse them. Let's fix them now.

include/haproxy/compat.h

index 0fe5a0b2a78bff45292829155f160e991f100f0d..3829060b7c4599f447c56d2e4e8f049b2911bdcf 100644 (file)
@@ -94,11 +94,19 @@ typedef struct { } empty_t;
 #endif
 
 #ifndef MIN
-#define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#define MIN(a, b) ({                           \
+       typeof(a) _a = (a);                     \
+       typeof(a) _b = (b);                     \
+       ((_a < _b) ? _a : _b);                  \
+})
 #endif
 
 #ifndef MAX
-#define MAX(a, b) (((a) > (b)) ? (a) : (b))
+#define MAX(a, b) ({                           \
+       typeof(a) _a = (a);                     \
+       typeof(a) _b = (b);                     \
+       ((_a > _b) ? _a : _b);                  \
+})
 #endif
 
 /* this is for libc5 for example */