summaryrefslogtreecommitdiff
path: root/include/wchar.h
blob: e2d56e8abeb68416142f6dd8ed037a4d1d245811 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#ifndef _FORTIFY_WCHAR_H
#define _FORTIFY_WCHAR_H

#include_next <stdlib.h>
#include_next <wchar.h>

#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0

#ifdef __cplusplus
extern "C" {
#endif

#undef fgetws
#undef mbsrtowcs
#undef mbstowcs
#undef wcrtomb
#undef wcscat
#undef wcscpy
#undef wcsncat
#undef wcsncpy
#undef wcsrtombs
#undef wcstombs
#undef wctomb
#undef wmemcpy
#undef wmemmove
#undef wmemset

__typeof__(fgetws) __fgetws_orig __asm__(__USER_LABEL_PREFIX__ "fgetws");
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
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_orig(s, n, fp);
}

#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
 || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)  || defined(_BSD_SOURCE)
#undef mbsnrtowcs
__typeof__(mbsnrtowcs) __mbsnrtowcs_orig __asm__(__USER_LABEL_PREFIX__ "mbsnrtowcs");
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
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_orig(d, s, n, wn > bos ? bos : wn, st);
		if (bos < wn && d && *s && r != (size_t)-1)
			__builtin_trap();
	} else {
		r = __mbsnrtowcs_orig(d, s, n > bos ? bos : n, wn, st);
		if (bos < n && d && *s && r != (size_t)-1)
			__builtin_trap();
	}
	return r;
}
#endif

__typeof__(mbsrtowcs) __mbsrtowcs_orig __asm__(__USER_LABEL_PREFIX__ "mbsrtowcs");
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
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_orig(d, s, wn > bos ? bos : wn, st);
	if (bos < wn && d && *s && r != (size_t)-1)
		__builtin_trap();
	return r;
}

__typeof__(mbstowcs) __mbstowcs_orig __asm__(__USER_LABEL_PREFIX__ "mbstowcs");
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
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_orig(ws, s, wn);
}

__typeof__(wcrtomb) __wcrtomb_orig __asm__(__USER_LABEL_PREFIX__ "wcrtomb");
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
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_orig(s, wc, st);
}

__typeof__(wcscat) __wcscat_orig __asm__(__USER_LABEL_PREFIX__ "wcscat");
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
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_orig(d, s);
}

__typeof__(wcscpy) __wcscpy_orig __asm__(__USER_LABEL_PREFIX__ "wcscpy");
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
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_orig(d, s);
}

__typeof__(wcsncat) __wcsncat_orig __asm__(__USER_LABEL_PREFIX__ "wcsncat");
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
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;

	if (n > bos / sizeof(wchar_t)) {
		slen = wcslen(s);
		dlen = wcslen(d);
		if (slen > n)
			slen = n;
		if (slen + dlen + 1 > bos / sizeof(wchar_t))
			__builtin_trap();
	}
	return __wcsncat_orig(d, s, n);
}

__typeof__(wcsncpy) __wcsncpy_orig __asm__(__USER_LABEL_PREFIX__ "wcsncpy");
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
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_orig(d, s, n);
}

#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
 || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)  || defined(_BSD_SOURCE)
#undef wcsnrtombs
__typeof__(wcsnrtombs) __wcsnrtombs_orig __asm__(__USER_LABEL_PREFIX__ "wcsnrtombs");
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
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_orig(d, s, wn > bos ? bos : wn, n, st);
		if (bos < wn && d && *s && r != (size_t)-1)
			__builtin_trap();
	} else {
		r = __wcsnrtombs_orig(d, s, wn, n > bos ? bos : n, st);
		if (bos < n && d && *s && r != (size_t)-1)
			__builtin_trap();
	}
	return r;
}
#endif

__typeof__(wcsrtombs) __wcsrtombs_orig __asm__(__USER_LABEL_PREFIX__ "wcsrtombs");
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
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_orig(d, s, n > bos ? bos : n, st);
	if (bos < n && d && *s && r != (size_t)-1)
		__builtin_trap();
	return r;
}

__typeof__(wcstombs) __wcstombs_orig __asm__(__USER_LABEL_PREFIX__ "wcstombs");
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
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_orig(s, ws, n);
}

__typeof__(wctomb) __wctomb_orig __asm__(__USER_LABEL_PREFIX__ "wctomb");
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
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_orig(s, wc);
}

__typeof__(wmemcpy) __wmemcpy_orig __asm__(__USER_LABEL_PREFIX__ "wmemcpy");
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
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_orig(d, s, n);
}

__typeof__(wmemmove) __wmemmove_orig __asm__(__USER_LABEL_PREFIX__ "wmemmove");
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
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_orig(d, s, n);
}

__typeof__(wmemset) __wmemset_orig __asm__(__USER_LABEL_PREFIX__ "wmemset");
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
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_orig(s, c, n);
}

#ifdef __cplusplus
}
#endif

#endif

#endif