From b333df29dcfabcd4ad0cf630fdb648eaa38ec1c7 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 9 Jul 2023 18:50:30 +0200 Subject: Add some `format` annotations --- include/fortify-headers.h | 6 ++++++ include/stdio.h | 2 ++ tests/test_vsprintf.c | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/include/fortify-headers.h b/include/fortify-headers.h index 08b8dd3..24af881 100644 --- a/include/fortify-headers.h +++ b/include/fortify-headers.h @@ -60,6 +60,12 @@ #define __access(...) #endif +#if defined __has_attribute && __has_attribute (format) +#define __format(...) __attribute__ ((format (__VA_ARGS__))) +#else +#define __format(...) +#endif + /* TODO(jvoisin) Figure a nice way to make use of __builtin_mul_overflow while ignoring the result. */ /* TODO(jvoisin) Make use of C23's stdckdint header: https://gustedt.gitlabpages.inria.fr/c23-library/#stdckdint */ diff --git a/include/stdio.h b/include/stdio.h index d5206f5..5d589f0 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -116,6 +116,7 @@ _FORTIFY_FN(vsprintf) int vsprintf(char * _FORTIFY_POS0 __s, const char *__f, */ __access(read_write, 1, 2) +__format(printf, 3, 4) _FORTIFY_FN(snprintf) int snprintf(char *__s, size_t __n, const char *__f, ...) { @@ -126,6 +127,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); diff --git a/tests/test_vsprintf.c b/tests/test_vsprintf.c index 740816f..4e27201 100644 --- a/tests/test_vsprintf.c +++ b/tests/test_vsprintf.c @@ -12,7 +12,7 @@ int msg_valid(const char * format, ... ) { va_end (args); } -int msg(int n, const char * format, ... ) { +int msg(const char * format, ... ) { va_list args; va_start (args, format); CHK_FAIL_START -- cgit v1.3