1
0

stdio2.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 stdio functions.
  17. Copyright (C) 2004-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 _BITS_STDIO2_H
  31. #define _BITS_STDIO2_H 1
  32. #ifndef _STDIO_H
  33. # error "Never include <bits/stdio2.h> directly; use <stdio.h> instead."
  34. #endif
  35. #ifdef __va_arg_pack
  36. __fortify_function int
  37. __NTH (sprintf (char *__restrict __s, const char *__restrict __fmt, ...))
  38. {
  39. return __builtin___sprintf_chk (__s, __USE_FORTIFY_LEVEL - 1,
  40. __glibc_objsize (__s), __fmt,
  41. __va_arg_pack ());
  42. }
  43. #elif __fortify_use_clang
  44. /* clang does not have __va_arg_pack, so defer to va_arg version. */
  45. __fortify_function_error_function __attribute_overloadable__ int
  46. __NTH (sprintf (__fortify_clang_overload_arg (char *, __restrict, __s),
  47. const char *__restrict __fmt, ...))
  48. {
  49. __gnuc_va_list __fortify_ap;
  50. __builtin_va_start (__fortify_ap, __fmt);
  51. int __r = __builtin___vsprintf_chk (__s, __USE_FORTIFY_LEVEL - 1,
  52. __glibc_objsize (__s), __fmt,
  53. __fortify_ap);
  54. __builtin_va_end (__fortify_ap);
  55. return __r;
  56. }
  57. #elif !defined __cplusplus
  58. # define sprintf(str, ...) \
  59. __builtin___sprintf_chk (str, __USE_FORTIFY_LEVEL - 1, \
  60. __glibc_objsize (str), __VA_ARGS__)
  61. #endif
  62. __fortify_function __attribute_overloadable__ int
  63. __NTH (vsprintf (__fortify_clang_overload_arg (char *, __restrict, __s),
  64. const char *__restrict __fmt, __gnuc_va_list __ap))
  65. {
  66. return __builtin___vsprintf_chk (__s, __USE_FORTIFY_LEVEL - 1,
  67. __glibc_objsize (__s), __fmt, __ap);
  68. }
  69. #if defined __USE_ISOC99 || defined __USE_UNIX98
  70. # ifdef __va_arg_pack
  71. __fortify_function int
  72. __NTH (snprintf (char *__restrict __s, size_t __n,
  73. const char *__restrict __fmt, ...))
  74. {
  75. return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
  76. __glibc_objsize (__s), __fmt,
  77. __va_arg_pack ());
  78. }
  79. # elif __fortify_use_clang
  80. /* clang does not have __va_arg_pack, so defer to va_arg version. */
  81. __fortify_function_error_function __attribute_overloadable__ int
  82. __NTH (snprintf (__fortify_clang_overload_arg (char *, __restrict, __s),
  83. size_t __n, const char *__restrict __fmt, ...))
  84. {
  85. __gnuc_va_list __fortify_ap;
  86. __builtin_va_start (__fortify_ap, __fmt);
  87. int __r = __builtin___vsnprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
  88. __glibc_objsize (__s), __fmt,
  89. __fortify_ap);
  90. __builtin_va_end (__fortify_ap);
  91. return __r;
  92. }
  93. # elif !defined __cplusplus
  94. # define snprintf(str, len, ...) \
  95. __builtin___snprintf_chk (str, len, __USE_FORTIFY_LEVEL - 1, \
  96. __glibc_objsize (str), __VA_ARGS__)
  97. # endif
  98. __fortify_function __attribute_overloadable__ int
  99. __NTH (vsnprintf (__fortify_clang_overload_arg (char *, __restrict, __s),
  100. size_t __n, const char *__restrict __fmt,
  101. __gnuc_va_list __ap))
  102. __fortify_clang_warning (__fortify_clang_bos_static_lt (__n, __s),
  103. "call to vsnprintf may overflow the destination "
  104. "buffer")
  105. {
  106. return __builtin___vsnprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
  107. __glibc_objsize (__s), __fmt, __ap);
  108. }
  109. #endif
  110. #if __USE_FORTIFY_LEVEL > 1
  111. # ifdef __va_arg_pack
  112. __fortify_function __nonnull ((1)) int
  113. fprintf (FILE *__restrict __stream, const char *__restrict __fmt, ...)
  114. {
  115. return __fprintf_chk (__stream, __USE_FORTIFY_LEVEL - 1, __fmt,
  116. __va_arg_pack ());
  117. }
  118. __fortify_function int
  119. printf (const char *__restrict __fmt, ...)
  120. {
  121. return __printf_chk (__USE_FORTIFY_LEVEL - 1, __fmt, __va_arg_pack ());
  122. }
  123. # elif __fortify_use_clang
  124. /* clang does not have __va_arg_pack, so defer to va_arg version. */
  125. __fortify_function_error_function __attribute_overloadable__ __nonnull ((1)) int
  126. fprintf (__fortify_clang_overload_arg (FILE *, __restrict, __stream),
  127. const char *__restrict __fmt, ...)
  128. {
  129. __gnuc_va_list __fortify_ap;
  130. __builtin_va_start (__fortify_ap, __fmt);
  131. int __r = __builtin___vfprintf_chk (__stream, __USE_FORTIFY_LEVEL - 1,
  132. __fmt, __fortify_ap);
  133. __builtin_va_end (__fortify_ap);
  134. return __r;
  135. }
  136. __fortify_function_error_function __attribute_overloadable__ int
  137. printf (__fortify_clang_overload_arg (const char *, __restrict, __fmt), ...)
  138. {
  139. __gnuc_va_list __fortify_ap;
  140. __builtin_va_start (__fortify_ap, __fmt);
  141. int __r = __builtin___vprintf_chk (__USE_FORTIFY_LEVEL - 1, __fmt,
  142. __fortify_ap);
  143. __builtin_va_end (__fortify_ap);
  144. return __r;
  145. }
  146. # elif !defined __cplusplus
  147. # define printf(...) \
  148. __printf_chk (__USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
  149. # define fprintf(stream, ...) \
  150. __fprintf_chk (stream, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
  151. # endif
  152. __fortify_function __attribute_overloadable__ int
  153. vprintf (__fortify_clang_overload_arg (const char *, __restrict, __fmt),
  154. __gnuc_va_list __ap)
  155. {
  156. #ifdef __USE_EXTERN_INLINES
  157. return __vfprintf_chk (stdout, __USE_FORTIFY_LEVEL - 1, __fmt, __ap);
  158. #else
  159. return __vprintf_chk (__USE_FORTIFY_LEVEL - 1, __fmt, __ap);
  160. #endif
  161. }
  162. __fortify_function __nonnull ((1)) int
  163. vfprintf (FILE *__restrict __stream,
  164. const char *__restrict __fmt, __gnuc_va_list __ap)
  165. {
  166. return __vfprintf_chk (__stream, __USE_FORTIFY_LEVEL - 1, __fmt, __ap);
  167. }
  168. # ifdef __USE_XOPEN2K8
  169. # ifdef __va_arg_pack
  170. __fortify_function int
  171. dprintf (int __fd, const char *__restrict __fmt, ...)
  172. {
  173. return __dprintf_chk (__fd, __USE_FORTIFY_LEVEL - 1, __fmt,
  174. __va_arg_pack ());
  175. }
  176. # elif __fortify_use_clang
  177. __fortify_function_error_function __attribute_overloadable__ int
  178. dprintf (int __fd, __fortify_clang_overload_arg (const char *, __restrict,
  179. __fmt), ...)
  180. {
  181. __gnuc_va_list __fortify_ap;
  182. __builtin_va_start (__fortify_ap, __fmt);
  183. int __r = __vdprintf_chk (__fd, __USE_FORTIFY_LEVEL - 1, __fmt,
  184. __fortify_ap);
  185. __builtin_va_end (__fortify_ap);
  186. return __r;
  187. }
  188. # elif !defined __cplusplus
  189. # define dprintf(fd, ...) \
  190. __dprintf_chk (fd, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
  191. # endif
  192. __fortify_function int
  193. vdprintf (int __fd, const char *__restrict __fmt, __gnuc_va_list __ap)
  194. {
  195. return __vdprintf_chk (__fd, __USE_FORTIFY_LEVEL - 1, __fmt, __ap);
  196. }
  197. # endif
  198. # ifdef __USE_GNU
  199. # ifdef __va_arg_pack
  200. __fortify_function int
  201. __NTH (asprintf (char **__restrict __ptr, const char *__restrict __fmt, ...))
  202. {
  203. return __asprintf_chk (__ptr, __USE_FORTIFY_LEVEL - 1, __fmt,
  204. __va_arg_pack ());
  205. }
  206. __fortify_function int
  207. __NTH (__asprintf (char **__restrict __ptr, const char *__restrict __fmt,
  208. ...))
  209. {
  210. return __asprintf_chk (__ptr, __USE_FORTIFY_LEVEL - 1, __fmt,
  211. __va_arg_pack ());
  212. }
  213. __fortify_function int
  214. __NTH (obstack_printf (struct obstack *__restrict __obstack,
  215. const char *__restrict __fmt, ...))
  216. {
  217. return __obstack_printf_chk (__obstack, __USE_FORTIFY_LEVEL - 1, __fmt,
  218. __va_arg_pack ());
  219. }
  220. # elif __fortify_use_clang
  221. __fortify_function_error_function __attribute_overloadable__ int
  222. __NTH (asprintf (__fortify_clang_overload_arg (char **, __restrict, __ptr),
  223. const char *__restrict __fmt, ...))
  224. {
  225. __gnuc_va_list __fortify_ap;
  226. __builtin_va_start (__fortify_ap, __fmt);
  227. int __r = __vasprintf_chk (__ptr, __USE_FORTIFY_LEVEL - 1, __fmt,
  228. __fortify_ap);
  229. __builtin_va_end (__fortify_ap);
  230. return __r;
  231. }
  232. __fortify_function_error_function __attribute_overloadable__ int
  233. __NTH (__asprintf (__fortify_clang_overload_arg (char **, __restrict, __ptr),
  234. const char *__restrict __fmt, ...))
  235. {
  236. __gnuc_va_list __fortify_ap;
  237. __builtin_va_start (__fortify_ap, __fmt);
  238. int __r = __vasprintf_chk (__ptr, __USE_FORTIFY_LEVEL - 1, __fmt,
  239. __fortify_ap);
  240. __builtin_va_end (__fortify_ap);
  241. return __r;
  242. }
  243. __fortify_function_error_function __attribute_overloadable__ int
  244. __NTH (obstack_printf (__fortify_clang_overload_arg (struct obstack *,
  245. __restrict, __obstack),
  246. const char *__restrict __fmt, ...))
  247. {
  248. __gnuc_va_list __fortify_ap;
  249. __builtin_va_start (__fortify_ap, __fmt);
  250. int __r = __obstack_vprintf_chk (__obstack, __USE_FORTIFY_LEVEL - 1,
  251. __fmt, __fortify_ap);
  252. __builtin_va_end (__fortify_ap);
  253. return __r;
  254. }
  255. # elif !defined __cplusplus
  256. # define asprintf(ptr, ...) \
  257. __asprintf_chk (ptr, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
  258. # define __asprintf(ptr, ...) \
  259. __asprintf_chk (ptr, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
  260. # define obstack_printf(obstack, ...) \
  261. __obstack_printf_chk (obstack, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
  262. # endif
  263. __fortify_function int
  264. __NTH (vasprintf (char **__restrict __ptr, const char *__restrict __fmt,
  265. __gnuc_va_list __ap))
  266. {
  267. return __vasprintf_chk (__ptr, __USE_FORTIFY_LEVEL - 1, __fmt, __ap);
  268. }
  269. __fortify_function int
  270. __NTH (obstack_vprintf (struct obstack *__restrict __obstack,
  271. const char *__restrict __fmt, __gnuc_va_list __ap))
  272. {
  273. return __obstack_vprintf_chk (__obstack, __USE_FORTIFY_LEVEL - 1, __fmt,
  274. __ap);
  275. }
  276. # endif
  277. #endif
  278. #if __GLIBC_USE (DEPRECATED_GETS)
  279. __fortify_function __wur __attribute_overloadable__ char *
  280. gets (__fortify_clang_overload_arg (char *, , __str))
  281. __fortify_clang_warning (__glibc_objsize (__str) == (size_t) -1,
  282. "please use fgets or getline instead, gets "
  283. "can not specify buffer size")
  284. {
  285. if (__glibc_objsize (__str) != (size_t) -1)
  286. return __gets_chk (__str, __glibc_objsize (__str));
  287. return __gets_warn (__str);
  288. }
  289. #endif
  290. __fortify_function __wur __fortified_attr_access (__write_only__, 1, 2)
  291. __nonnull ((3)) __attribute_overloadable__ char *
  292. fgets (__fortify_clang_overload_arg (char *, __restrict, __s), int __n,
  293. FILE *__restrict __stream)
  294. __fortify_clang_warning (__fortify_clang_bos_static_lt (__n, __s) && __n > 0,
  295. "fgets called with bigger size than length of "
  296. "destination buffer")
  297. {
  298. size_t __sz = __glibc_objsize (__s);
  299. if (__glibc_safe_or_unknown_len (__n, sizeof (char), __sz))
  300. return __fgets_alias (__s, __n, __stream);
  301. #if !__fortify_use_clang
  302. if (__glibc_unsafe_len (__n, sizeof (char), __sz))
  303. return __fgets_chk_warn (__s, __sz, __n, __stream);
  304. #endif
  305. return __fgets_chk (__s, __sz, __n, __stream);
  306. }
  307. __fortify_function __wur __nonnull ((4)) __attribute_overloadable__ size_t
  308. fread (__fortify_clang_overload_arg (void *, __restrict, __ptr),
  309. size_t __size, size_t __n, FILE *__restrict __stream)
  310. __fortify_clang_warning (__fortify_clang_bos0_static_lt (__size * __n, __ptr)
  311. && !__fortify_clang_mul_may_overflow (__size, __n),
  312. "fread called with bigger size * n than length "
  313. "of destination buffer")
  314. {
  315. size_t __sz = __glibc_objsize0 (__ptr);
  316. if (__glibc_safe_or_unknown_len (__n, __size, __sz))
  317. return __fread_alias (__ptr, __size, __n, __stream);
  318. #if !__fortify_use_clang
  319. if (__glibc_unsafe_len (__n, __size, __sz))
  320. return __fread_chk_warn (__ptr, __sz, __size, __n, __stream);
  321. #endif
  322. return __fread_chk (__ptr, __sz, __size, __n, __stream);
  323. }
  324. #ifdef __USE_GNU
  325. __fortify_function __wur __fortified_attr_access (__write_only__, 1, 2)
  326. __nonnull ((3)) __attribute_overloadable__ char *
  327. fgets_unlocked (__fortify_clang_overload_arg (char *, __restrict, __s),
  328. int __n, FILE *__restrict __stream)
  329. __fortify_clang_warning (__fortify_clang_bos_static_lt (__n, __s) && __n > 0,
  330. "fgets called with bigger size than length of "
  331. "destination buffer")
  332. {
  333. size_t __sz = __glibc_objsize (__s);
  334. if (__glibc_safe_or_unknown_len (__n, sizeof (char), __sz))
  335. return __fgets_unlocked_alias (__s, __n, __stream);
  336. #if !__fortify_use_clang
  337. if (__glibc_unsafe_len (__n, sizeof (char), __sz))
  338. return __fgets_unlocked_chk_warn (__s, __sz, __n, __stream);
  339. #endif
  340. return __fgets_unlocked_chk (__s, __sz, __n, __stream);
  341. }
  342. #endif
  343. #ifdef __USE_MISC
  344. # undef fread_unlocked
  345. __fortify_function __wur __nonnull ((4)) __attribute_overloadable__ size_t
  346. fread_unlocked (__fortify_clang_overload_arg0 (void *, __restrict, __ptr),
  347. size_t __size, size_t __n, FILE *__restrict __stream)
  348. __fortify_clang_warning (__fortify_clang_bos0_static_lt (__size * __n, __ptr)
  349. && !__fortify_clang_mul_may_overflow (__size, __n),
  350. "fread_unlocked called with bigger size * n than "
  351. "length of destination buffer")
  352. {
  353. size_t __sz = __glibc_objsize0 (__ptr);
  354. if (__glibc_safe_or_unknown_len (__n, __size, __sz))
  355. {
  356. # ifdef __USE_EXTERN_INLINES
  357. if (__builtin_constant_p (__size)
  358. && __builtin_constant_p (__n)
  359. && (__size | __n) < (((size_t) 1) << (8 * sizeof (size_t) / 2))
  360. && __size * __n <= 8)
  361. {
  362. size_t __cnt = __size * __n;
  363. char *__cptr = (char *) __ptr;
  364. if (__cnt == 0)
  365. return 0;
  366. for (; __cnt > 0; --__cnt)
  367. {
  368. int __c = getc_unlocked (__stream);
  369. if (__c == EOF)
  370. break;
  371. *__cptr++ = __c;
  372. }
  373. return (__cptr - (char *) __ptr) / __size;
  374. }
  375. # endif
  376. return __fread_unlocked_alias (__ptr, __size, __n, __stream);
  377. }
  378. # if !__fortify_use_clang
  379. if (__glibc_unsafe_len (__n, __size, __sz))
  380. return __fread_unlocked_chk_warn (__ptr, __sz, __size, __n, __stream);
  381. # endif
  382. return __fread_unlocked_chk (__ptr, __sz, __size, __n, __stream);
  383. }
  384. #endif
  385. #endif /* bits/stdio2.h. */