fortify-headers

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

commit 15c2b9d8df9c4a2959abe5dee488f741f9aeb223
parent 0c985218e9043289f4e72d1492cdb83f07fe600a
Author: sin <sin@2f30.org>
Date:   Sun,  1 Mar 2015 13:17:21 +0000

Add fwrite() check

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

diff --git a/include/stdio.h b/include/stdio.h @@ -29,6 +29,19 @@ __fortify_fread(void *dst, size_t n, size_t nmemb, FILE *fp) return fread(dst, n, nmemb, fp); } +static inline __attribute__ ((always_inline)) +size_t +__fortify_fwrite(const void *dst, size_t n, size_t nmemb, FILE *fp) +{ + size_t bos = __builtin_object_size(dst, 0); + + if (n != 0 && (n * nmemb) / n != nmemb) + __builtin_trap(); + if (n * nmemb > bos) + __builtin_trap(); + return fwrite(dst, n, nmemb, fp); +} + static inline __attribute__ ((always_inline)) __attribute__ ((__format__ (printf, 2, 0))) @@ -67,6 +80,8 @@ __fortify_vsnprintf(char *s, size_t n, const char *fmt, __builtin_va_list ap) #define fgets(s, n, fp) __fortify_fgets(s, n, fp) #undef fread #define fread(dst, n, nmemb, fp) __fortify_fread(dst, n, nmemb, fp) +#undef fwrite +#define fwrite(dst, n, nmemb, fp) __fortify_fwrite(dst, n, nmemb, fp) #undef vsprintf #define vsprintf(s, fmt, ap) __fortify_vsprintf(s, fmt, ap) #undef vsnprintf