1/* Checking macros for wchar functions.
2 Copyright (C) 2005-2024 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__fortify_function wchar_t *
24__NTH (wmemcpy (wchar_t *__restrict __s1, const wchar_t *__restrict __s2,
25 size_t __n))
26{
27 return __glibc_fortify_n (wmemcpy, __n, sizeof (wchar_t),
28 __glibc_objsize0 (__s1),
29 __s1, __s2, __n);
30}
31
32__fortify_function wchar_t *
33__NTH (wmemmove (wchar_t *__s1, const wchar_t *__s2, size_t __n))
34{
35 return __glibc_fortify_n (wmemmove, __n, sizeof (wchar_t),
36 __glibc_objsize0 (__s1),
37 __s1, __s2, __n);
38}
39
40#ifdef __USE_GNU
41__fortify_function wchar_t *
42__NTH (wmempcpy (wchar_t *__restrict __s1, const wchar_t *__restrict __s2,
43 size_t __n))
44{
45 return __glibc_fortify_n (wmempcpy, __n, sizeof (wchar_t),
46 __glibc_objsize0 (__s1),
47 __s1, __s2, __n);
48}
49#endif
50
51__fortify_function wchar_t *
52__NTH (wmemset (wchar_t *__s, wchar_t __c, size_t __n))
53{
54 return __glibc_fortify_n (wmemset, __n, sizeof (wchar_t),
55 __glibc_objsize0 (__s),
56 __s, __c, __n);
57}
58
59__fortify_function wchar_t *
60__NTH (wcscpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src))
61{
62 size_t sz = __glibc_objsize (__dest);
63 if (sz != (size_t) -1)
64 return __wcscpy_chk (__dest, __src, n: sz / sizeof (wchar_t));
65 return __wcscpy_alias (__dest, __src);
66}
67
68__fortify_function wchar_t *
69__NTH (wcpcpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src))
70{
71 size_t sz = __glibc_objsize (__dest);
72 if (sz != (size_t) -1)
73 return __wcpcpy_chk (__dest, __src, destlen: sz / sizeof (wchar_t));
74 return __wcpcpy_alias (__dest, __src);
75}
76
77__fortify_function wchar_t *
78__NTH (wcsncpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src,
79 size_t __n))
80{
81 return __glibc_fortify_n (wcsncpy, __n, sizeof (wchar_t),
82 __glibc_objsize (__dest),
83 __dest, __src, __n);
84}
85
86__fortify_function wchar_t *
87__NTH (wcpncpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src,
88 size_t __n))
89{
90 return __glibc_fortify_n (wcpncpy, __n, sizeof (wchar_t),
91 __glibc_objsize (__dest),
92 __dest, __src, __n);
93}
94
95__fortify_function wchar_t *
96__NTH (wcscat (wchar_t *__restrict __dest, const wchar_t *__restrict __src))
97{
98 size_t sz = __glibc_objsize (__dest);
99 if (sz != (size_t) -1)
100 return __wcscat_chk (__dest, __src, destlen: sz / sizeof (wchar_t));
101 return __wcscat_alias (__dest, __src);
102}
103
104__fortify_function wchar_t *
105__NTH (wcsncat (wchar_t *__restrict __dest, const wchar_t *__restrict __src,
106 size_t __n))
107{
108 size_t sz = __glibc_objsize (__dest);
109 if (sz != (size_t) -1)
110 return __wcsncat_chk (__dest, __src, __n, destlen: sz / sizeof (wchar_t));
111 return __wcsncat_alias (__dest, __src, __n);
112}
113
114#ifdef __USE_MISC
115__fortify_function size_t
116__NTH (wcslcpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src,
117 size_t __n))
118{
119 if (__glibc_objsize (__dest) != (size_t) -1
120 && (!__builtin_constant_p (__n
121 > __glibc_objsize (__dest) / sizeof (wchar_t))
122 || __n > __glibc_objsize (__dest) / sizeof (wchar_t)))
123 return __wcslcpy_chk (__dest, __src, __n,
124 __glibc_objsize (__dest) / sizeof (wchar_t));
125 return __wcslcpy_alias (__dest, __src, __n);
126}
127
128__fortify_function size_t
129__NTH (wcslcat (wchar_t *__restrict __dest, const wchar_t *__restrict __src,
130 size_t __n))
131{
132 if (__glibc_objsize (__dest) != (size_t) -1
133 && (!__builtin_constant_p (__n > __glibc_objsize (__dest)
134 / sizeof (wchar_t))
135 || __n > __glibc_objsize (__dest) / sizeof (wchar_t)))
136 return __wcslcat_chk (__dest, __src, __n,
137 __glibc_objsize (__dest) / sizeof (wchar_t));
138 return __wcslcat_alias (__dest, __src, __n);
139}
140#endif /* __USE_MISC */
141
142#ifdef __va_arg_pack
143__fortify_function int
144__NTH (swprintf (wchar_t *__restrict __s, size_t __n,
145 const wchar_t *__restrict __fmt, ...))
146{
147 size_t sz = __glibc_objsize (__s);
148 if (sz != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
149 return __swprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
150 sz / sizeof (wchar_t), __fmt, __va_arg_pack ());
151 return __swprintf_alias (__s, __n, __fmt, __va_arg_pack ());
152}
153#elif !defined __cplusplus
154/* XXX We might want to have support in gcc for swprintf. */
155# define swprintf(s, n, ...) \
156 (__glibc_objsize (s) != (size_t) -1 || __USE_FORTIFY_LEVEL > 1 \
157 ? __swprintf_chk (s, n, __USE_FORTIFY_LEVEL - 1, \
158 __glibc_objsize (s) / sizeof (wchar_t), __VA_ARGS__) \
159 : swprintf (s, n, __VA_ARGS__))
160#endif
161
162__fortify_function int
163__NTH (vswprintf (wchar_t *__restrict __s, size_t __n,
164 const wchar_t *__restrict __fmt, __gnuc_va_list __ap))
165{
166 size_t sz = __glibc_objsize (__s);
167 if (sz != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
168 return __vswprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
169 s_len: sz / sizeof (wchar_t), format: __fmt, arg: __ap);
170 return __vswprintf_alias (__s, __n, __fmt, __ap);
171}
172
173
174#if __USE_FORTIFY_LEVEL > 1
175
176# ifdef __va_arg_pack
177__fortify_function int
178wprintf (const wchar_t *__restrict __fmt, ...)
179{
180 return __wprintf_chk (__USE_FORTIFY_LEVEL - 1, __fmt, __va_arg_pack ());
181}
182
183__fortify_function int
184fwprintf (__FILE *__restrict __stream, const wchar_t *__restrict __fmt, ...)
185{
186 return __fwprintf_chk (__stream, __USE_FORTIFY_LEVEL - 1, __fmt,
187 __va_arg_pack ());
188}
189# elif !defined __cplusplus
190# define wprintf(...) \
191 __wprintf_chk (__USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
192# define fwprintf(stream, ...) \
193 __fwprintf_chk (stream, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
194# endif
195
196__fortify_function int
197vwprintf (const wchar_t *__restrict __fmt, __gnuc_va_list __ap)
198{
199 return __vwprintf_chk (__USE_FORTIFY_LEVEL - 1, format: __fmt, __ap);
200}
201
202__fortify_function int
203vfwprintf (__FILE *__restrict __stream,
204 const wchar_t *__restrict __fmt, __gnuc_va_list __ap)
205{
206 return __vfwprintf_chk (__stream, __USE_FORTIFY_LEVEL - 1, format: __fmt, __ap);
207}
208
209#endif
210__fortify_function __wur wchar_t *
211fgetws (wchar_t *__restrict __s, int __n, __FILE *__restrict __stream)
212{
213 size_t sz = __glibc_objsize (__s);
214 if (__glibc_safe_or_unknown_len (__n, sizeof (wchar_t), sz))
215 return __fgetws_alias (__s, __n, __stream);
216 if (__glibc_unsafe_len (__n, sizeof (wchar_t), sz))
217 return __fgetws_chk_warn (__s, size: sz / sizeof (wchar_t), __n, __stream);
218 return __fgetws_chk (__s, size: sz / sizeof (wchar_t), __n, __stream);
219}
220
221#ifdef __USE_GNU
222__fortify_function __wur wchar_t *
223fgetws_unlocked (wchar_t *__restrict __s, int __n, __FILE *__restrict __stream)
224{
225 size_t sz = __glibc_objsize (__s);
226 if (__glibc_safe_or_unknown_len (__n, sizeof (wchar_t), sz))
227 return __fgetws_unlocked_alias (__s, __n, __stream);
228 if (__glibc_unsafe_len (__n, sizeof (wchar_t), sz))
229 return __fgetws_unlocked_chk_warn (__s, size: sz / sizeof (wchar_t), __n,
230 __stream);
231 return __fgetws_unlocked_chk (__s, size: sz / sizeof (wchar_t), __n, __stream);
232}
233#endif
234
235__fortify_function __wur size_t
236__NTH (wcrtomb (char *__restrict __s, wchar_t __wchar,
237 mbstate_t *__restrict __ps))
238{
239 /* We would have to include <limits.h> to get a definition of MB_LEN_MAX.
240 But this would only disturb the namespace. So we define our own
241 version here. */
242#define __WCHAR_MB_LEN_MAX 16
243#if defined MB_LEN_MAX && MB_LEN_MAX != __WCHAR_MB_LEN_MAX
244# error "Assumed value of MB_LEN_MAX wrong"
245#endif
246 if (__glibc_objsize (__s) != (size_t) -1
247 && __WCHAR_MB_LEN_MAX > __glibc_objsize (__s))
248 return __wcrtomb_chk (__s, __wchar, p: __ps, __glibc_objsize (__s));
249 return __wcrtomb_alias (__s, __wchar, __ps);
250}
251
252__fortify_function size_t
253__NTH (mbsrtowcs (wchar_t *__restrict __dst, const char **__restrict __src,
254 size_t __len, mbstate_t *__restrict __ps))
255{
256 return __glibc_fortify_n (mbsrtowcs, __len, sizeof (wchar_t),
257 __glibc_objsize (__dst),
258 __dst, __src, __len, __ps);
259}
260
261__fortify_function size_t
262__NTH (wcsrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
263 size_t __len, mbstate_t *__restrict __ps))
264{
265 return __glibc_fortify (wcsrtombs, __len, sizeof (char),
266 __glibc_objsize (__dst),
267 __dst, __src, __len, __ps);
268}
269
270
271#ifdef __USE_XOPEN2K8
272__fortify_function size_t
273__NTH (mbsnrtowcs (wchar_t *__restrict __dst, const char **__restrict __src,
274 size_t __nmc, size_t __len, mbstate_t *__restrict __ps))
275{
276 return __glibc_fortify_n (mbsnrtowcs, __len, sizeof (wchar_t),
277 __glibc_objsize (__dst),
278 __dst, __src, __nmc, __len, __ps);
279}
280
281__fortify_function size_t
282__NTH (wcsnrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
283 size_t __nwc, size_t __len, mbstate_t *__restrict __ps))
284{
285 return __glibc_fortify (wcsnrtombs, __len, sizeof (char),
286 __glibc_objsize (__dst),
287 __dst, __src, __nwc, __len, __ps);
288}
289#endif
290

source code of glibc/wcsmbs/bits/wchar2.h