1 | /* Checking macros for wchar functions. |
2 | Copyright (C) 2005-2022 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #ifndef _WCHAR_H |
20 | # error "Never include <bits/wchar2.h> directly; use <wchar.h> instead." |
21 | #endif |
22 | |
23 | |
24 | extern wchar_t *__REDIRECT_NTH (__wmemcpy_alias, |
25 | (wchar_t *__restrict __s1, |
26 | const wchar_t *__restrict __s2, size_t __n), |
27 | wmemcpy); |
28 | extern wchar_t *__REDIRECT_NTH (__wmemcpy_chk_warn, |
29 | (wchar_t *__restrict __s1, |
30 | const wchar_t *__restrict __s2, size_t __n, |
31 | size_t __ns1), __wmemcpy_chk) |
32 | __warnattr ("wmemcpy called with length bigger than size of destination " |
33 | "buffer" ); |
34 | |
35 | __fortify_function wchar_t * |
36 | __NTH (wmemcpy (wchar_t *__restrict __s1, const wchar_t *__restrict __s2, |
37 | size_t __n)) |
38 | { |
39 | return __glibc_fortify_n (wmemcpy, __n, sizeof (wchar_t), |
40 | __glibc_objsize0 (__s1), |
41 | __s1, __s2, __n); |
42 | } |
43 | |
44 | |
45 | extern wchar_t *__REDIRECT_NTH (__wmemmove_alias, (wchar_t *__s1, |
46 | const wchar_t *__s2, |
47 | size_t __n), wmemmove); |
48 | extern wchar_t *__REDIRECT_NTH (__wmemmove_chk_warn, |
49 | (wchar_t *__s1, const wchar_t *__s2, |
50 | size_t __n, size_t __ns1), __wmemmove_chk) |
51 | __warnattr ("wmemmove called with length bigger than size of destination " |
52 | "buffer" ); |
53 | |
54 | __fortify_function wchar_t * |
55 | __NTH (wmemmove (wchar_t *__s1, const wchar_t *__s2, size_t __n)) |
56 | { |
57 | return __glibc_fortify_n (wmemmove, __n, sizeof (wchar_t), |
58 | __glibc_objsize0 (__s1), |
59 | __s1, __s2, __n); |
60 | } |
61 | |
62 | |
63 | #ifdef __USE_GNU |
64 | extern wchar_t *__REDIRECT_NTH (__wmempcpy_alias, |
65 | (wchar_t *__restrict __s1, |
66 | const wchar_t *__restrict __s2, |
67 | size_t __n), wmempcpy); |
68 | extern wchar_t *__REDIRECT_NTH (__wmempcpy_chk_warn, |
69 | (wchar_t *__restrict __s1, |
70 | const wchar_t *__restrict __s2, size_t __n, |
71 | size_t __ns1), __wmempcpy_chk) |
72 | __warnattr ("wmempcpy called with length bigger than size of destination " |
73 | "buffer" ); |
74 | |
75 | __fortify_function wchar_t * |
76 | __NTH (wmempcpy (wchar_t *__restrict __s1, const wchar_t *__restrict __s2, |
77 | size_t __n)) |
78 | { |
79 | return __glibc_fortify_n (wmempcpy, __n, sizeof (wchar_t), |
80 | __glibc_objsize0 (__s1), |
81 | __s1, __s2, __n); |
82 | } |
83 | #endif |
84 | |
85 | |
86 | extern wchar_t *__REDIRECT_NTH (__wmemset_alias, (wchar_t *__s, wchar_t __c, |
87 | size_t __n), wmemset); |
88 | extern wchar_t *__REDIRECT_NTH (__wmemset_chk_warn, |
89 | (wchar_t *__s, wchar_t __c, size_t __n, |
90 | size_t __ns), __wmemset_chk) |
91 | __warnattr ("wmemset called with length bigger than size of destination " |
92 | "buffer" ); |
93 | |
94 | __fortify_function wchar_t * |
95 | __NTH (wmemset (wchar_t *__s, wchar_t __c, size_t __n)) |
96 | { |
97 | return __glibc_fortify_n (wmemset, __n, sizeof (wchar_t), |
98 | __glibc_objsize0 (__s), |
99 | __s, __c, __n); |
100 | } |
101 | |
102 | |
103 | extern wchar_t *__REDIRECT_NTH (__wcscpy_alias, |
104 | (wchar_t *__restrict __dest, |
105 | const wchar_t *__restrict __src), wcscpy); |
106 | |
107 | __fortify_function wchar_t * |
108 | __NTH (wcscpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src)) |
109 | { |
110 | size_t sz = __glibc_objsize (__dest); |
111 | if (sz != (size_t) -1) |
112 | return __wcscpy_chk (__dest, __src, n: sz / sizeof (wchar_t)); |
113 | return __wcscpy_alias (__dest, __src); |
114 | } |
115 | |
116 | |
117 | extern wchar_t *__REDIRECT_NTH (__wcpcpy_alias, |
118 | (wchar_t *__restrict __dest, |
119 | const wchar_t *__restrict __src), wcpcpy); |
120 | |
121 | __fortify_function wchar_t * |
122 | __NTH (wcpcpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src)) |
123 | { |
124 | size_t sz = __glibc_objsize (__dest); |
125 | if (sz != (size_t) -1) |
126 | return __wcpcpy_chk (__dest, __src, destlen: sz / sizeof (wchar_t)); |
127 | return __wcpcpy_alias (__dest, __src); |
128 | } |
129 | |
130 | |
131 | extern wchar_t *__REDIRECT_NTH (__wcsncpy_alias, |
132 | (wchar_t *__restrict __dest, |
133 | const wchar_t *__restrict __src, |
134 | size_t __n), wcsncpy); |
135 | extern wchar_t *__REDIRECT_NTH (__wcsncpy_chk_warn, |
136 | (wchar_t *__restrict __dest, |
137 | const wchar_t *__restrict __src, |
138 | size_t __n, size_t __destlen), __wcsncpy_chk) |
139 | __warnattr ("wcsncpy called with length bigger than size of destination " |
140 | "buffer" ); |
141 | |
142 | __fortify_function wchar_t * |
143 | __NTH (wcsncpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src, |
144 | size_t __n)) |
145 | { |
146 | return __glibc_fortify_n (wcsncpy, __n, sizeof (wchar_t), |
147 | __glibc_objsize (__dest), |
148 | __dest, __src, __n); |
149 | } |
150 | |
151 | |
152 | extern wchar_t *__REDIRECT_NTH (__wcpncpy_alias, |
153 | (wchar_t *__restrict __dest, |
154 | const wchar_t *__restrict __src, |
155 | size_t __n), wcpncpy); |
156 | extern wchar_t *__REDIRECT_NTH (__wcpncpy_chk_warn, |
157 | (wchar_t *__restrict __dest, |
158 | const wchar_t *__restrict __src, |
159 | size_t __n, size_t __destlen), __wcpncpy_chk) |
160 | __warnattr ("wcpncpy called with length bigger than size of destination " |
161 | "buffer" ); |
162 | |
163 | __fortify_function wchar_t * |
164 | __NTH (wcpncpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src, |
165 | size_t __n)) |
166 | { |
167 | return __glibc_fortify_n (wcpncpy, __n, sizeof (wchar_t), |
168 | __glibc_objsize (__dest), |
169 | __dest, __src, __n); |
170 | } |
171 | |
172 | |
173 | extern wchar_t *__REDIRECT_NTH (__wcscat_alias, |
174 | (wchar_t *__restrict __dest, |
175 | const wchar_t *__restrict __src), wcscat); |
176 | |
177 | __fortify_function wchar_t * |
178 | __NTH (wcscat (wchar_t *__restrict __dest, const wchar_t *__restrict __src)) |
179 | { |
180 | size_t sz = __glibc_objsize (__dest); |
181 | if (sz != (size_t) -1) |
182 | return __wcscat_chk (__dest, __src, destlen: sz / sizeof (wchar_t)); |
183 | return __wcscat_alias (__dest, __src); |
184 | } |
185 | |
186 | |
187 | extern wchar_t *__REDIRECT_NTH (__wcsncat_alias, |
188 | (wchar_t *__restrict __dest, |
189 | const wchar_t *__restrict __src, |
190 | size_t __n), wcsncat); |
191 | |
192 | __fortify_function wchar_t * |
193 | __NTH (wcsncat (wchar_t *__restrict __dest, const wchar_t *__restrict __src, |
194 | size_t __n)) |
195 | { |
196 | size_t sz = __glibc_objsize (__dest); |
197 | if (sz != (size_t) -1) |
198 | return __wcsncat_chk (__dest, __src, __n, destlen: sz / sizeof (wchar_t)); |
199 | return __wcsncat_alias (__dest, __src, __n); |
200 | } |
201 | |
202 | |
203 | |
204 | extern int __REDIRECT_NTH_LDBL (__swprintf_alias, |
205 | (wchar_t *__restrict __s, size_t __n, |
206 | const wchar_t *__restrict __fmt, ...), |
207 | swprintf); |
208 | |
209 | #ifdef __va_arg_pack |
210 | __fortify_function int |
211 | __NTH (swprintf (wchar_t *__restrict __s, size_t __n, |
212 | const wchar_t *__restrict __fmt, ...)) |
213 | { |
214 | size_t sz = __glibc_objsize (__s); |
215 | if (sz != (size_t) -1 || __USE_FORTIFY_LEVEL > 1) |
216 | return __swprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1, |
217 | sz / sizeof (wchar_t), __fmt, __va_arg_pack ()); |
218 | return __swprintf_alias (__s, __n, __fmt, __va_arg_pack ()); |
219 | } |
220 | #elif !defined __cplusplus |
221 | /* XXX We might want to have support in gcc for swprintf. */ |
222 | # define swprintf(s, n, ...) \ |
223 | (__glibc_objsize (s) != (size_t) -1 || __USE_FORTIFY_LEVEL > 1 \ |
224 | ? __swprintf_chk (s, n, __USE_FORTIFY_LEVEL - 1, \ |
225 | __glibc_objsize (s) / sizeof (wchar_t), __VA_ARGS__) \ |
226 | : swprintf (s, n, __VA_ARGS__)) |
227 | #endif |
228 | |
229 | |
230 | extern int __REDIRECT_NTH_LDBL (__vswprintf_alias, |
231 | (wchar_t *__restrict __s, size_t __n, |
232 | const wchar_t *__restrict __fmt, |
233 | __gnuc_va_list __ap), vswprintf); |
234 | |
235 | __fortify_function int |
236 | __NTH (vswprintf (wchar_t *__restrict __s, size_t __n, |
237 | const wchar_t *__restrict __fmt, __gnuc_va_list __ap)) |
238 | { |
239 | size_t sz = __glibc_objsize (__s); |
240 | if (sz != (size_t) -1 || __USE_FORTIFY_LEVEL > 1) |
241 | return __vswprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1, |
242 | s_len: sz / sizeof (wchar_t), format: __fmt, arg: __ap); |
243 | return __vswprintf_alias (__s, __n, __fmt, __ap); |
244 | } |
245 | |
246 | |
247 | #if __USE_FORTIFY_LEVEL > 1 |
248 | |
249 | # ifdef __va_arg_pack |
250 | __fortify_function int |
251 | wprintf (const wchar_t *__restrict __fmt, ...) |
252 | { |
253 | return __wprintf_chk (__USE_FORTIFY_LEVEL - 1, __fmt, __va_arg_pack ()); |
254 | } |
255 | |
256 | __fortify_function int |
257 | fwprintf (__FILE *__restrict __stream, const wchar_t *__restrict __fmt, ...) |
258 | { |
259 | return __fwprintf_chk (__stream, __USE_FORTIFY_LEVEL - 1, __fmt, |
260 | __va_arg_pack ()); |
261 | } |
262 | # elif !defined __cplusplus |
263 | # define wprintf(...) \ |
264 | __wprintf_chk (__USE_FORTIFY_LEVEL - 1, __VA_ARGS__) |
265 | # define fwprintf(stream, ...) \ |
266 | __fwprintf_chk (stream, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__) |
267 | # endif |
268 | |
269 | __fortify_function int |
270 | vwprintf (const wchar_t *__restrict __fmt, __gnuc_va_list __ap) |
271 | { |
272 | return __vwprintf_chk (__USE_FORTIFY_LEVEL - 1, format: __fmt, __ap); |
273 | } |
274 | |
275 | __fortify_function int |
276 | vfwprintf (__FILE *__restrict __stream, |
277 | const wchar_t *__restrict __fmt, __gnuc_va_list __ap) |
278 | { |
279 | return __vfwprintf_chk (__stream, __USE_FORTIFY_LEVEL - 1, format: __fmt, __ap); |
280 | } |
281 | |
282 | #endif |
283 | |
284 | extern wchar_t *__REDIRECT (__fgetws_alias, |
285 | (wchar_t *__restrict __s, int __n, |
286 | __FILE *__restrict __stream), fgetws) __wur; |
287 | extern wchar_t *__REDIRECT (__fgetws_chk_warn, |
288 | (wchar_t *__restrict __s, size_t __size, int __n, |
289 | __FILE *__restrict __stream), __fgetws_chk) |
290 | __wur __warnattr ("fgetws called with bigger size than length " |
291 | "of destination buffer" ); |
292 | |
293 | __fortify_function __wur wchar_t * |
294 | fgetws (wchar_t *__restrict __s, int __n, __FILE *__restrict __stream) |
295 | { |
296 | size_t sz = __glibc_objsize (__s); |
297 | if (__glibc_safe_or_unknown_len (__n, sizeof (wchar_t), sz)) |
298 | return __fgetws_alias (__s, __n, __stream); |
299 | if (__glibc_unsafe_len (__n, sizeof (wchar_t), sz)) |
300 | return __fgetws_chk_warn (__s, size: sz / sizeof (wchar_t), __n, __stream); |
301 | return __fgetws_chk (__s, size: sz / sizeof (wchar_t), __n, __stream); |
302 | } |
303 | |
304 | #ifdef __USE_GNU |
305 | extern wchar_t *__REDIRECT (__fgetws_unlocked_alias, |
306 | (wchar_t *__restrict __s, int __n, |
307 | __FILE *__restrict __stream), fgetws_unlocked) |
308 | __wur; |
309 | extern wchar_t *__REDIRECT (__fgetws_unlocked_chk_warn, |
310 | (wchar_t *__restrict __s, size_t __size, int __n, |
311 | __FILE *__restrict __stream), |
312 | __fgetws_unlocked_chk) |
313 | __wur __warnattr ("fgetws_unlocked called with bigger size than length " |
314 | "of destination buffer" ); |
315 | |
316 | __fortify_function __wur wchar_t * |
317 | fgetws_unlocked (wchar_t *__restrict __s, int __n, __FILE *__restrict __stream) |
318 | { |
319 | size_t sz = __glibc_objsize (__s); |
320 | if (__glibc_safe_or_unknown_len (__n, sizeof (wchar_t), sz)) |
321 | return __fgetws_unlocked_alias (__s, __n, __stream); |
322 | if (__glibc_unsafe_len (__n, sizeof (wchar_t), sz)) |
323 | return __fgetws_unlocked_chk_warn (__s, size: sz / sizeof (wchar_t), __n, |
324 | __stream); |
325 | return __fgetws_unlocked_chk (__s, size: sz / sizeof (wchar_t), __n, __stream); |
326 | } |
327 | #endif |
328 | |
329 | |
330 | extern size_t __REDIRECT_NTH (__wcrtomb_alias, |
331 | (char *__restrict __s, wchar_t __wchar, |
332 | mbstate_t *__restrict __ps), wcrtomb) __wur; |
333 | |
334 | __fortify_function __wur size_t |
335 | __NTH (wcrtomb (char *__restrict __s, wchar_t __wchar, |
336 | mbstate_t *__restrict __ps)) |
337 | { |
338 | /* We would have to include <limits.h> to get a definition of MB_LEN_MAX. |
339 | But this would only disturb the namespace. So we define our own |
340 | version here. */ |
341 | #define __WCHAR_MB_LEN_MAX 16 |
342 | #if defined MB_LEN_MAX && MB_LEN_MAX != __WCHAR_MB_LEN_MAX |
343 | # error "Assumed value of MB_LEN_MAX wrong" |
344 | #endif |
345 | if (__glibc_objsize (__s) != (size_t) -1 |
346 | && __WCHAR_MB_LEN_MAX > __glibc_objsize (__s)) |
347 | return __wcrtomb_chk (__s, __wchar, p: __ps, __glibc_objsize (__s)); |
348 | return __wcrtomb_alias (__s, __wchar, __ps); |
349 | } |
350 | |
351 | |
352 | extern size_t __REDIRECT_NTH (__mbsrtowcs_alias, |
353 | (wchar_t *__restrict __dst, |
354 | const char **__restrict __src, |
355 | size_t __len, mbstate_t *__restrict __ps), |
356 | mbsrtowcs); |
357 | extern size_t __REDIRECT_NTH (__mbsrtowcs_chk_warn, |
358 | (wchar_t *__restrict __dst, |
359 | const char **__restrict __src, |
360 | size_t __len, mbstate_t *__restrict __ps, |
361 | size_t __dstlen), __mbsrtowcs_chk) |
362 | __warnattr ("mbsrtowcs called with dst buffer smaller than len " |
363 | "* sizeof (wchar_t)" ); |
364 | |
365 | __fortify_function size_t |
366 | __NTH (mbsrtowcs (wchar_t *__restrict __dst, const char **__restrict __src, |
367 | size_t __len, mbstate_t *__restrict __ps)) |
368 | { |
369 | return __glibc_fortify_n (mbsrtowcs, __len, sizeof (wchar_t), |
370 | __glibc_objsize (__dst), |
371 | __dst, __src, __len, __ps); |
372 | } |
373 | |
374 | |
375 | extern size_t __REDIRECT_NTH (__wcsrtombs_alias, |
376 | (char *__restrict __dst, |
377 | const wchar_t **__restrict __src, |
378 | size_t __len, mbstate_t *__restrict __ps), |
379 | wcsrtombs); |
380 | extern size_t __REDIRECT_NTH (__wcsrtombs_chk_warn, |
381 | (char *__restrict __dst, |
382 | const wchar_t **__restrict __src, |
383 | size_t __len, mbstate_t *__restrict __ps, |
384 | size_t __dstlen), __wcsrtombs_chk) |
385 | __warnattr ("wcsrtombs called with dst buffer smaller than len" ); |
386 | |
387 | __fortify_function size_t |
388 | __NTH (wcsrtombs (char *__restrict __dst, const wchar_t **__restrict __src, |
389 | size_t __len, mbstate_t *__restrict __ps)) |
390 | { |
391 | return __glibc_fortify (wcsrtombs, __len, sizeof (char), |
392 | __glibc_objsize (__dst), |
393 | __dst, __src, __len, __ps); |
394 | } |
395 | |
396 | |
397 | #ifdef __USE_XOPEN2K8 |
398 | extern size_t __REDIRECT_NTH (__mbsnrtowcs_alias, |
399 | (wchar_t *__restrict __dst, |
400 | const char **__restrict __src, size_t __nmc, |
401 | size_t __len, mbstate_t *__restrict __ps), |
402 | mbsnrtowcs); |
403 | extern size_t __REDIRECT_NTH (__mbsnrtowcs_chk_warn, |
404 | (wchar_t *__restrict __dst, |
405 | const char **__restrict __src, size_t __nmc, |
406 | size_t __len, mbstate_t *__restrict __ps, |
407 | size_t __dstlen), __mbsnrtowcs_chk) |
408 | __warnattr ("mbsnrtowcs called with dst buffer smaller than len " |
409 | "* sizeof (wchar_t)" ); |
410 | |
411 | __fortify_function size_t |
412 | __NTH (mbsnrtowcs (wchar_t *__restrict __dst, const char **__restrict __src, |
413 | size_t __nmc, size_t __len, mbstate_t *__restrict __ps)) |
414 | { |
415 | return __glibc_fortify_n (mbsnrtowcs, __len, sizeof (wchar_t), |
416 | __glibc_objsize (__dst), |
417 | __dst, __src, __nmc, __len, __ps); |
418 | } |
419 | |
420 | |
421 | extern size_t __REDIRECT_NTH (__wcsnrtombs_alias, |
422 | (char *__restrict __dst, |
423 | const wchar_t **__restrict __src, |
424 | size_t __nwc, size_t __len, |
425 | mbstate_t *__restrict __ps), wcsnrtombs); |
426 | extern size_t __REDIRECT_NTH (__wcsnrtombs_chk_warn, |
427 | (char *__restrict __dst, |
428 | const wchar_t **__restrict __src, |
429 | size_t __nwc, size_t __len, |
430 | mbstate_t *__restrict __ps, |
431 | size_t __dstlen), __wcsnrtombs_chk) |
432 | __warnattr ("wcsnrtombs called with dst buffer smaller than len" ); |
433 | |
434 | __fortify_function size_t |
435 | __NTH (wcsnrtombs (char *__restrict __dst, const wchar_t **__restrict __src, |
436 | size_t __nwc, size_t __len, mbstate_t *__restrict __ps)) |
437 | { |
438 | return __glibc_fortify (wcsnrtombs, __len, sizeof (char), |
439 | __glibc_objsize (__dst), |
440 | __dst, __src, __nwc, __len, __ps); |
441 | } |
442 | #endif |
443 | |