From 9f8c543dc81f0c4239acae6713f5414eb7dc681d Mon Sep 17 00:00:00 2001 From: sin Date: Fri, 13 Mar 2015 11:00:46 +0000 Subject: Rework fortify implementation to use extern inline Overriding functions with macros is legal in C but a lot of software is not prepared for it. Use the extern inline method to achieve the same result. --- include/wchar.h | 201 +++++++++++++++++++++++++++----------------------------- 1 file changed, 98 insertions(+), 103 deletions(-) (limited to 'include/wchar.h') diff --git a/include/wchar.h b/include/wchar.h index 805f309..526275a 100644 --- a/include/wchar.h +++ b/include/wchar.h @@ -7,101 +7,123 @@ #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 #ifndef __cplusplus +#undef fgetws +#undef mbsnrtowcs +#undef mbsrtowcs +#undef mbstowcs +#undef wcrtomb +#undef wcscat +#undef wcscpy +#undef wcsncat +#undef wcsncpy +#undef wcsnrtombs +#undef wcsrtombs +#undef wcstombs +#undef wctomb +#undef wmemcpy +#undef wmemmove +#undef wmemset -static inline __attribute__ ((always_inline)) -wchar_t * -__fortify_fgetws(wchar_t *s, int n, FILE *fp) +extern wchar_t *__fgetws_orig(wchar_t *, int, FILE *) + __asm__(__USER_LABEL_PREFIX__ "fgetws"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +wchar_t *fgetws(wchar_t *s, int n, FILE *fp) { size_t bos = __builtin_object_size(s, 0); if ((size_t)n > bos / sizeof(wchar_t)) __builtin_trap(); - return fgetws(s, n, fp); + return __fgetws_orig(s, n, fp); } -static inline __attribute__ ((always_inline)) -size_t -__fortify_mbsnrtowcs(wchar_t *d, const char **s, size_t n, - size_t wn, mbstate_t *st) +extern size_t __mbsnrtowcs(wchar_t *, const char **, size_t, size_t, mbstate_t *) + __asm__(__USER_LABEL_PREFIX__ "mbsnrtowcs"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +size_t mbsnrtowcs(wchar_t *d, const char **s, size_t n, size_t wn, mbstate_t *st) { size_t bos = __builtin_object_size(d, 0); size_t r; if (wn > n / sizeof(wchar_t)) { bos /= sizeof(wchar_t); - r = mbsnrtowcs(d, s, n, wn > bos ? bos : wn, st); + r = __mbsnrtowcs_orig(d, s, n, wn > bos ? bos : wn, st); if (bos < wn && d && *s && r != (size_t)-1) __builtin_trap(); } else { - r = mbsnrtowcs(d, s, n > bos ? bos : n, wn, st); + r = __mbsnrtowcs_orig(d, s, n > bos ? bos : n, wn, st); if (bos < n && d && *s && r != (size_t)-1) __builtin_trap(); } return r; } -static inline __attribute__ ((always_inline)) -size_t -__fortify_mbsrtowcs(wchar_t *d, const char **s, - size_t wn, mbstate_t *st) +extern size_t __mbsrtowcs_orig(wchar_t *, const char **, size_t, mbstate_t *) + __asm__(__USER_LABEL_PREFIX__ "mbsrtowcs"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +size_t mbsrtowcs(wchar_t *d, const char **s, size_t wn, mbstate_t *st) { size_t bos = __builtin_object_size(d, 0); size_t r; bos /= sizeof(wchar_t); - r = mbsrtowcs(d, s, wn > bos ? bos : wn, st); + r = __mbsrtowcs_orig(d, s, wn > bos ? bos : wn, st); if (bos < wn && d && *s && r != (size_t)-1) __builtin_trap(); return r; } -static inline __attribute__ ((always_inline)) -size_t -__fortify_mbstowcs(wchar_t *ws, const char *s, size_t wn) +extern size_t __mbstowcs_orig(wchar_t *, const char *, size_t) + __asm__(__USER_LABEL_PREFIX__ "mbstowcs"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +size_t mbstowcs(wchar_t *ws, const char *s, size_t wn) { size_t bos = __builtin_object_size(ws, 0); if (ws && wn > bos / sizeof(wchar_t)) __builtin_trap(); - return mbstowcs(ws, s, wn); + return __mbstowcs_orig(ws, s, wn); } -static inline __attribute__ ((always_inline)) -size_t -__fortify_wcrtomb(char *s, wchar_t wc, mbstate_t *st) +extern size_t __wcrtomb_orig(char *, wchar_t, mbstate_t *) + __asm__(__USER_LABEL_PREFIX__ "wcrtomb"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +size_t wcrtomb(char *s, wchar_t wc, mbstate_t *st) { size_t bos = __builtin_object_size(s, 0); if (s && MB_CUR_MAX > bos) __builtin_trap(); - return wcrtomb(s, wc, st); + return __wcrtomb_orig(s, wc, st); } -static inline __attribute__ ((always_inline)) -wchar_t * -__fortify_wcscat(wchar_t *d, const wchar_t *s) +extern wchar_t *__wcscat_orig(wchar_t *, const wchar_t *) + __asm__(__USER_LABEL_PREFIX__ "wcscat"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +wchar_t *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); + return __wcscat_orig(d, s); } -static inline __attribute__ ((always_inline)) -wchar_t * -__fortify_wcscpy(wchar_t *d, const wchar_t *s) +extern wchar_t *__wcscpy_orig(wchar_t *, const wchar_t *) + __asm__(__USER_LABEL_PREFIX__ "wcscpy"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +wchar_t *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); + return __wcscpy_orig(d, s); } -static inline __attribute__ ((always_inline)) -wchar_t * -__fortify_wcsncat(wchar_t *d, const wchar_t *s, size_t n) +extern wchar_t *__wcsncat_orig(wchar_t *, const wchar_t *, size_t) + __asm__(__USER_LABEL_PREFIX__ "wcsncat"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +wchar_t *wcsncat(wchar_t *d, const wchar_t *s, size_t n) { size_t bos = __builtin_object_size(d, 0); size_t slen, dlen; @@ -114,143 +136,116 @@ __fortify_wcsncat(wchar_t *d, const wchar_t *s, size_t n) if (slen + dlen + 1 > bos / sizeof(wchar_t)) __builtin_trap(); } - return wcsncat(d, s, n); + return __wcsncat_orig(d, s, n); } -static inline __attribute__ ((always_inline)) -wchar_t * -__fortify_wcsncpy(wchar_t *d, const wchar_t *s, size_t n) +extern wchar_t *__wcsncpy_orig(wchar_t *, const wchar_t *, size_t) + __asm__(__USER_LABEL_PREFIX__ "wcsncpy"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +wchar_t *wcsncpy(wchar_t *d, const wchar_t *s, size_t n) { size_t bos = __builtin_object_size(d, 0); if (n > bos / sizeof(wchar_t)) __builtin_trap(); - return wcsncpy(d, s, n); + return __wcsncpy_orig(d, s, n); } -static inline __attribute__ ((always_inline)) -size_t -__fortify_wcsnrtombs(char *d, const wchar_t **s, size_t wn, - size_t n, mbstate_t *st) +extern size_t __wcsnrtombs_orig(char *, const wchar_t **, size_t, size_t, mbstate_t *) + __asm__(__USER_LABEL_PREFIX__ "wcsnrtombs"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +size_t wcsnrtombs(char *d, const wchar_t **s, size_t wn, size_t n, mbstate_t *st) { size_t bos = __builtin_object_size(d, 0); size_t r; if (wn > n / sizeof(wchar_t)) { bos /= sizeof(wchar_t); - r = wcsnrtombs(d, s, wn > bos ? bos : wn, n, st); + r = __wcsnrtombs_orig(d, s, wn > bos ? bos : wn, n, st); if (bos < wn && d && *s && r != (size_t)-1) __builtin_trap(); } else { - r = wcsnrtombs(d, s, wn, n > bos ? bos : n, st); + r = __wcsnrtombs_orig(d, s, wn, n > bos ? bos : n, st); if (bos < n && d && *s && r != (size_t)-1) __builtin_trap(); } return r; } -static inline __attribute__ ((always_inline)) -size_t -__fortify_wcsrtombs(char *d, const wchar_t **s, size_t n, - mbstate_t *st) +extern size_t __wcsrtombs_orig(char *, const wchar_t **, size_t, mbstate_t *) + __asm__(__USER_LABEL_PREFIX__ "wcsrtombs"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +size_t wcsrtombs(char *d, const wchar_t **s, size_t n, mbstate_t *st) { size_t bos = __builtin_object_size(d, 0); size_t r; - r = wcsrtombs(d, s, n > bos ? bos : n, st); + r = __wcsrtombs_orig(d, s, n > bos ? bos : n, st); if (bos < n && d && *s && r != (size_t)-1) __builtin_trap(); return r; } -static inline __attribute__ ((always_inline)) -size_t -__fortify_wcstombs(char *s, const wchar_t *ws, size_t n) +extern size_t __wcstombs_orig(char *, const wchar_t *, size_t) + __asm__(__USER_LABEL_PREFIX__ "wcstombs"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +size_t wcstombs(char *s, const wchar_t *ws, size_t n) { size_t bos = __builtin_object_size(s, 0); if (s && n > bos) __builtin_trap(); - return wcstombs(s, ws, n); + return __wcstombs_orig(s, ws, n); } -static inline __attribute__ ((always_inline)) -int -__fortify_wctomb(char *s, wchar_t wc) +extern int __wctomb_orig(char *, wchar_t) + __asm__(__USER_LABEL_PREFIX__ "wctomb"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +int wctomb(char *s, wchar_t wc) { size_t bos = __builtin_object_size(s, 0); if (s && MB_CUR_MAX > bos) __builtin_trap(); - return wctomb(s, wc); + return __wctomb_orig(s, wc); } -static inline __attribute__ ((always_inline)) -wchar_t * -__fortify_wmemcpy(wchar_t *d, const wchar_t *s, size_t n) +extern wchar_t *__wmemcpy_orig(wchar_t *, const wchar_t *, size_t) + __asm__(__USER_LABEL_PREFIX__ "wmemcpy"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +wchar_t *wmemcpy(wchar_t *d, const wchar_t *s, size_t n) { size_t bos = __builtin_object_size(d, 0); if (n > bos / sizeof(wchar_t)) __builtin_trap(); - return wmemcpy(d, s, n); + return __wmemcpy_orig(d, s, n); } -static inline __attribute__ ((always_inline)) -wchar_t * -__fortify_wmemmove(wchar_t *d, const wchar_t *s, size_t n) +extern wchar_t *__wmemmove_orig(wchar_t *, const wchar_t *, size_t) + __asm__(__USER_LABEL_PREFIX__ "wmemmove"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +wchar_t *wmemmove(wchar_t *d, const wchar_t *s, size_t n) { size_t bos = __builtin_object_size(d, 0); if (n > bos / sizeof(wchar_t)) __builtin_trap(); - return wmemmove(d, s, n); + return __wmemmove_orig(d, s, n); } -static inline __attribute__ ((always_inline)) -wchar_t * -__fortify_wmemset(wchar_t *s, wchar_t c, size_t n) +extern wchar_t *__wmemset_orig(wchar_t *, wchar_t, size_t) + __asm__(__USER_LABEL_PREFIX__ "wmemset"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +wchar_t *wmemset(wchar_t *s, wchar_t c, size_t n) { size_t bos = __builtin_object_size(s, 0); if (n > bos / sizeof(wchar_t)) __builtin_trap(); - return wmemset(s, c, n); + return __wmemset_orig(s, c, n); } -#undef fgetws -#define fgetws(s, n, fp) __fortify_fgetws(s, n, fp) -#undef mbsnrtowcs -#define mbsnrtowcs(d, s, n, wn, st) __fortify_mbsnrtowcs(d, s, n, wn, st) -#undef mbsrtowcs -#define mbsrtowcs(d, s, wn, st) __fortify_mbsrtowcs(d, s, wn, st) -#undef mbstowcs -#define mbstowcs(ws, s, wn) __fortify_mbstowcs(ws, s, wn) -#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 wcsncat -#define wcsncat(d, s, n) __fortify_wcsncat(d, s, n) -#undef wcsncpy -#define wcsncpy(d, s, n) __fortify_wcsncpy(d, s, n) -#undef wcsnrtombs -#define wcsnrtombs(d, s, wn, n, st) __fortify_wcsnrtombs(d, s, wn, n, st) -#undef wcsrtombs -#define wcsrtombs(d, s, n, st) __fortify_wcsrtombs(d, s, n, st) -#undef wcstombs -#define wcstombs(s, ws, n) __fortify_wcstombs(s, ws, n) -#undef wctomb -#define wctomb(s, wc) __fortify_wctomb(s, wc) -#undef wmemcpy -#define wmemcpy(d, s, n) __fortify_wmemcpy(d, s, n) -#undef wmemmove -#define wmemmove(d, s, n) __fortify_wmemmove(d, s, n) -#undef wmemset -#define wmemset(s, c, n) __fortify_wmemset(s, c, n) - #endif #endif -- cgit v1.3