stdio2.h 12 KB

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