fortify-headers

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

commit 5960e3364f54eebfbfe509abc6c629d26171734b
parent b4a9df1a083cd519005895e81433a685cde3fc34
Author: sin <sin@2f30.org>
Date:   Wed, 28 Jan 2015 17:08:27 +0000

Add snprintf() checks

We need to use a variadic macro in this case because GCC doesn't
allow inline functions with variable argument lists.

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

diff --git a/include/stdio.h b/include/stdio.h @@ -13,15 +13,21 @@ int __fortify_vsnprintf(char *__restrict s, size_t n, const char *__restrict fmt { 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) + if (n > bos) __builtin_trap(); return vsnprintf(s, n, fmt, ap); } #undef vsnprintf #define vsnprintf(s, n, fmt, ap) __fortify_vsnprintf(s, n, fmt, ap) +#undef snprintf +#define snprintf(s, n, fmt, ...) ({ \ + size_t _n = (n); \ + size_t bos = __builtin_object_size(s, 0); \ + if (n > bos) \ + __builtin_trap(); \ + snprintf(s, n, fmt, __VA_ARGS__); \ +}) #endif