From 5ce4b94b7ac1d529b42ac76609b686d1ba0dffcb Mon Sep 17 00:00:00 2001 From: jvoisin Date: Mon, 11 Sep 2023 17:41:31 +0200 Subject: Add the warn_unused_result attribute to alloc-related functions --- include/fortify-headers.h | 7 +++++++ include/stdlib.h | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/include/fortify-headers.h b/include/fortify-headers.h index dbace47..74c814b 100644 --- a/include/fortify-headers.h +++ b/include/fortify-headers.h @@ -110,6 +110,13 @@ #define __error_if(cond, msg) #endif +#if __has_attribute (warn_unused_result) +#define __warn_unused_result __attribute__ ((warn_unused_result)) +#else +#define __warn_unused_result +#endif + + #endif /* __has_attribute */ /* diff --git a/include/stdlib.h b/include/stdlib.h index 4efdb82..2671280 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -42,6 +42,7 @@ extern "C" { __malloc(malloc (free, 1)) __alloc_size(1) +__warn_unused_result __diagnose_as_builtin(__builtin_malloc, 1) _FORTIFY_FN(malloc) void *malloc(size_t __s) { @@ -49,6 +50,7 @@ _FORTIFY_FN(malloc) void *malloc(size_t __s) } __alloc_size(2) +__warn_unused_result __diagnose_as_builtin(__builtin_realloc, 1, 2) _FORTIFY_FN(realloc) void *realloc(void *__p, size_t __s) { @@ -56,6 +58,7 @@ _FORTIFY_FN(realloc) void *realloc(void *__p, size_t __s) } __alloc_size(1, 2) +__warn_unused_result __diagnose_as_builtin(__builtin_calloc, 1, 2) _FORTIFY_FN(calloc) void *calloc(size_t __n, size_t __s) { @@ -65,6 +68,7 @@ _FORTIFY_FN(calloc) void *calloc(size_t __n, size_t __s) #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) #undef reallocarray __alloc_size (2, 3) +__warn_unused_result __diagnose_as_builtin(__builtin_reallocarray, 1, 2, 3) _FORTIFY_FN(reallocarray) void* reallocarray(void* __p, size_t __n, size_t __s) { -- cgit v1.3