stdio2.h 12 KB

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