From a28cd72ce6fa6f5619f0952a9569f046a13770d2 Mon Sep 17 00:00:00 2001 From: sin Date: Wed, 4 Mar 2015 12:27:04 +0000 Subject: Add wcrtomb(), wcscat() and wcscpy() checks --- include/wchar.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'include/wchar.h') diff --git a/include/wchar.h b/include/wchar.h index b3a1dc7..69c2188 100644 --- a/include/wchar.h +++ b/include/wchar.h @@ -1,6 +1,7 @@ #ifndef FORTIFY_WCHAR_H_ #define FORTIFY_WCHAR_H_ +#include_next #include_next #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 @@ -51,6 +52,39 @@ __fortify_mbstowcs(wchar_t *d, const char *s, size_t n) return mbstowcs(d, s, n); } +static inline __attribute__ ((always_inline)) +size_t +__fortify_wcrtomb(char *s, wchar_t wc, mbstate_t *st) +{ + size_t bos = __builtin_object_size(s, 0); + + if (MB_CUR_MAX > bos) + __builtin_trap(); + return wcrtomb(s, wc, st); +} + +static inline __attribute__ ((always_inline)) +wchar_t * +__fortify_wcscat(wchar_t *d, const wchar_t *s) +{ + size_t bos = __builtin_object_size(d, 0); + + if (wcslen(s) + wcslen(d) + 1 > bos / sizeof(wchar_t)) + __builtin_trap(); + return wcscat(d, s); +} + +static inline __attribute__ ((always_inline)) +wchar_t * +__fortify_wcscpy(wchar_t *d, const wchar_t *s) +{ + size_t bos = __builtin_object_size(d, 0); + + if (wcslen(s) + 1 > bos / sizeof(wchar_t)) + __builtin_trap(); + return wcscpy(d, s); +} + static inline __attribute__ ((always_inline)) wchar_t * __fortify_wmemcpy(wchar_t *d, const wchar_t *s, size_t n) @@ -92,6 +126,12 @@ __fortify_wmemset(wchar_t *s, wchar_t c, size_t n) #define mbsrtowcs(d, s, wn, st) __fortify_mbsrtowcs(d, s, wn, st) #undef mbstowcs #define mbstowcs(d, s, n) __fortify_mbstowcs(d, s, n) +#undef wcrtomb +#define wcrtomb(s, wc, st) __fortify_wcrtomb(s, wc, st) +#undef wcscat +#define wcscat(d, s) __fortify_wcscat(d, s) +#undef wcscpy +#define wcscpy(d, s) __fortify_wcscpy(d, s) #undef wmemcpy #define wmemcpy(d, s, n) __fortify_wmemcpy(d, s, n) #undef wmemmove -- cgit v1.3