fortify-headers

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

commit 2f6dc9f34ff30887dd81d98e7e2a2b7e7ff8b431
parent f54195479d315568ad4f7541ef33dc1d36a986b8
Author: sin <sin@2f30.org>
Date:   Thu, 29 Jan 2015 00:12:59 +0000

Add fgets() checks

Diffstat:
Minclude/stdio.h | 12++++++++++++
1 file changed, 12 insertions(+), 0 deletions(-)

diff --git a/include/stdio.h b/include/stdio.h @@ -5,6 +5,16 @@ #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 +static inline __attribute__ ((always_inline)) +char *__fortify_fgets(char *s, int n, FILE *fp) +{ + size_t bos = __builtin_object_size(s, 0); + + if ((size_t)n > bos) + __builtin_trap(); + return fgets(s, n, fp); +} + static inline __attribute__ ((always_inline)) __attribute__ ((__format__ (printf, 3, 0))) @@ -20,6 +30,8 @@ __fortify_vsnprintf(char *__restrict s, size_t n, const char *__restrict fmt, return vsnprintf(s, n, fmt, ap); } +#undef fgets +#define fgets(s, n, fp) __fortify_fgets(s, n, fp) #undef vsnprintf #define vsnprintf(s, n, fmt, ap) __fortify_vsnprintf(s, n, fmt, ap) #undef snprintf