stdio2.h 12 KB

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