fortify-headers

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

commit a255506ca487250255f9f048e61cf90166ceab77
parent 7fd984fcb532be01f68cddc194c09a7ca10c1ea6
Author: sin <sin@2f30.org>
Date:   Wed, 15 Jul 2015 15:53:41 +0100

Fix wcrtomb() check

This was breaking valid code, example:
char c;
wcrtomb(&c, L'0', st);

Diffstat:
Minclude/wchar.h | 15+++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/include/wchar.h b/include/wchar.h @@ -17,8 +17,12 @@ #define _FORTIFY_WCHAR_H __extension__ +#include_next <limits.h> +__extension__ #include_next <stdlib.h> __extension__ +#include_next <string.h> +__extension__ #include_next <wchar.h> #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 @@ -99,10 +103,17 @@ _FORTIFY_FN(mbstowcs) size_t mbstowcs(wchar_t *__ws, const char *__s, size_t __w _FORTIFY_FN(wcrtomb) size_t wcrtomb(char *__s, wchar_t __w, mbstate_t *__st) { + char __buf[MB_LEN_MAX]; size_t __b = __builtin_object_size(__s, 0); + size_t __r; - if (__s && MB_CUR_MAX > __b) - __builtin_trap(); + if (__s) { + __r = __orig_wcrtomb(__buf, __w, __st); + if (__r > __b) + __builtin_trap(); + memcpy(__s, __buf, __r); + return __r; + } return __orig_wcrtomb(__s, __w, __st); }