fortify-headers

standalone fortify-source implementation
git clone git://git.2f30.org/fortify-headers
Log | Files | Refs | README | LICENSE

commit 6a801d8a749da3889ec57030cee09d1d60b4663e
parent 76fef96b2b29fc90cdfea3e595ab98d267c6b965
Author: jvoisin <julien.voisin@dustri.org>
Date:   Sun,  1 Oct 2023 22:00:21 +0200

Add `fprintf`

Diffstat:
Minclude/stdio.h | 21++++++++++++++++++---
Mtests/Makefile | 1+
2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/include/stdio.h b/include/stdio.h @@ -36,8 +36,6 @@ extern "C" { #undef fread #undef fwrite #undef popen -#undef printf -#undef vprintf #undef vsnprintf #undef vsprintf @@ -185,6 +183,7 @@ _FORTIFY_FN(vsprintf) int vsprintf(char * _FORTIFY_POS0 __s, const char *__f, } #ifndef __clang__ /* FIXME */ +#undef vprintf __access(read_only, 1) __format(printf, 1, 0) #if __has_builtin(__builtin_vprintf) @@ -198,7 +197,7 @@ _FORTIFY_FN(vprintf) int vprintf(const char *__f, __builtin_va_list __v) return __orig_vprintf(__f, __v); #endif } -#endif +#endif // __clang__ #if __has_builtin(__builtin_va_arg_pack) @@ -219,6 +218,8 @@ _FORTIFY_FN(vprintf) int vprintf(const char *__f, __builtin_va_list __v) #undef snprintf #undef sprintf +#undef printf +#undef fprintf __access(read_write, 1, 2) __access(read_only, 3) @@ -270,6 +271,20 @@ _FORTIFY_FN(printf) int printf(const char *__f, ...) #endif } +__access(read_only, 2) +__format(printf, 2, 0) +#if __has_builtin(__builtin_fprintf) +__diagnose_as_builtin(__builtin_fprintf, 2, 3) +#endif +_FORTIFY_FN(fprintf) int fprintf(FILE *__s, const char *__f, ...) +{ +#if __has_builtin(__builtin___fprintf_chk) && USE_NATIVE_CHK + return __builtin___fprintf_chk(_FORTIFY_SOURCE, __s, __f, __builtin_va_arg_pack()); +#else + return __orig_fprintf(__s, __f, __builtin_va_arg_pack()); +#endif +} + #endif /* __has_builtin(__builtin_va_arg_pack) */ #ifdef __cplusplus diff --git a/tests/Makefile b/tests/Makefile @@ -22,6 +22,7 @@ RUNTIME_TARGETS= \ test_fgets_static \ test_fgetws_dynamic \ test_fgetws_static \ + test_fprintf \ test_fread_int_overflow \ test_fread_overwrite_dynamic \ test_fread_overwrite_static \