summaryrefslogtreecommitdiff
path: root/include/wchar.h
diff options
context:
space:
mode:
authorsin2015-03-13 11:00:46 +0000
committersin2015-03-13 11:00:46 +0000
commit9f8c543dc81f0c4239acae6713f5414eb7dc681d (patch)
tree0c8dad17e27c510cc3c98502841aa1a75dfa3d1e /include/wchar.h
parentb211796d68c4a6b56f999534627791f3576b6135 (diff)
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.
Diffstat (limited to 'include/wchar.h')
-rw-r--r--include/wchar.h201
1 files changed, 98 insertions, 103 deletions
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 @@
7#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 7#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
8 8
9#ifndef __cplusplus 9#ifndef __cplusplus
10#undef fgetws
11#undef mbsnrtowcs
12#undef mbsrtowcs
13#undef mbstowcs
14#undef wcrtomb
15#undef wcscat
16#undef wcscpy
17#undef wcsncat
18#undef wcsncpy
19#undef wcsnrtombs
20#undef wcsrtombs
21#undef wcstombs
22#undef wctomb
23#undef wmemcpy
24#undef wmemmove
25#undef wmemset
10 26
11static inline __attribute__ ((always_inline)) 27extern wchar_t *__fgetws_orig(wchar_t *, int, FILE *)
12wchar_t * 28 __asm__(__USER_LABEL_PREFIX__ "fgetws");
13__fortify_fgetws(wchar_t *s, int n, FILE *fp) 29extern __inline __attribute__((__always_inline__,__gnu_inline__))
30wchar_t *fgetws(wchar_t *s, int n, FILE *fp)
14{ 31{
15 size_t bos = __builtin_object_size(s, 0); 32 size_t bos = __builtin_object_size(s, 0);
16 33
17 if ((size_t)n > bos / sizeof(wchar_t)) 34 if ((size_t)n > bos / sizeof(wchar_t))
18 __builtin_trap(); 35 __builtin_trap();
19 return fgetws(s, n, fp); 36 return __fgetws_orig(s, n, fp);
20} 37}
21 38
22static inline __attribute__ ((always_inline)) 39extern size_t __mbsnrtowcs(wchar_t *, const char **, size_t, size_t, mbstate_t *)
23size_t 40 __asm__(__USER_LABEL_PREFIX__ "mbsnrtowcs");
24__fortify_mbsnrtowcs(wchar_t *d, const char **s, size_t n, 41extern __inline __attribute__((__always_inline__,__gnu_inline__))
25 size_t wn, mbstate_t *st) 42size_t mbsnrtowcs(wchar_t *d, const char **s, size_t n, size_t wn, mbstate_t *st)
26{ 43{
27 size_t bos = __builtin_object_size(d, 0); 44 size_t bos = __builtin_object_size(d, 0);
28 size_t r; 45 size_t r;
29 46
30 if (wn > n / sizeof(wchar_t)) { 47 if (wn > n / sizeof(wchar_t)) {
31 bos /= sizeof(wchar_t); 48 bos /= sizeof(wchar_t);
32 r = mbsnrtowcs(d, s, n, wn > bos ? bos : wn, st); 49 r = __mbsnrtowcs_orig(d, s, n, wn > bos ? bos : wn, st);
33 if (bos < wn && d && *s && r != (size_t)-1) 50 if (bos < wn && d && *s && r != (size_t)-1)
34 __builtin_trap(); 51 __builtin_trap();
35 } else { 52 } else {
36 r = mbsnrtowcs(d, s, n > bos ? bos : n, wn, st); 53 r = __mbsnrtowcs_orig(d, s, n > bos ? bos : n, wn, st);
37 if (bos < n && d && *s && r != (size_t)-1) 54 if (bos < n && d && *s && r != (size_t)-1)
38 __builtin_trap(); 55 __builtin_trap();
39 } 56 }
40 return r; 57 return r;
41} 58}
42 59
43static inline __attribute__ ((always_inline)) 60extern size_t __mbsrtowcs_orig(wchar_t *, const char **, size_t, mbstate_t *)
44size_t 61 __asm__(__USER_LABEL_PREFIX__ "mbsrtowcs");
45__fortify_mbsrtowcs(wchar_t *d, const char **s, 62extern __inline __attribute__((__always_inline__,__gnu_inline__))
46 size_t wn, mbstate_t *st) 63size_t mbsrtowcs(wchar_t *d, const char **s, size_t wn, mbstate_t *st)
47{ 64{
48 size_t bos = __builtin_object_size(d, 0); 65 size_t bos = __builtin_object_size(d, 0);
49 size_t r; 66 size_t r;
50 67
51 bos /= sizeof(wchar_t); 68 bos /= sizeof(wchar_t);
52 r = mbsrtowcs(d, s, wn > bos ? bos : wn, st); 69 r = __mbsrtowcs_orig(d, s, wn > bos ? bos : wn, st);
53 if (bos < wn && d && *s && r != (size_t)-1) 70 if (bos < wn && d && *s && r != (size_t)-1)
54 __builtin_trap(); 71 __builtin_trap();
55 return r; 72 return r;
56} 73}
57 74
58static inline __attribute__ ((always_inline)) 75extern size_t __mbstowcs_orig(wchar_t *, const char *, size_t)
59size_t 76 __asm__(__USER_LABEL_PREFIX__ "mbstowcs");
60__fortify_mbstowcs(wchar_t *ws, const char *s, size_t wn) 77extern __inline __attribute__((__always_inline__,__gnu_inline__))
78size_t mbstowcs(wchar_t *ws, const char *s, size_t wn)
61{ 79{
62 size_t bos = __builtin_object_size(ws, 0); 80 size_t bos = __builtin_object_size(ws, 0);
63 81
64 if (ws && wn > bos / sizeof(wchar_t)) 82 if (ws && wn > bos / sizeof(wchar_t))
65 __builtin_trap(); 83 __builtin_trap();
66 return mbstowcs(ws, s, wn); 84 return __mbstowcs_orig(ws, s, wn);
67} 85}
68 86
69static inline __attribute__ ((always_inline)) 87extern size_t __wcrtomb_orig(char *, wchar_t, mbstate_t *)
70size_t 88 __asm__(__USER_LABEL_PREFIX__ "wcrtomb");
71__fortify_wcrtomb(char *s, wchar_t wc, mbstate_t *st) 89extern __inline __attribute__((__always_inline__,__gnu_inline__))
90size_t wcrtomb(char *s, wchar_t wc, mbstate_t *st)
72{ 91{
73 size_t bos = __builtin_object_size(s, 0); 92 size_t bos = __builtin_object_size(s, 0);
74 93
75 if (s && MB_CUR_MAX > bos) 94 if (s && MB_CUR_MAX > bos)
76 __builtin_trap(); 95 __builtin_trap();
77 return wcrtomb(s, wc, st); 96 return __wcrtomb_orig(s, wc, st);
78} 97}
79 98
80static inline __attribute__ ((always_inline)) 99extern wchar_t *__wcscat_orig(wchar_t *, const wchar_t *)
81wchar_t * 100 __asm__(__USER_LABEL_PREFIX__ "wcscat");
82__fortify_wcscat(wchar_t *d, const wchar_t *s) 101extern __inline __attribute__((__always_inline__,__gnu_inline__))
102wchar_t *wcscat(wchar_t *d, const wchar_t *s)
83{ 103{
84 size_t bos = __builtin_object_size(d, 0); 104 size_t bos = __builtin_object_size(d, 0);
85 105
86 if (wcslen(s) + wcslen(d) + 1 > bos / sizeof(wchar_t)) 106 if (wcslen(s) + wcslen(d) + 1 > bos / sizeof(wchar_t))
87 __builtin_trap(); 107 __builtin_trap();
88 return wcscat(d, s); 108 return __wcscat_orig(d, s);
89} 109}
90 110
91static inline __attribute__ ((always_inline)) 111extern wchar_t *__wcscpy_orig(wchar_t *, const wchar_t *)
92wchar_t * 112 __asm__(__USER_LABEL_PREFIX__ "wcscpy");
93__fortify_wcscpy(wchar_t *d, const wchar_t *s) 113extern __inline __attribute__((__always_inline__,__gnu_inline__))
114wchar_t *wcscpy(wchar_t *d, const wchar_t *s)
94{ 115{
95 size_t bos = __builtin_object_size(d, 0); 116 size_t bos = __builtin_object_size(d, 0);
96 117
97 if (wcslen(s) + 1 > bos / sizeof(wchar_t)) 118 if (wcslen(s) + 1 > bos / sizeof(wchar_t))
98 __builtin_trap(); 119 __builtin_trap();
99 return wcscpy(d, s); 120 return __wcscpy_orig(d, s);
100} 121}
101 122
102static inline __attribute__ ((always_inline)) 123extern wchar_t *__wcsncat_orig(wchar_t *, const wchar_t *, size_t)
103wchar_t * 124 __asm__(__USER_LABEL_PREFIX__ "wcsncat");
104__fortify_wcsncat(wchar_t *d, const wchar_t *s, size_t n) 125extern __inline __attribute__((__always_inline__,__gnu_inline__))
126wchar_t *wcsncat(wchar_t *d, const wchar_t *s, size_t n)
105{ 127{
106 size_t bos = __builtin_object_size(d, 0); 128 size_t bos = __builtin_object_size(d, 0);
107 size_t slen, dlen; 129 size_t slen, dlen;
@@ -114,143 +136,116 @@ __fortify_wcsncat(wchar_t *d, const wchar_t *s, size_t n)
114 if (slen + dlen + 1 > bos / sizeof(wchar_t)) 136 if (slen + dlen + 1 > bos / sizeof(wchar_t))
115 __builtin_trap(); 137 __builtin_trap();
116 } 138 }
117 return wcsncat(d, s, n); 139 return __wcsncat_orig(d, s, n);
118} 140}
119 141
120static inline __attribute__ ((always_inline)) 142extern wchar_t *__wcsncpy_orig(wchar_t *, const wchar_t *, size_t)
121wchar_t * 143 __asm__(__USER_LABEL_PREFIX__ "wcsncpy");
122__fortify_wcsncpy(wchar_t *d, const wchar_t *s, size_t n) 144extern __inline __attribute__((__always_inline__,__gnu_inline__))
145wchar_t *wcsncpy(wchar_t *d, const wchar_t *s, size_t n)
123{ 146{
124 size_t bos = __builtin_object_size(d, 0); 147 size_t bos = __builtin_object_size(d, 0);
125 148
126 if (n > bos / sizeof(wchar_t)) 149 if (n > bos / sizeof(wchar_t))
127 __builtin_trap(); 150 __builtin_trap();
128 return wcsncpy(d, s, n); 151 return __wcsncpy_orig(d, s, n);
129} 152}
130 153
131static inline __attribute__ ((always_inline)) 154extern size_t __wcsnrtombs_orig(char *, const wchar_t **, size_t, size_t, mbstate_t *)
132size_t 155 __asm__(__USER_LABEL_PREFIX__ "wcsnrtombs");
133__fortify_wcsnrtombs(char *d, const wchar_t **s, size_t wn, 156extern __inline __attribute__((__always_inline__,__gnu_inline__))
134 size_t n, mbstate_t *st) 157size_t wcsnrtombs(char *d, const wchar_t **s, size_t wn, size_t n, mbstate_t *st)
135{ 158{
136 size_t bos = __builtin_object_size(d, 0); 159 size_t bos = __builtin_object_size(d, 0);
137 size_t r; 160 size_t r;
138 161
139 if (wn > n / sizeof(wchar_t)) { 162 if (wn > n / sizeof(wchar_t)) {
140 bos /= sizeof(wchar_t); 163 bos /= sizeof(wchar_t);
141 r = wcsnrtombs(d, s, wn > bos ? bos : wn, n, st); 164 r = __wcsnrtombs_orig(d, s, wn > bos ? bos : wn, n, st);
142 if (bos < wn && d && *s && r != (size_t)-1) 165 if (bos < wn && d && *s && r != (size_t)-1)
143 __builtin_trap(); 166 __builtin_trap();
144 } else { 167 } else {
145 r = wcsnrtombs(d, s, wn, n > bos ? bos : n, st); 168 r = __wcsnrtombs_orig(d, s, wn, n > bos ? bos : n, st);
146 if (bos < n && d && *s && r != (size_t)-1) 169 if (bos < n && d && *s && r != (size_t)-1)
147 __builtin_trap(); 170 __builtin_trap();
148 } 171 }
149 return r; 172 return r;
150} 173}
151 174
152static inline __attribute__ ((always_inline)) 175extern size_t __wcsrtombs_orig(char *, const wchar_t **, size_t, mbstate_t *)
153size_t 176 __asm__(__USER_LABEL_PREFIX__ "wcsrtombs");
154__fortify_wcsrtombs(char *d, const wchar_t **s, size_t n, 177extern __inline __attribute__((__always_inline__,__gnu_inline__))
155 mbstate_t *st) 178size_t wcsrtombs(char *d, const wchar_t **s, size_t n, mbstate_t *st)
156{ 179{
157 size_t bos = __builtin_object_size(d, 0); 180 size_t bos = __builtin_object_size(d, 0);
158 size_t r; 181 size_t r;
159 182
160 r = wcsrtombs(d, s, n > bos ? bos : n, st); 183 r = __wcsrtombs_orig(d, s, n > bos ? bos : n, st);
161 if (bos < n && d && *s && r != (size_t)-1) 184 if (bos < n && d && *s && r != (size_t)-1)
162 __builtin_trap(); 185 __builtin_trap();
163 return r; 186 return r;
164} 187}
165 188
166static inline __attribute__ ((always_inline)) 189extern size_t __wcstombs_orig(char *, const wchar_t *, size_t)
167size_t 190 __asm__(__USER_LABEL_PREFIX__ "wcstombs");
168__fortify_wcstombs(char *s, const wchar_t *ws, size_t n) 191extern __inline __attribute__((__always_inline__,__gnu_inline__))
192size_t wcstombs(char *s, const wchar_t *ws, size_t n)
169{ 193{
170 size_t bos = __builtin_object_size(s, 0); 194 size_t bos = __builtin_object_size(s, 0);
171 195
172 if (s && n > bos) 196 if (s && n > bos)
173 __builtin_trap(); 197 __builtin_trap();
174 return wcstombs(s, ws, n); 198 return __wcstombs_orig(s, ws, n);
175} 199}
176 200
177static inline __attribute__ ((always_inline)) 201extern int __wctomb_orig(char *, wchar_t)
178int 202 __asm__(__USER_LABEL_PREFIX__ "wctomb");
179__fortify_wctomb(char *s, wchar_t wc) 203extern __inline __attribute__((__always_inline__,__gnu_inline__))
204int wctomb(char *s, wchar_t wc)
180{ 205{
181 size_t bos = __builtin_object_size(s, 0); 206 size_t bos = __builtin_object_size(s, 0);
182 207
183 if (s && MB_CUR_MAX > bos) 208 if (s && MB_CUR_MAX > bos)
184 __builtin_trap(); 209 __builtin_trap();
185 return wctomb(s, wc); 210 return __wctomb_orig(s, wc);
186} 211}
187 212
188static inline __attribute__ ((always_inline)) 213extern wchar_t *__wmemcpy_orig(wchar_t *, const wchar_t *, size_t)
189wchar_t * 214 __asm__(__USER_LABEL_PREFIX__ "wmemcpy");
190__fortify_wmemcpy(wchar_t *d, const wchar_t *s, size_t n) 215extern __inline __attribute__((__always_inline__,__gnu_inline__))
216wchar_t *wmemcpy(wchar_t *d, const wchar_t *s, size_t n)
191{ 217{
192 size_t bos = __builtin_object_size(d, 0); 218 size_t bos = __builtin_object_size(d, 0);
193 219
194 if (n > bos / sizeof(wchar_t)) 220 if (n > bos / sizeof(wchar_t))
195 __builtin_trap(); 221 __builtin_trap();
196 return wmemcpy(d, s, n); 222 return __wmemcpy_orig(d, s, n);
197} 223}
198 224
199static inline __attribute__ ((always_inline)) 225extern wchar_t *__wmemmove_orig(wchar_t *, const wchar_t *, size_t)
200wchar_t * 226 __asm__(__USER_LABEL_PREFIX__ "wmemmove");
201__fortify_wmemmove(wchar_t *d, const wchar_t *s, size_t n) 227extern __inline __attribute__((__always_inline__,__gnu_inline__))
228wchar_t *wmemmove(wchar_t *d, const wchar_t *s, size_t n)
202{ 229{
203 size_t bos = __builtin_object_size(d, 0); 230 size_t bos = __builtin_object_size(d, 0);
204 231
205 if (n > bos / sizeof(wchar_t)) 232 if (n > bos / sizeof(wchar_t))
206 __builtin_trap(); 233 __builtin_trap();
207 return wmemmove(d, s, n); 234 return __wmemmove_orig(d, s, n);
208} 235}
209 236
210static inline __attribute__ ((always_inline)) 237extern wchar_t *__wmemset_orig(wchar_t *, wchar_t, size_t)
211wchar_t * 238 __asm__(__USER_LABEL_PREFIX__ "wmemset");
212__fortify_wmemset(wchar_t *s, wchar_t c, size_t n) 239extern __inline __attribute__((__always_inline__,__gnu_inline__))
240wchar_t *wmemset(wchar_t *s, wchar_t c, size_t n)
213{ 241{
214 size_t bos = __builtin_object_size(s, 0); 242 size_t bos = __builtin_object_size(s, 0);
215 243
216 if (n > bos / sizeof(wchar_t)) 244 if (n > bos / sizeof(wchar_t))
217 __builtin_trap(); 245 __builtin_trap();
218 return wmemset(s, c, n); 246 return __wmemset_orig(s, c, n);
219} 247}
220 248
221#undef fgetws
222#define fgetws(s, n, fp) __fortify_fgetws(s, n, fp)
223#undef mbsnrtowcs
224#define mbsnrtowcs(d, s, n, wn, st) __fortify_mbsnrtowcs(d, s, n, wn, st)
225#undef mbsrtowcs
226#define mbsrtowcs(d, s, wn, st) __fortify_mbsrtowcs(d, s, wn, st)
227#undef mbstowcs
228#define mbstowcs(ws, s, wn) __fortify_mbstowcs(ws, s, wn)
229#undef wcrtomb
230#define wcrtomb(s, wc, st) __fortify_wcrtomb(s, wc, st)
231#undef wcscat
232#define wcscat(d, s) __fortify_wcscat(d, s)
233#undef wcscpy
234#define wcscpy(d, s) __fortify_wcscpy(d, s)
235#undef wcsncat
236#define wcsncat(d, s, n) __fortify_wcsncat(d, s, n)
237#undef wcsncpy
238#define wcsncpy(d, s, n) __fortify_wcsncpy(d, s, n)
239#undef wcsnrtombs
240#define wcsnrtombs(d, s, wn, n, st) __fortify_wcsnrtombs(d, s, wn, n, st)
241#undef wcsrtombs
242#define wcsrtombs(d, s, n, st) __fortify_wcsrtombs(d, s, n, st)
243#undef wcstombs
244#define wcstombs(s, ws, n) __fortify_wcstombs(s, ws, n)
245#undef wctomb
246#define wctomb(s, wc) __fortify_wctomb(s, wc)
247#undef wmemcpy
248#define wmemcpy(d, s, n) __fortify_wmemcpy(d, s, n)
249#undef wmemmove
250#define wmemmove(d, s, n) __fortify_wmemmove(d, s, n)
251#undef wmemset
252#define wmemset(s, c, n) __fortify_wmemset(s, c, n)
253
254#endif 249#endif
255 250
256#endif 251#endif