wchar2.h 14 KB

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