fortify-headers

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

commit 8d090c8a1a86408fa8b54029b97fccd17f9d35d5
parent c607773a80e6685ab4c922245c33cf2ea5dcfb72
Author: q66 <q66@chimera-linux.org>
Date:   Fri, 15 Dec 2023 23:05:29 +0100

Guard some conditional stdio APIs with the right macros

Fixes https://github.com/jvoisin/fortify-headers/issues/34

Diffstat:
Minclude/stdio.h | 53++++++++++++++++++++++++++++++-----------------------
1 file changed, 30 insertions(+), 23 deletions(-)

diff --git a/include/stdio.h b/include/stdio.h @@ -29,16 +29,21 @@ __extension__ extern "C" { #endif -#undef fdopen #undef fgets -#undef fmemopen #undef fopen #undef fread #undef fwrite -#undef popen #undef vsnprintf #undef vsprintf +#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ + || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ + || defined(_BSD_SOURCE) + +#undef fdopen +#undef fmemopen +#undef popen + __access(read_only, 2) #if __has_builtin(__builtin_fdopen) __diagnose_as_builtin(__builtin_fdopen, 1, 2) @@ -48,6 +53,28 @@ _FORTIFY_FN(fdopen) FILE *fdopen(int __f, const char* _FORTIFY_POS0 __m) return __orig_fdopen(__f, __m); } +__malloc(malloc (fclose, 1)) +#if __has_builtin(__builtin_fmemopen) +__diagnose_as_builtin(__builtin_fmemopen, 1, 2, 3) +#endif +_FORTIFY_FN(fmemopen) FILE *fmemopen(void* _FORTIFY_POS0 __b, size_t __s, const char* _FORTIFY_POS0 __m) +{ + return __orig_fmemopen(__b, __s, __m); +} + +__access(read_only, 1) +__access(read_only, 2) +__malloc(malloc (pclose, 1)) +#if __has_builtin(__builtin_popen) +__diagnose_as_builtin(__builtin_popen, 1, 2) +#endif +_FORTIFY_FN(popen) FILE *popen(const char* _FORTIFY_POS0 __c, const char* _FORTIFY_POS0 __t) +{ + return __orig_popen(__c, __t); +} + +#endif /* _POSIX_SOURCE || _POSIX_C_SOURCE || _XOPEN_SOURCE || _GNU_SOURCE || _BSD_SOURCE */ + __access(write_only, 1, 2) #if __has_builtin(__builtin_fgets) __diagnose_as_builtin(__builtin_fgets, 1, 2, 3) @@ -61,15 +88,6 @@ _FORTIFY_FN(fgets) char *fgets(char * _FORTIFY_POS0 __s, int __n, FILE *__f) return __orig_fgets(__s, __n, __f); } -__malloc(malloc (fclose, 1)) -#if __has_builtin(__builtin_fmemopen) -__diagnose_as_builtin(__builtin_fmemopen, 1, 2, 3) -#endif -_FORTIFY_FN(fmemopen) FILE *fmemopen(void* _FORTIFY_POS0 __b, size_t __s, const char* _FORTIFY_POS0 __m) -{ - return __orig_fmemopen(__b, __s, __m); -} - __access(read_only, 1) __access(read_only, 2) __malloc(malloc (fclose, 1)) @@ -113,17 +131,6 @@ _FORTIFY_FN(fwrite) size_t fwrite(const void * _FORTIFY_POS0 __d, size_t __n, return __orig_fwrite(__d, __n, __m, __f); } -__access(read_only, 1) -__access(read_only, 2) -__malloc(malloc (pclose, 1)) -#if __has_builtin(__builtin_popen) -__diagnose_as_builtin(__builtin_popen, 1, 2) -#endif -_FORTIFY_FN(popen) FILE *popen(const char* _FORTIFY_POS0 __c, const char* _FORTIFY_POS0 __t) -{ - return __orig_popen(__c, __t); -} - #ifndef __clang__ /* FIXME */ #undef tmpfile __malloc(malloc (fclose, 1))