commit e9b6cb7e2eb7029902ae92ef43fb905e5072785b
parent bf242b15e1f7873bdb940c2441b5e57747bc2f81
Author: jvoisin <julien.voisin@dustri.org>
Date: Fri, 14 Nov 2025 15:06:23 +0100
Add `format` annotations
Diffstat:
2 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/include/fortify-headers.h b/include/fortify-headers.h
@@ -59,15 +59,20 @@
#define __bos(ptr, type) __builtin_object_size (ptr, type)
#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 (access)
#define __access(...) __attribute__ ((access (__VA_ARGS__)))
#else
#define __access(...)
#endif
+
+#if __has_attribute (format)
+#define __format(...) __attribute__ ((format (__VA_ARGS__)))
#else
-#define __access(...)
+#define __format(...)
+#endif
+
#endif
#endif
diff --git a/include/stdio.h b/include/stdio.h
@@ -76,6 +76,7 @@ _FORTIFY_FN(fwrite) size_t fwrite(const void * _FORTIFY_POS0 __d, size_t __n,
return __orig_fwrite(__d, __n, __m, __f);
}
+__format(printf, 3, 0)
__access(write_only, 1, 2)
__access(read_only, 3)
_FORTIFY_FN(vsnprintf) int vsnprintf(char * _FORTIFY_POS0 __s, size_t __n,
@@ -88,6 +89,7 @@ _FORTIFY_FN(vsnprintf) int vsnprintf(char * _FORTIFY_POS0 __s, size_t __n,
return __orig_vsnprintf(__s, __n, __f, __v);
}
+__format(printf, 2, 0)
__access(write_only, 1)
__access(read_only, 2)
_FORTIFY_FN(vsprintf) int vsprintf(char * _FORTIFY_POS0 __s, const char *__f,
@@ -121,6 +123,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 +134,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);