From 0999e3d9597c5a1ad01a4288ee0093a58d6f75d0 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Fri, 17 May 2024 15:25:26 +0200 Subject: [PATCH] CLEANUP: compat: make the MIN/MAX macros more reliable 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 | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/include/haproxy/compat.h b/include/haproxy/compat.h index 0fe5a0b2a..3829060b7 100644 --- a/include/haproxy/compat.h +++ b/include/haproxy/compat.h @@ -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 */