fortify-headers

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

commit 265fa03fa0c467c9c41d803ebe2a538e758cba20
parent 8ed72e7c1caa0eb4238e649063c26a11720935de
Author: jvoisin <julien.voisin@dustri.org>
Date:   Wed, 24 Apr 2024 14:49:52 +0200

Disable pedantic checks by default

They can be re-enabled via `PEDANTIC_CHECKS`

Diffstat:
MREADME.md | 6++++--
Minclude/string.h | 9++++++++-
Mtests/Makefile | 2+-
3 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/README.md b/README.md @@ -28,8 +28,10 @@ on Clang. It was initially intended to be used on [significant coverage](https://jvoisin.github.io/fortify-headers/) - Defining `USE_NATIVE_CHK` will make use of compiler-provided builtin `_chk` functions, which might be a bit better in term of diagnostics, - but won't necesarily provide the same amount of security checks. - + but won't necessarily provide the same amount of security checks. +- Defining `PEDANTIC_CHECKS` will enable pedantic checks, that while technically + correct, might break some programs relying on widely accepted + undefined-behaviours. # Sample usage diff --git a/include/string.h b/include/string.h @@ -51,8 +51,10 @@ __error_if((__fh_bos(__od, 0) < __n), "'memcpy' called with `n` bigger than the #if __has_builtin(__builtin___memcpy_chk) && USE_NATIVE_CHK return __builtin___memcpy_chk(__od, __os, __n, __fh_bos(__od, 0)); #else +#if defined PEDANTIC_CHECKS if (!__od || !__os) __builtin_trap(); +#endif __fh_size_t __bd = __fh_bos(__od, 0); __fh_size_t __bs = __fh_bos(__os, 0); @@ -78,8 +80,10 @@ _FORTIFY_FN(memmove) void *memmove(void * _FORTIFY_POS0 __d, #if __has_builtin(__builtin___memmove_chk) && USE_NATIVE_CHK return __builtin___memmove_chk(__d, __s, __n, __fh_bos(__d, 0)); #else +#if defined PEDANTIC_CHECKS if (!__d || !__s) __builtin_trap(); +#endif __fh_size_t __bd = __fh_bos(__d, 0); __fh_size_t __bs = __fh_bos(__s, 0); @@ -100,8 +104,10 @@ __warning_if(__c != 0 && __n == 0, "'memset' will set `0` bytes; did you invert #if __has_builtin(__builtin___memset_chk) && USE_NATIVE_CHK return __builtin___memset_chk(__d, __c, __n, __fh_bos(__d, 0)); #else +#if defined PEDANTIC_CHECKS if (!__d) __builtin_trap(); +#endif __fh_size_t __b = __fh_bos(__d, 0); @@ -120,14 +126,15 @@ _FORTIFY_FN(memchr) void *memchr(const void * _FORTIFY_POS0 __d, int __c, size_t #if __has_builtin(__builtin___memchr_chk) && USE_NATIVE_CHK return __builtin___memchr_chk(__d, __c, __n, __fh_bos(__d, 0)); #else +#if defined PEDANTIC_CHECKS if (!__d) __builtin_trap(); - #if __STDC_VERSION__ < 201112L __fh_size_t __b = __fh_bos(__d, 0); if (__n > __b) __builtin_trap(); #endif +#endif return __builtin_memchr(__d, __c, __n); #endif diff --git a/tests/Makefile b/tests/Makefile @@ -1,4 +1,4 @@ -CFLAGS+=-I../include/ -D_FORTIFY_SOURCE=3 -static -O2 +CFLAGS+=-I../include/ -D_FORTIFY_SOURCE=3 -static -O2 -DPEDANTIC_CHECKS COMPTIME_TARGETS= \ test_memcpy_overwrite_under \