fortify-headers

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

commit e2a76a9502415f2bbbc83d634afb4991da2ea960
parent 1872b6a7e567a6f983b3db973ccddd0f9b045b34
Author: sin <sin@2f30.org>
Date:   Wed, 28 Jan 2015 16:54:48 +0000

Add vsnprintf() checks

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

diff --git a/include/stdio.h b/include/stdio.h @@ -0,0 +1,28 @@ +#ifndef FORTIFY_STDIO_H_ +#define FORTIFY_STDIO_H_ + +#include_next <stdio.h> + +#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 + +static inline +__attribute__ ((always_inline)) +__attribute__ ((__format__ (printf, 3, 0))) +__attribute__ ((__nonnull__ (3))) +int __fortify_vsnprintf(char *__restrict s, size_t n, const char *__restrict fmt, __va_list ap) +{ + size_t bos = __builtin_object_size(s, 0); + + if (bos == (size_t)-1) + return vsnprintf(s, n, fmt, ap); + if (__builtin_constant_p(n) && n > bos) + __builtin_trap(); + return vsnprintf(s, n, fmt, ap); +} + +#undef vsnprintf +#define vsnprintf(s, n, fmt, ap) __fortify_vsnprintf(s, n, fmt, ap) + +#endif + +#endif