fortify-headers

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

commit 0826c593bee3c68261318decc5475e45c4b030ec
parent e71d1fd257717987239380f52c25026e634c6a81
Author: sin <sin@2f30.org>
Date:   Sun,  1 Mar 2015 11:09:13 +0000

Add sprintf() check

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

diff --git a/include/stdio.h b/include/stdio.h @@ -41,7 +41,21 @@ __fortify_vsnprintf(char *s, size_t n, const char *fmt, __builtin_va_list ap) size_t bos = __builtin_object_size(s, 0); \ if (_n > bos) \ __builtin_trap(); \ - snprintf(s, _n, fmt, ## __VA_ARGS__); \ + (snprintf)(s, _n, fmt, ## __VA_ARGS__); \ +}) + +#undef sprintf +#define sprintf(s, fmt, ...) ({ \ + size_t bos = __builtin_object_size(s, 0); \ + int r; \ + if (bos != -1) { \ + r = (snprintf)(s, bos, fmt, ## __VA_ARGS__); \ + if (r == -1 || r >= bos) \ + __builtin_trap(); \ + } else { \ + r = (sprintf)(s, fmt, ## __VA_ARGS__); \ + } \ + r; \ }) #endif