wchar2.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* Checking macros for wchar functions.
  2. Copyright (C) 2005-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _WCHAR_H
  16. # error "Never include <bits/wchar2.h> directly; use <wchar.h> instead."
  17. #endif
  18. __fortify_function __attribute_overloadable__ wchar_t *
  19. __NTH (wmemcpy (__fortify_clang_overload_arg (wchar_t *, __restrict, __s1),
  20. const wchar_t *__restrict __s2, size_t __n))
  21. __fortify_clang_warning_only_if_bos0_lt2 (__n, __s1, sizeof (wchar_t),
  22. "wmemcpy called with length bigger "
  23. "than size of destination buffer")
  24. {
  25. return __glibc_fortify_n (wmemcpy, __n, sizeof (wchar_t),
  26. __glibc_objsize0 (__s1),
  27. __s1, __s2, __n);
  28. }
  29. __fortify_function __attribute_overloadable__ wchar_t *
  30. __NTH (wmemmove (__fortify_clang_overload_arg (wchar_t *, ,__s1),
  31. const wchar_t *__s2, size_t __n))
  32. __fortify_clang_warning_only_if_bos0_lt2 (__n, __s1, sizeof (wchar_t),
  33. "wmemmove called with length bigger "
  34. "than size of destination buffer")
  35. {
  36. return __glibc_fortify_n (wmemmove, __n, sizeof (wchar_t),
  37. __glibc_objsize0 (__s1),
  38. __s1, __s2, __n);
  39. }
  40. #ifdef __USE_GNU
  41. __fortify_function __attribute_overloadable__ wchar_t *
  42. __NTH (wmempcpy (__fortify_clang_overload_arg (wchar_t *, __restrict, __s1),
  43. const wchar_t *__restrict __s2, size_t __n))
  44. __fortify_clang_warning_only_if_bos0_lt2 (__n, __s1, sizeof (wchar_t),
  45. "wmempcpy called with length bigger "
  46. "than size of destination buffer")
  47. {
  48. return __glibc_fortify_n (wmempcpy, __n, sizeof (wchar_t),
  49. __glibc_objsize0 (__s1),
  50. __s1, __s2, __n);
  51. }
  52. #endif
  53. __fortify_function __attribute_overloadable__ wchar_t *
  54. __NTH (wmemset (__fortify_clang_overload_arg (wchar_t *, ,__s), wchar_t __c,
  55. size_t __n))
  56. __fortify_clang_warning_only_if_bos0_lt2 (__n, __s, sizeof (wchar_t),
  57. "wmemset called with length bigger "
  58. "than size of destination buffer")
  59. {
  60. return __glibc_fortify_n (wmemset, __n, sizeof (wchar_t),
  61. __glibc_objsize0 (__s),
  62. __s, __c, __n);
  63. }
  64. __fortify_function __attribute_overloadable__ wchar_t *
  65. __NTH (wcscpy (__fortify_clang_overload_arg (wchar_t *, __restrict, __dest),
  66. const wchar_t *__restrict __src))
  67. {
  68. size_t __sz = __glibc_objsize (__dest);
  69. if (__sz != (size_t) -1)
  70. return __wcscpy_chk (__dest, __src, __sz / sizeof (wchar_t));
  71. return __wcscpy_alias (__dest, __src);
  72. }
  73. __fortify_function __attribute_overloadable__ wchar_t *
  74. __NTH (wcpcpy (__fortify_clang_overload_arg (wchar_t *, __restrict, __dest),
  75. const wchar_t *__restrict __src))
  76. {
  77. size_t __sz = __glibc_objsize (__dest);
  78. if (__sz != (size_t) -1)
  79. return __wcpcpy_chk (__dest, __src, __sz / sizeof (wchar_t));
  80. return __wcpcpy_alias (__dest, __src);
  81. }
  82. __fortify_function __attribute_overloadable__ wchar_t *
  83. __NTH (wcsncpy (__fortify_clang_overload_arg (wchar_t *, __restrict, __dest),
  84. const wchar_t *__restrict __src, size_t __n))
  85. __fortify_clang_warning_only_if_bos0_lt2 (__n, __dest, sizeof (wchar_t),
  86. "wcsncpy called with length bigger "
  87. "than size of destination buffer")
  88. {
  89. return __glibc_fortify_n (wcsncpy, __n, sizeof (wchar_t),
  90. __glibc_objsize (__dest),
  91. __dest, __src, __n);
  92. }
  93. __fortify_function __attribute_overloadable__ wchar_t *
  94. __NTH (wcpncpy (__fortify_clang_overload_arg (wchar_t *, __restrict, __dest),
  95. const wchar_t *__restrict __src, size_t __n))
  96. __fortify_clang_warning_only_if_bos0_lt2 (__n, __dest, sizeof (wchar_t),
  97. "wcpncpy called with length bigger "
  98. "than size of destination buffer")
  99. {
  100. return __glibc_fortify_n (wcpncpy, __n, sizeof (wchar_t),
  101. __glibc_objsize (__dest),
  102. __dest, __src, __n);
  103. }
  104. __fortify_function __attribute_overloadable__ wchar_t *
  105. __NTH (wcscat (__fortify_clang_overload_arg (wchar_t *, __restrict, __dest),
  106. const wchar_t *__restrict __src))
  107. {
  108. size_t __sz = __glibc_objsize (__dest);
  109. if (__sz != (size_t) -1)
  110. return __wcscat_chk (__dest, __src, __sz / sizeof (wchar_t));
  111. return __wcscat_alias (__dest, __src);
  112. }
  113. __fortify_function __attribute_overloadable__ wchar_t *
  114. __NTH (wcsncat (__fortify_clang_overload_arg (wchar_t *, __restrict, __dest),
  115. const wchar_t *__restrict __src, size_t __n))
  116. {
  117. size_t __sz = __glibc_objsize (__dest);
  118. if (__sz != (size_t) -1)
  119. return __wcsncat_chk (__dest, __src, __n, __sz / sizeof (wchar_t));
  120. return __wcsncat_alias (__dest, __src, __n);
  121. }
  122. #ifdef __USE_MISC
  123. __fortify_function __attribute_overloadable__ size_t
  124. __NTH (wcslcpy (__fortify_clang_overload_arg (wchar_t *, __restrict, __dest),
  125. const wchar_t *__restrict __src, size_t __n))
  126. __fortify_clang_warning_only_if_bos0_lt2 (__n, __dest, sizeof (wchar_t),
  127. "wcslcpy called with length bigger "
  128. "than size of destination buffer")
  129. {
  130. if (__glibc_objsize (__dest) != (size_t) -1
  131. && (!__builtin_constant_p (__n
  132. > __glibc_objsize (__dest) / sizeof (wchar_t))
  133. || __n > __glibc_objsize (__dest) / sizeof (wchar_t)))
  134. return __wcslcpy_chk (__dest, __src, __n,
  135. __glibc_objsize (__dest) / sizeof (wchar_t));
  136. return __wcslcpy_alias (__dest, __src, __n);
  137. }
  138. __fortify_function __attribute_overloadable__ size_t
  139. __NTH (wcslcat (__fortify_clang_overload_arg (wchar_t *, __restrict, __dest),
  140. const wchar_t *__restrict __src, size_t __n))
  141. {
  142. if (__glibc_objsize (__dest) != (size_t) -1
  143. && (!__builtin_constant_p (__n > __glibc_objsize (__dest)
  144. / sizeof (wchar_t))
  145. || __n > __glibc_objsize (__dest) / sizeof (wchar_t)))
  146. return __wcslcat_chk (__dest, __src, __n,
  147. __glibc_objsize (__dest) / sizeof (wchar_t));
  148. return __wcslcat_alias (__dest, __src, __n);
  149. }
  150. #endif /* __USE_MISC */
  151. #ifdef __va_arg_pack
  152. __fortify_function int
  153. __NTH (swprintf (wchar_t *__restrict __s, size_t __n,
  154. const wchar_t *__restrict __fmt, ...))
  155. {
  156. size_t __sz = __glibc_objsize (__s);
  157. if (__sz != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
  158. return __swprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
  159. __sz / sizeof (wchar_t), __fmt, __va_arg_pack ());
  160. return __swprintf_alias (__s, __n, __fmt, __va_arg_pack ());
  161. }
  162. #elif __fortify_use_clang
  163. __fortify_function_error_function __attribute_overloadable__ int
  164. __NTH (swprintf (__fortify_clang_overload_arg (wchar_t *, __restrict, __s),
  165. size_t __n, const wchar_t *__restrict __fmt, ...))
  166. {
  167. __gnuc_va_list __fortify_ap;
  168. __builtin_va_start (__fortify_ap, __fmt);
  169. int __r;
  170. if (__glibc_objsize (__s) != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
  171. __r = __vswprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
  172. __glibc_objsize (__s) / sizeof (wchar_t),
  173. __fmt, __fortify_ap);
  174. else
  175. __r = __vswprintf_alias (__s, __n, __fmt, __fortify_ap);
  176. __builtin_va_end (__fortify_ap);
  177. return __r;
  178. }
  179. #elif !defined __cplusplus
  180. /* XXX We might want to have support in gcc for swprintf. */
  181. # define swprintf(s, n, ...) \
  182. (__glibc_objsize (s) != (size_t) -1 || __USE_FORTIFY_LEVEL > 1 \
  183. ? __swprintf_chk (s, n, __USE_FORTIFY_LEVEL - 1, \
  184. __glibc_objsize (s) / sizeof (wchar_t), __VA_ARGS__) \
  185. : swprintf (s, n, __VA_ARGS__))
  186. #endif
  187. __fortify_function int
  188. __NTH (vswprintf (wchar_t *__restrict __s, size_t __n,
  189. const wchar_t *__restrict __fmt, __gnuc_va_list __ap))
  190. {
  191. size_t __sz = __glibc_objsize (__s);
  192. if (__sz != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
  193. return __vswprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
  194. __sz / sizeof (wchar_t), __fmt, __ap);
  195. return __vswprintf_alias (__s, __n, __fmt, __ap);
  196. }
  197. #if __USE_FORTIFY_LEVEL > 1
  198. # ifdef __va_arg_pack
  199. __fortify_function int
  200. wprintf (const wchar_t *__restrict __fmt, ...)
  201. {
  202. return __wprintf_chk (__USE_FORTIFY_LEVEL - 1, __fmt, __va_arg_pack ());
  203. }
  204. __fortify_function int
  205. fwprintf (__FILE *__restrict __stream, const wchar_t *__restrict __fmt, ...)
  206. {
  207. return __fwprintf_chk (__stream, __USE_FORTIFY_LEVEL - 1, __fmt,
  208. __va_arg_pack ());
  209. }
  210. # elif !defined __cplusplus
  211. # define wprintf(...) \
  212. __wprintf_chk (__USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
  213. # define fwprintf(stream, ...) \
  214. __fwprintf_chk (stream, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
  215. # endif
  216. __fortify_function int
  217. vwprintf (const wchar_t *__restrict __fmt, __gnuc_va_list __ap)
  218. {
  219. return __vwprintf_chk (__USE_FORTIFY_LEVEL - 1, __fmt, __ap);
  220. }
  221. __fortify_function int
  222. vfwprintf (__FILE *__restrict __stream,
  223. const wchar_t *__restrict __fmt, __gnuc_va_list __ap)
  224. {
  225. return __vfwprintf_chk (__stream, __USE_FORTIFY_LEVEL - 1, __fmt, __ap);
  226. }
  227. #endif
  228. __fortify_function __attribute_overloadable__ __wur wchar_t *
  229. fgetws (__fortify_clang_overload_arg (wchar_t *, __restrict, __s), int __n,
  230. __FILE *__restrict __stream)
  231. __fortify_clang_warning_only_if_bos_lt2 (__n, __s, sizeof (wchar_t),
  232. "fgetws called with length bigger "
  233. "than size of destination buffer")
  234. {
  235. size_t __sz = __glibc_objsize (__s);
  236. if (__glibc_safe_or_unknown_len (__n, sizeof (wchar_t), __sz))
  237. return __fgetws_alias (__s, __n, __stream);
  238. #if !__fortify_use_clang
  239. if (__glibc_unsafe_len (__n, sizeof (wchar_t), __sz))
  240. return __fgetws_chk_warn (__s, __sz / sizeof (wchar_t), __n, __stream);
  241. #endif
  242. return __fgetws_chk (__s, __sz / sizeof (wchar_t), __n, __stream);
  243. }
  244. #ifdef __USE_GNU
  245. __fortify_function __attribute_overloadable__ __wur wchar_t *
  246. fgetws_unlocked (__fortify_clang_overload_arg (wchar_t *, __restrict, __s),
  247. int __n, __FILE *__restrict __stream)
  248. __fortify_clang_warning_only_if_bos_lt2 (__n, __s, sizeof (wchar_t),
  249. "fgetws_unlocked called with length bigger "
  250. "than size of destination buffer")
  251. {
  252. size_t __sz = __glibc_objsize (__s);
  253. if (__glibc_safe_or_unknown_len (__n, sizeof (wchar_t), __sz))
  254. return __fgetws_unlocked_alias (__s, __n, __stream);
  255. # if !__fortify_use_clang
  256. if (__glibc_unsafe_len (__n, sizeof (wchar_t), __sz))
  257. return __fgetws_unlocked_chk_warn (__s, __sz / sizeof (wchar_t), __n,
  258. __stream);
  259. # endif
  260. return __fgetws_unlocked_chk (__s, __sz / sizeof (wchar_t), __n, __stream);
  261. }
  262. #endif
  263. __fortify_function __attribute_overloadable__ __wur size_t
  264. __NTH (wcrtomb (__fortify_clang_overload_arg (char *, __restrict, __s),
  265. wchar_t __wchar, mbstate_t *__restrict __ps))
  266. {
  267. /* We would have to include <limits.h> to get a definition of MB_LEN_MAX.
  268. But this would only disturb the namespace. So we define our own
  269. version here. */
  270. #define __WCHAR_MB_LEN_MAX 16
  271. #if defined MB_LEN_MAX && MB_LEN_MAX != __WCHAR_MB_LEN_MAX
  272. # error "Assumed value of MB_LEN_MAX wrong"
  273. #endif
  274. if (__glibc_objsize (__s) != (size_t) -1
  275. && __WCHAR_MB_LEN_MAX > __glibc_objsize (__s))
  276. return __wcrtomb_chk (__s, __wchar, __ps, __glibc_objsize (__s));
  277. return __wcrtomb_alias (__s, __wchar, __ps);
  278. }
  279. __fortify_function __attribute_overloadable__ size_t
  280. __NTH (mbsrtowcs (__fortify_clang_overload_arg (wchar_t *, __restrict, __dst),
  281. const char **__restrict __src,
  282. size_t __len, mbstate_t *__restrict __ps))
  283. __fortify_clang_warning_only_if_bos_lt2 (__len, __dst, sizeof (wchar_t),
  284. "mbsrtowcs called with dst buffer "
  285. "smaller than len * sizeof (wchar_t)")
  286. {
  287. return __glibc_fortify_n (mbsrtowcs, __len, sizeof (wchar_t),
  288. __glibc_objsize (__dst),
  289. __dst, __src, __len, __ps);
  290. }
  291. __fortify_function __attribute_overloadable__ size_t
  292. __NTH (wcsrtombs (__fortify_clang_overload_arg (char *, __restrict, __dst),
  293. const wchar_t **__restrict __src,
  294. size_t __len, mbstate_t *__restrict __ps))
  295. __fortify_clang_warning_only_if_bos_lt (__len, __dst,
  296. "wcsrtombs called with dst buffer "
  297. "smaller than len")
  298. {
  299. return __glibc_fortify (wcsrtombs, __len, sizeof (char),
  300. __glibc_objsize (__dst),
  301. __dst, __src, __len, __ps);
  302. }
  303. #ifdef __USE_XOPEN2K8
  304. __fortify_function __attribute_overloadable__ size_t
  305. __NTH (mbsnrtowcs (__fortify_clang_overload_arg (wchar_t *, __restrict, __dst),
  306. const char **__restrict __src, size_t __nmc, size_t __len,
  307. mbstate_t *__restrict __ps))
  308. __fortify_clang_warning_only_if_bos_lt (sizeof (wchar_t) * __len, __dst,
  309. "mbsnrtowcs called with dst buffer "
  310. "smaller than len * sizeof (wchar_t)")
  311. {
  312. return __glibc_fortify_n (mbsnrtowcs, __len, sizeof (wchar_t),
  313. __glibc_objsize (__dst),
  314. __dst, __src, __nmc, __len, __ps);
  315. }
  316. __fortify_function __attribute_overloadable__ size_t
  317. __NTH (wcsnrtombs (__fortify_clang_overload_arg (char *, __restrict, __dst),
  318. const wchar_t **__restrict __src, size_t __nwc,
  319. size_t __len, mbstate_t *__restrict __ps))
  320. __fortify_clang_warning_only_if_bos_lt (__len, __dst,
  321. "wcsnrtombs called with dst buffer "
  322. "smaller than len")
  323. {
  324. return __glibc_fortify (wcsnrtombs, __len, sizeof (char),
  325. __glibc_objsize (__dst),
  326. __dst, __src, __nwc, __len, __ps);
  327. }
  328. #endif