fortify-headers

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

wchar.h (6760B)


      1 /*
      2  * Copyright (C) 2015-2017 Dimitris Papastamos <sin@2f30.org>
      3  *
      4  * Permission to use, copy, modify, and/or distribute this software for any
      5  * purpose with or without fee is hereby granted.
      6  *
      7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
      9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     10  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     12  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     13  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     14  */
     15 
     16 #ifndef _FORTIFY_WCHAR_H
     17 #define _FORTIFY_WCHAR_H
     18 
     19 #ifndef __cplusplus
     20 __extension__
     21 #endif
     22 #include_next <limits.h>
     23 #ifndef __cplusplus
     24 __extension__
     25 #endif
     26 #include_next <stdlib.h>
     27 #ifndef __cplusplus
     28 __extension__
     29 #endif
     30 #include_next <string.h>
     31 #ifndef __cplusplus
     32 __extension__
     33 #endif
     34 #include_next <wchar.h>
     35 
     36 #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
     37 #include "fortify-headers.h"
     38 
     39 #ifdef __cplusplus
     40 extern "C" {
     41 #endif
     42 
     43 #undef fgetws
     44 #undef mbsrtowcs
     45 #undef mbstowcs
     46 #undef wcrtomb
     47 #undef wcscat
     48 #undef wcscpy
     49 #undef wcsncat
     50 #undef wcsncpy
     51 #undef wcsrtombs
     52 #undef wcstombs
     53 #undef wctomb
     54 #undef wmemcpy
     55 #undef wmemmove
     56 #undef wmemset
     57 
     58 _FORTIFY_FN(fgetws) wchar_t *fgetws(wchar_t *__s, int __n, FILE *__f)
     59 {
     60 	size_t __b = __builtin_object_size(__s, 0);
     61 
     62 	if ((size_t)__n > __b / sizeof(wchar_t))
     63 		__builtin_trap();
     64 	return __orig_fgetws(__s, __n, __f);
     65 }
     66 
     67 #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
     68  || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)  || defined(_BSD_SOURCE)
     69 #undef mbsnrtowcs
     70 _FORTIFY_FN(mbsnrtowcs) size_t mbsnrtowcs(wchar_t *__d, const char **__s, size_t __n,
     71                                           size_t __wn, mbstate_t *__st)
     72 {
     73 	size_t __b = __builtin_object_size(__d, 0);
     74 	size_t __r;
     75 
     76 	if (__wn > __n / sizeof(wchar_t)) {
     77 		__b /= sizeof(wchar_t);
     78 		__r = __orig_mbsnrtowcs(__d, __s, __n, __wn > __b ? __b : __wn, __st);
     79 		if (__b < __wn && __d && *__s && __r != (size_t)-1)
     80 			__builtin_trap();
     81 	} else {
     82 		__r = __orig_mbsnrtowcs(__d, __s, __n > __b ? __b : __n, __wn, __st);
     83 		if (__b < __n && __d && *__s && __r != (size_t)-1)
     84 			__builtin_trap();
     85 	}
     86 	return __r;
     87 }
     88 #endif
     89 
     90 _FORTIFY_FN(mbsrtowcs) size_t mbsrtowcs(wchar_t *__d, const char **__s, size_t __wn,
     91                                         mbstate_t *__st)
     92 {
     93 	size_t __b = __builtin_object_size(__d, 0);
     94 	size_t __r;
     95 
     96 	__b /= sizeof(wchar_t);
     97 	__r = __orig_mbsrtowcs(__d, __s, __wn > __b ? __b : __wn, __st);
     98 	if (__b < __wn && __d && *__s && __r != (size_t)-1)
     99 		__builtin_trap();
    100 	return __r;
    101 }
    102 
    103 _FORTIFY_FN(mbstowcs) size_t mbstowcs(wchar_t *__ws, const char *__s, size_t __wn)
    104 {
    105 	size_t __b = __builtin_object_size(__ws, 0);
    106 
    107 	if (__ws && __wn > __b / sizeof(wchar_t))
    108 		__builtin_trap();
    109 	return __orig_mbstowcs(__ws, __s, __wn);
    110 }
    111 
    112 _FORTIFY_FN(wcrtomb) size_t wcrtomb(char *__s, wchar_t __w, mbstate_t *__st)
    113 {
    114 	if (__s && MB_LEN_MAX > __builtin_object_size(__s, 2)) {
    115 		char __buf[MB_LEN_MAX];
    116 		size_t __r;
    117 
    118 		__r = __orig_wcrtomb(__buf, __w, __st);
    119 		if (__r == (size_t)-1)
    120 			return __r;
    121 		if (__r > __builtin_object_size(__s, 0))
    122 			__builtin_trap();
    123 		__builtin_memcpy(__s, __buf, __r);
    124 		return __r;
    125 	}
    126 	return __orig_wcrtomb(__s, __w, __st);
    127 }
    128 
    129 _FORTIFY_FN(wcscat) wchar_t *wcscat(wchar_t *__d, const wchar_t *__s)
    130 {
    131 	size_t __b = __builtin_object_size(__d, 0);
    132 
    133 	if (wcslen(__s) + wcslen(__d) + 1 > __b / sizeof(wchar_t))
    134 		__builtin_trap();
    135 	return __orig_wcscat(__d, __s);
    136 }
    137 
    138 _FORTIFY_FN(wcscpy) wchar_t *wcscpy(wchar_t *__d, const wchar_t *__s)
    139 {
    140 	size_t __b = __builtin_object_size(__d, 0);
    141 
    142 	if (wcslen(__s) + 1 > __b / sizeof(wchar_t))
    143 		__builtin_trap();
    144 	return __orig_wcscpy(__d, __s);
    145 }
    146 
    147 _FORTIFY_FN(wcsncat) wchar_t *wcsncat(wchar_t *__d, const wchar_t *__s, size_t __n)
    148 {
    149 	size_t __b = __builtin_object_size(__d, 0);
    150 	size_t __sl, __dl;
    151 
    152 	if (__n > __b / sizeof(wchar_t)) {
    153 		__sl = wcslen(__s);
    154 		__dl = wcslen(__d);
    155 		if (__sl > __n)
    156 			__sl = __n;
    157 		if (__sl + __dl + 1 > __b / sizeof(wchar_t))
    158 			__builtin_trap();
    159 	}
    160 	return __orig_wcsncat(__d, __s, __n);
    161 }
    162 
    163 _FORTIFY_FN(wcsncpy) wchar_t *wcsncpy(wchar_t *__d, const wchar_t *__s, size_t __n)
    164 {
    165 	size_t __b = __builtin_object_size(__d, 0);
    166 
    167 	if (__n > __b / sizeof(wchar_t))
    168 		__builtin_trap();
    169 	return __orig_wcsncpy(__d, __s, __n);
    170 }
    171 
    172 #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
    173  || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)  || defined(_BSD_SOURCE)
    174 #undef wcsnrtombs
    175 _FORTIFY_FN(wcsnrtombs) size_t wcsnrtombs(char *__d, const wchar_t **__s, size_t __wn,
    176                                           size_t __n, mbstate_t *__st)
    177 {
    178 	size_t __b = __builtin_object_size(__d, 0);
    179 	size_t __r;
    180 
    181 	if (__wn > __n / sizeof(wchar_t)) {
    182 		__b /= sizeof(wchar_t);
    183 		__r = __orig_wcsnrtombs(__d, __s, __wn > __b ? __b : __wn, __n, __st);
    184 		if (__b < __wn && __d && *__s && __r != (size_t)-1)
    185 			__builtin_trap();
    186 	} else {
    187 		__r = __orig_wcsnrtombs(__d, __s, __wn, __n > __b ? __b : __n, __st);
    188 		if (__b < __n && __d && *__s && __r != (size_t)-1)
    189 			__builtin_trap();
    190 	}
    191 	return __r;
    192 }
    193 #endif
    194 
    195 _FORTIFY_FN(wcsrtombs) size_t wcsrtombs(char *__d, const wchar_t **__s, size_t __n,
    196                                         mbstate_t *__st)
    197 {
    198 	size_t __b = __builtin_object_size(__d, 0);
    199 	size_t __r;
    200 
    201 	__r = __orig_wcsrtombs(__d, __s, __n > __b ? __b : __n, __st);
    202 	if (__b < __n && __d && *__s && __r != (size_t)-1)
    203 		__builtin_trap();
    204 	return __r;
    205 }
    206 
    207 _FORTIFY_FN(wcstombs) size_t wcstombs(char *__s, const wchar_t *__ws, size_t __n)
    208 {
    209 	size_t __b = __builtin_object_size(__s, 0);
    210 
    211 	if (__s && __n > __b)
    212 		__builtin_trap();
    213 	return __orig_wcstombs(__s, __ws, __n);
    214 }
    215 
    216 _FORTIFY_FN(wctomb) int wctomb(char *__s, wchar_t __w)
    217 {
    218 	size_t __b = __builtin_object_size(__s, 0);
    219 
    220 	if (__s && MB_LEN_MAX > __b && MB_CUR_MAX > __b)
    221 		__builtin_trap();
    222 	return __orig_wctomb(__s, __w);
    223 }
    224 
    225 _FORTIFY_FN(wmemcpy) wchar_t *wmemcpy(wchar_t *__d, const wchar_t *__s, size_t __n)
    226 {
    227 	size_t __b = __builtin_object_size(__d, 0);
    228 
    229 	if (__n > __b / sizeof(wchar_t))
    230 		__builtin_trap();
    231 	return __orig_wmemcpy(__d, __s, __n);
    232 }
    233 
    234 _FORTIFY_FN(wmemmove) wchar_t *wmemmove(wchar_t *__d, const wchar_t *__s, size_t __n)
    235 {
    236 	size_t __b = __builtin_object_size(__d, 0);
    237 
    238 	if (__n > __b / sizeof(wchar_t))
    239 		__builtin_trap();
    240 	return __orig_wmemmove(__d, __s, __n);
    241 }
    242 
    243 _FORTIFY_FN(wmemset) wchar_t *wmemset(wchar_t *__s, wchar_t __c, size_t __n)
    244 {
    245 	size_t __b = __builtin_object_size(__s, 0);
    246 
    247 	if (__n > __b / sizeof(wchar_t))
    248 		__builtin_trap();
    249 	return __orig_wmemset(__s, __c, __n);
    250 }
    251 
    252 #ifdef __cplusplus
    253 }
    254 #endif
    255 
    256 #endif
    257 
    258 #endif