fortify-headers

standalone fortify-source implementation
git clone git://git.2f30.org/fortify-headers
Log | Files | Refs | README | LICENSE

commit 1a8431430ec4b97f0baaf2bf3b385be5f5da08cf
parent 22a8094b41b2606084dc0c0c70487e5ed0fcb652
Author: jvoisin <julien.voisin@dustri.org>
Date:   Sat, 13 Jan 2024 20:34:19 +0100

Properly check for builtins

Clang's [documentation](https://clang.llvm.org/docs/LanguageExtensions.html#has-builtin) says:

 > __has_builtin should not be used to detect support for a builtin macro; use #ifdef instead.

So we're now using both, since it's often tedious/non-trivial to find out
what is a macro and what is a compiler builtin, across compilers and C
versions.

Diffstat:
Minclude/fortify-headers.h | 12++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/include/fortify-headers.h b/include/fortify-headers.h @@ -21,7 +21,11 @@ #error a compiler with __has_builtin support is required #endif -#if ! __has_builtin(__builtin_trap) +// https://clang.llvm.org/docs/LanguageExtensions.html#has-builtin +// > __has_builtin should not be used to detect support for a builtin macro; use #ifdef instead. +#define __fh_has_builtin(x) (__has_builtin(x) || defined(x)) + +#if ! __fh_has_builtin(__builtin_trap) #define __builtin_trap abort #endif @@ -31,7 +35,7 @@ #ifdef __clang__ -#if _FORTIFY_SOURCE > 2 && __has_builtin (__builtin_dynamic_object_size) && __has_attribute(pass_dynamic_object_size) +#if _FORTIFY_SOURCE > 2 && __fh_has_builtin (__builtin_dynamic_object_size) && __has_attribute(pass_dynamic_object_size) #define _FORTIFY_POSN(n) const __attribute__((pass_dynamic_object_size(n))) #else #define _FORTIFY_POSN(n) const __attribute__((pass_object_size(n))) @@ -66,7 +70,7 @@ #define _FORTIFY_FN(fn) _FORTIFY_FNB(fn); _FORTIFY_INLINE /* Use __builtin_dynamic_object_size with _FORTIFY_SOURCE>2, if available. */ -#if _FORTIFY_SOURCE > 2 && __has_builtin (__builtin_dynamic_object_size) +#if _FORTIFY_SOURCE > 2 && __fh_has_builtin (__builtin_dynamic_object_size) /* * See: * - https://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html @@ -151,7 +155,7 @@ * - https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html * - https://clang.llvm.org/docs/LanguageExtensions.html#checked-arithmetic-builtins */ -#if __has_builtin (__builtin_mul_overflow_p) +#if __fh_has_builtin (__builtin_mul_overflow_p) #define __bmo(x, y) (x != 0 && __builtin_mul_overflow_p(x, y, (__typeof__ ((x) + (y))) 0)) #else /* !__builtin_mul_overflow_p */ #define __bmo(x, y) (x != 0 && (x * y) / x != y)