From c40b3cd03d963286f0f21f3299bd33b7ceaea3b1 Mon Sep 17 00:00:00 2001 From: Frantisek Sumsal Date: Sat, 30 Jan 2021 13:12:42 +0100 Subject: [PATCH] text-utils: correctly detect ASan under clang __SANITIZE_ADDRESS__ is not defined when compiling with clang, so cover both use cases with a special set of macros --- include/c.h | 17 +++++++++++++++++ include/closestream.h | 2 +- text-utils/col.c | 3 ++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/include/c.h b/include/c.h index 6b742512b3..b891ad4808 100644 --- a/include/c.h +++ b/include/c.h @@ -411,6 +411,23 @@ static inline int xusleep(useconds_t usec) #define stringify_value(s) stringify(s) #define stringify(s) #s +/* Detect if we're compiled with Address Sanitizer + * - gcc (__SANITIZE_ADDRESS__) + * - clang (__has_feature(address_sanitizer)) + */ +#if !defined(HAS_FEATURE_ADDRESS_SANITIZER) +# ifdef __SANITIZE_ADDRESS__ +# define HAS_FEATURE_ADDRESS_SANITIZER 1 +# elif defined(__has_feature) +# if __has_feature(address_sanitizer) +# define HAS_FEATURE_ADDRESS_SANITIZER 1 +# endif +# endif +# if !defined(HAS_FEATURE_ADDRESS_SANITIZER) +# define HAS_FEATURE_ADDRESS_SANITIZER 0 +# endif +#endif + /* * UL_ASAN_BLACKLIST is a macro to tell AddressSanitizer (a compile-time * instrumentation shipped with Clang and GCC) to not instrument the diff --git a/include/closestream.h b/include/closestream.h index 41afbe2082..8e5d18d977 100644 --- a/include/closestream.h +++ b/include/closestream.h @@ -83,7 +83,7 @@ close_stdout_atexit(void) /* * Note that close stdout at exit disables ASAN to report memory leaks */ -#if !defined(__SANITIZE_ADDRESS__) +#if !HAS_FEATURE_ADDRESS_SANITIZER atexit(close_stdout); #endif } diff --git a/text-utils/col.c b/text-utils/col.c index f9e39a2261..afea8aa625 100644 --- a/text-utils/col.c +++ b/text-utils/col.c @@ -61,6 +61,7 @@ #include #include +#include "c.h" #include "closestream.h" #include "nls.h" #include "optutils.h" @@ -89,7 +90,7 @@ /* number of lines to allocate */ #define NALLOC 64 -#if defined(__SANITIZE_ADDRESS__) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) +#if HAS_FEATURE_ADDRESS_SANITIZER || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) # define COL_DEALLOCATE_ON_EXIT #endif -- 2.47.3