BUILD: compat: provide relaxed versions of the MIN/MAX macros
authorWilly Tarreau <w@1wt.eu>
Wed, 6 Aug 2025 17:05:29 +0000 (19:05 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 1 Oct 2025 14:44:10 +0000 (16:44 +0200)
In 3.0 the MIN/MAX macros were converted to compound expressions with
commit 0999e3d959 ("CLEANUP: compat: make the MIN/MAX macros more
reliable"). However with older compilers these are not supported out
of code blocks (e.g. to initialize variables or struct members). This
is the case on Solaris 10 with gcc-5.5 when QUIC doesn't compile
anymore with the future pool registration:

  In file included from include/haproxy/quic_tx.h:26:0,
                   from src/quic_tx.c:15:
  include/haproxy/compat.h:106:19: error: braced-group within expression allowed only inside a function
   #define MAX(a, b) ({    \
                     ^
  include/haproxy/pool.h:41:11: note: in definition of macro '__REGISTER_POOL'
     .size = _size,           \
             ^
  ...
  include/haproxy/quic_tx-t.h:6:29: note: in expansion of macro 'MAX'
   #define QUIC_MAX_CC_BUFSIZE MAX(QUIC_INITIAL_IPV6_MTU, QUIC_INITIAL_IPV4_MTU)

Let's provide the old relaxed versions as _MIN/_MAX for use with constants
like such cases where it's certain that there is no risk. A previous attempt
using __builtin_constant_p() to switch between the variants did not work,
and it's really not worth the hassle of going this far.

(cherry picked from commit cf8871ae40ae85f86b0d396b6af1eb657134ffc1)
Signed-off-by: Amaury Denoyelle <adenoyelle@haproxy.com>
(cherry picked from commit a245298b3c05826834d0c5925a1eb74fe22d4392)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit 2ae6edb59f8acf7fe0342c08e1d939c691004cf2)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>

include/haproxy/compat.h

index 0e4844d..1bc25ab 100644 (file)
@@ -93,11 +93,21 @@ typedef struct { } empty_t;
 # endif
 #endif
 
+/* unsafe ones for use with constant macros needed in initializers */
+#ifndef _MIN
+#define _MIN(a, b) ((a < b) ? a : b)
+#endif
+
+#ifndef _MAX
+#define _MAX(a, b) ((a > b) ? a : b)
+#endif
+
+/* safe versions for use anywhere except in initializers */
 #ifndef MIN
 #define MIN(a, b) ({                           \
        typeof(a) _a = (a);                     \
        typeof(a) _b = (b);                     \
-       ((_a < _b) ? _a : _b);                  \
+       _MIN(_a, _b);                           \
 })
 #endif
 
@@ -105,7 +115,7 @@ typedef struct { } empty_t;
 #define MAX(a, b) ({                           \
        typeof(a) _a = (a);                     \
        typeof(a) _b = (b);                     \
-       ((_a > _b) ? _a : _b);                  \
+       _MAX(_a, _b);                           \
 })
 #endif