stdio2.h 13 KB

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