From fd4332dbcd5227fde96e7bc128418d834b5b910f Mon Sep 17 00:00:00 2001 From: jvoisin Date: Wed, 20 Sep 2023 18:05:27 +0200 Subject: Add tests for compile-time errors --- include/fortify-headers.h | 6 +++--- include/string.h | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/fortify-headers.h b/include/fortify-headers.h index 3eaed7c..bc7bf00 100644 --- a/include/fortify-headers.h +++ b/include/fortify-headers.h @@ -106,9 +106,9 @@ #define __diagnose_as_builtin(...) #endif -#if __has_attribute (__diagnose_if) -#define __warning_if(cond, msg) __attribute__ ((__diagnose_if (cond, msg, "warning"))) -#define __error_if(cond, msg) __attribute__ ((__diagnose_if (cond, msg, "error"))) +#if __has_attribute (diagnose_if) +#define __warning_if(cond, msg) __attribute__ ((diagnose_if (cond, msg, "warning"))) +#define __error_if(cond, msg) __attribute__ ((diagnose_if (cond, msg, "error"))) #else #define __warning_if(cond, msg) #define __error_if(cond, msg) diff --git a/include/string.h b/include/string.h index be13cb4..010b7f2 100644 --- a/include/string.h +++ b/include/string.h @@ -43,6 +43,7 @@ __access(read_only, 2, 3) __diagnose_as_builtin(__builtin_memcpy, 1, 2, 3) _FORTIFY_FN(memcpy) void *memcpy(void * _FORTIFY_POS0 __od, const void * _FORTIFY_POS0 __os, size_t __n) +__error_if((__bos(__od, 0) < __n), "'memcpy' called with `n` bigger than the size of `d`.") { size_t __bd = __bos(__od, 0); size_t __bs = __bos(__os, 0); @@ -72,8 +73,8 @@ _FORTIFY_FN(memmove) void *memmove(void * _FORTIFY_POS0 __d, __access(write_only, 1, 3) __diagnose_as_builtin(__builtin_memset, 1, 2, 3) -__warning_if(__c != 0 && __n == 0, "'memset' will set `0` bytes; did you invert the arguments?") _FORTIFY_FN(memset) void *memset(void * _FORTIFY_POS0 __d, int __c, size_t __n) +__warning_if(__c != 0 && __n == 0, "'memset' will set `0` bytes; did you invert the arguments?") { size_t __b = __bos(__d, 0); -- cgit v1.3