From: Willy Tarreau Date: Fri, 17 May 2024 13:25:26 +0000 (+0200) Subject: CLEANUP: compat: make the MIN/MAX macros more reliable X-Git-Tag: v3.0-dev12~21 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0999e3d9597c5a1ad01a4288ee0093a58d6f75d0;p=thirdparty%2Fhaproxy.git 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. --- diff --git a/include/haproxy/compat.h b/include/haproxy/compat.h index 0fe5a0b2a7..3829060b7c 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 */