]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUILD: Fix build when using clang without optimizing.
authorOlivier Houchard <cognet@ci0.org>
Fri, 5 Mar 2021 15:42:14 +0000 (16:42 +0100)
committerOlivier Houchard <cognet@ci0.org>
Fri, 5 Mar 2021 15:58:56 +0000 (16:58 +0100)
ha_free() uses code that attempts to set a non-existant variable to provoke
a link-time error, with the expectation that the compiler will not omit that
if the code is unreachable. However, clang will emit it when compiling with
no optimization, so only do that if __OPTIMIZE__ is defined.

include/haproxy/bug.h

index 4f0df6ddae202486de525db3d112904c9cf1e3d4..57b7fc061c9cd037c9df71a743d8a316a2a01dd7 100644 (file)
 #define BUG_ON(cond)
 #endif
 
+/* When not optimizing, clang won't remove that code, so only compile it in when optimizing */
+#ifdef __OPTIMIZE__
+#define HA_LINK_ERROR(what)                                                  \
+       do {                                                                 \
+               /* provoke a build-time error */                             \
+               extern volatile int what;                                    \
+               what = 1;                                                    \
+       } while (0)
+#else
+#define HA_LINK_ERROR(what)                                                  \
+       do {                                                                 \
+       } while (0)
+#endif /* __OPTIMIZE__ */
+
 /* more reliable free() that clears the pointer */
 #define ha_free(x) do {                                                        \
                typeof(x) __x = (x);                                    \
                if (__builtin_constant_p((x)) || __builtin_constant_p(*(x))) { \
-                       /* provoke a build-time error */                \
-                       extern volatile int call_to_ha_free_attempts_to_free_a_constant; \
-                       call_to_ha_free_attempts_to_free_a_constant = 1; \
+                       HA_LINK_ERROR(call_to_ha_free_attempts_to_free_a_constant); \
                }                                                       \
                free(*__x);                                             \
                *__x = NULL;                                            \
@@ -150,9 +162,7 @@ struct mem_stats {
        __asm__(".globl __start_mem_stats");                            \
        __asm__(".globl __stop_mem_stats");                             \
        if (__builtin_constant_p((x)) || __builtin_constant_p(*(x))) {  \
-               /* provoke a build-time error */                        \
-               extern volatile int call_to_ha_free_attempts_to_free_a_constant; \
-               call_to_ha_free_attempts_to_free_a_constant = 1;        \
+               HA_LINK_ERROR(call_to_ha_free_attempts_to_free_a_constant); \
        }                                                               \
        if (*__x)                                                       \
                _HA_ATOMIC_ADD(&_.calls, 1);                            \