From 4e5b57095bee4cd50e2095ad7043f5a885c2c180 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Fri, 14 Nov 2025 15:06:23 +0100 Subject: Add `format` annotations --- include/fortify-headers.h | 11 +++++++++++ include/stdio.h | 2 ++ 2 files changed, 13 insertions(+) diff --git a/include/fortify-headers.h b/include/fortify-headers.h index a9b7498..deff351 100644 --- a/include/fortify-headers.h +++ b/include/fortify-headers.h @@ -70,4 +70,15 @@ #define __access(...) #endif +/* This needs to be two different conditions: https://gcc.gnu.org/onlinedocs/cpp/_005f_005fhas_005fattribute.html */ +#if defined __has_attribute +#if __has_attribute (format) +#define __format(...) __attribute__ ((format (__VA_ARGS__))) +#else +#define __format(...) +#endif +#else +#define __format(...) +#endif + #endif diff --git a/include/stdio.h b/include/stdio.h index 67738ed..52032ae 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -121,6 +121,7 @@ _FORTIFY_FN(vsprintf) int vsprintf(char * _FORTIFY_POS0 __s, const char *__f, * 3) not implementing these under clang, which is what we do for now */ +__format(printf, 3, 4) _FORTIFY_FN(snprintf) int snprintf(char *__s, size_t __n, const char *__f, ...) { @@ -131,6 +132,7 @@ _FORTIFY_FN(snprintf) int snprintf(char *__s, size_t __n, return __orig_snprintf(__s, __n, __f, __builtin_va_arg_pack()); } +__format(printf, 2, 3) _FORTIFY_FN(sprintf) int sprintf(char *__s, const char *__f, ...) { size_t __b = __bos(__s, 0); -- cgit v1.3