From 7b00e31509c46d70ddc55189ebf22d691cbea266 Mon Sep 17 00:00:00 2001 From: Olivier Houchard Date: Fri, 5 Mar 2021 16:42:14 +0100 Subject: [PATCH] BUILD: Fix build when using clang without optimizing. 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 | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/include/haproxy/bug.h b/include/haproxy/bug.h index 4f0df6dda..57b7fc061 100644 --- a/include/haproxy/bug.h +++ b/include/haproxy/bug.h @@ -73,13 +73,25 @@ #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); \