1/* Checking macros for stdio functions.
2 Copyright (C) 2004-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19#ifndef _STDIO_H
20# error "Never include <bits/stdio2.h> directly; use <stdio.h> instead."
21#endif
22
23extern int __sprintf_chk (char *__restrict __s, int __flag, size_t __slen,
24 const char *__restrict __format, ...) __THROW;
25extern int __vsprintf_chk (char *__restrict __s, int __flag, size_t __slen,
26 const char *__restrict __format,
27 _G_va_list __ap) __THROW;
28
29#ifdef __va_arg_pack
30__fortify_function int
31__NTH (sprintf (char *__restrict __s, const char *__restrict __fmt, ...))
32{
33 return __builtin___sprintf_chk (__s, __USE_FORTIFY_LEVEL - 1,
34 __bos (__s), __fmt, __va_arg_pack ());
35}
36#elif !defined __cplusplus
37# define sprintf(str, ...) \
38 __builtin___sprintf_chk (str, __USE_FORTIFY_LEVEL - 1, __bos (str), \
39 __VA_ARGS__)
40#endif
41
42__fortify_function int
43__NTH (vsprintf (char *__restrict __s, const char *__restrict __fmt,
44 _G_va_list __ap))
45{
46 return __builtin___vsprintf_chk (__s, __USE_FORTIFY_LEVEL - 1,
47 __bos (__s), __fmt, __ap);
48}
49
50#if defined __USE_ISOC99 || defined __USE_UNIX98
51
52extern int __snprintf_chk (char *__restrict __s, size_t __n, int __flag,
53 size_t __slen, const char *__restrict __format,
54 ...) __THROW;
55extern int __vsnprintf_chk (char *__restrict __s, size_t __n, int __flag,
56 size_t __slen, const char *__restrict __format,
57 _G_va_list __ap) __THROW;
58
59# ifdef __va_arg_pack
60__fortify_function int
61__NTH (snprintf (char *__restrict __s, size_t __n,
62 const char *__restrict __fmt, ...))
63{
64 return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
65 __bos (__s), __fmt, __va_arg_pack ());
66}
67# elif !defined __cplusplus
68# define snprintf(str, len, ...) \
69 __builtin___snprintf_chk (str, len, __USE_FORTIFY_LEVEL - 1, __bos (str), \
70 __VA_ARGS__)
71# endif
72
73__fortify_function int
74__NTH (vsnprintf (char *__restrict __s, size_t __n,
75 const char *__restrict __fmt, _G_va_list __ap))
76{
77 return __builtin___vsnprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
78 __bos (__s), __fmt, __ap);
79}
80
81#endif
82
83#if __USE_FORTIFY_LEVEL > 1
84
85extern int __fprintf_chk (FILE *__restrict __stream, int __flag,
86 const char *__restrict __format, ...);
87extern int __printf_chk (int __flag, const char *__restrict __format, ...);
88extern int __vfprintf_chk (FILE *__restrict __stream, int __flag,
89 const char *__restrict __format, _G_va_list __ap);
90extern int __vprintf_chk (int __flag, const char *__restrict __format,
91 _G_va_list __ap);
92
93# ifdef __va_arg_pack
94__fortify_function int
95fprintf (FILE *__restrict __stream, const char *__restrict __fmt, ...)
96{
97 return __fprintf_chk (__stream, __USE_FORTIFY_LEVEL - 1, __fmt,
98 __va_arg_pack ());
99}
100
101__fortify_function int
102printf (const char *__restrict __fmt, ...)
103{
104 return __printf_chk (__USE_FORTIFY_LEVEL - 1, __fmt, __va_arg_pack ());
105}
106# elif !defined __cplusplus
107# define printf(...) \
108 __printf_chk (__USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
109# define fprintf(stream, ...) \
110 __fprintf_chk (stream, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
111# endif
112
113__fortify_function int
114vprintf (const char *__restrict __fmt, _G_va_list __ap)
115{
116#ifdef __USE_EXTERN_INLINES
117 return __vfprintf_chk (stdout, __USE_FORTIFY_LEVEL - 1, __fmt, __ap);
118#else
119 return __vprintf_chk (__USE_FORTIFY_LEVEL - 1, __fmt, __ap);
120#endif
121}
122
123__fortify_function int
124vfprintf (FILE *__restrict __stream,
125 const char *__restrict __fmt, _G_va_list __ap)
126{
127 return __vfprintf_chk (__stream, __USE_FORTIFY_LEVEL - 1, __fmt, __ap);
128}
129
130# ifdef __USE_XOPEN2K8
131extern int __dprintf_chk (int __fd, int __flag, const char *__restrict __fmt,
132 ...) __attribute__ ((__format__ (__printf__, 3, 4)));
133extern int __vdprintf_chk (int __fd, int __flag,
134 const char *__restrict __fmt, _G_va_list __arg)
135 __attribute__ ((__format__ (__printf__, 3, 0)));
136
137# ifdef __va_arg_pack
138__fortify_function int
139dprintf (int __fd, const char *__restrict __fmt, ...)
140{
141 return __dprintf_chk (__fd, __USE_FORTIFY_LEVEL - 1, __fmt,
142 __va_arg_pack ());
143}
144# elif !defined __cplusplus
145# define dprintf(fd, ...) \
146 __dprintf_chk (fd, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
147# endif
148
149__fortify_function int
150vdprintf (int __fd, const char *__restrict __fmt, _G_va_list __ap)
151{
152 return __vdprintf_chk (__fd, __USE_FORTIFY_LEVEL - 1, __fmt, __ap);
153}
154# endif
155
156# ifdef __USE_GNU
157
158extern int __asprintf_chk (char **__restrict __ptr, int __flag,
159 const char *__restrict __fmt, ...)
160 __THROW __attribute__ ((__format__ (__printf__, 3, 4))) __wur;
161extern int __vasprintf_chk (char **__restrict __ptr, int __flag,
162 const char *__restrict __fmt, _G_va_list __arg)
163 __THROW __attribute__ ((__format__ (__printf__, 3, 0))) __wur;
164extern int __obstack_printf_chk (struct obstack *__restrict __obstack,
165 int __flag, const char *__restrict __format,
166 ...)
167 __THROW __attribute__ ((__format__ (__printf__, 3, 4)));
168extern int __obstack_vprintf_chk (struct obstack *__restrict __obstack,
169 int __flag,
170 const char *__restrict __format,
171 _G_va_list __args)
172 __THROW __attribute__ ((__format__ (__printf__, 3, 0)));
173
174# ifdef __va_arg_pack
175__fortify_function int
176__NTH (asprintf (char **__restrict __ptr, const char *__restrict __fmt, ...))
177{
178 return __asprintf_chk (__ptr, __USE_FORTIFY_LEVEL - 1, __fmt,
179 __va_arg_pack ());
180}
181
182__fortify_function int
183__NTH (__asprintf (char **__restrict __ptr, const char *__restrict __fmt,
184 ...))
185{
186 return __asprintf_chk (__ptr, __USE_FORTIFY_LEVEL - 1, __fmt,
187 __va_arg_pack ());
188}
189
190__fortify_function int
191__NTH (obstack_printf (struct obstack *__restrict __obstack,
192 const char *__restrict __fmt, ...))
193{
194 return __obstack_printf_chk (__obstack, __USE_FORTIFY_LEVEL - 1, __fmt,
195 __va_arg_pack ());
196}
197# elif !defined __cplusplus
198# define asprintf(ptr, ...) \
199 __asprintf_chk (ptr, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
200# define __asprintf(ptr, ...) \
201 __asprintf_chk (ptr, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
202# define obstack_printf(obstack, ...) \
203 __obstack_printf_chk (obstack, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
204# endif
205
206__fortify_function int
207__NTH (vasprintf (char **__restrict __ptr, const char *__restrict __fmt,
208 _G_va_list __ap))
209{
210 return __vasprintf_chk (__ptr, __USE_FORTIFY_LEVEL - 1, __fmt, __ap);
211}
212
213__fortify_function int
214__NTH (obstack_vprintf (struct obstack *__restrict __obstack,
215 const char *__restrict __fmt, _G_va_list __ap))
216{
217 return __obstack_vprintf_chk (__obstack, __USE_FORTIFY_LEVEL - 1, __fmt,
218 __ap);
219}
220
221# endif
222
223#endif
224
225#if __GLIBC_USE (DEPRECATED_GETS)
226extern char *__gets_chk (char *__str, size_t) __wur;
227extern char *__REDIRECT (__gets_warn, (char *__str), gets)
228 __wur __warnattr ("please use fgets or getline instead, gets can't "
229 "specify buffer size");
230
231__fortify_function __wur char *
232gets (char *__str)
233{
234 if (__bos (__str) != (size_t) -1)
235 return __gets_chk (__str, __bos (__str));
236 return __gets_warn (__str);
237}
238#endif
239
240extern char *__fgets_chk (char *__restrict __s, size_t __size, int __n,
241 FILE *__restrict __stream) __wur;
242extern char *__REDIRECT (__fgets_alias,
243 (char *__restrict __s, int __n,
244 FILE *__restrict __stream), fgets) __wur;
245extern char *__REDIRECT (__fgets_chk_warn,
246 (char *__restrict __s, size_t __size, int __n,
247 FILE *__restrict __stream), __fgets_chk)
248 __wur __warnattr ("fgets called with bigger size than length "
249 "of destination buffer");
250
251__fortify_function __wur char *
252fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
253{
254 if (__bos (__s) != (size_t) -1)
255 {
256 if (!__builtin_constant_p (__n) || __n <= 0)
257 return __fgets_chk (__s, __bos (__s), __n, __stream);
258
259 if ((size_t) __n > __bos (__s))
260 return __fgets_chk_warn (__s, __bos (__s), __n, __stream);
261 }
262 return __fgets_alias (__s, __n, __stream);
263}
264
265extern size_t __fread_chk (void *__restrict __ptr, size_t __ptrlen,
266 size_t __size, size_t __n,
267 FILE *__restrict __stream) __wur;
268extern size_t __REDIRECT (__fread_alias,
269 (void *__restrict __ptr, size_t __size,
270 size_t __n, FILE *__restrict __stream),
271 fread) __wur;
272extern size_t __REDIRECT (__fread_chk_warn,
273 (void *__restrict __ptr, size_t __ptrlen,
274 size_t __size, size_t __n,
275 FILE *__restrict __stream),
276 __fread_chk)
277 __wur __warnattr ("fread called with bigger size * nmemb than length "
278 "of destination buffer");
279
280__fortify_function __wur size_t
281fread (void *__restrict __ptr, size_t __size, size_t __n,
282 FILE *__restrict __stream)
283{
284 if (__bos0 (__ptr) != (size_t) -1)
285 {
286 if (!__builtin_constant_p (__size)
287 || !__builtin_constant_p (__n)
288 || (__size | __n) >= (((size_t) 1) << (8 * sizeof (size_t) / 2)))
289 return __fread_chk (__ptr, __bos0 (__ptr), __size, __n, __stream);
290
291 if (__size * __n > __bos0 (__ptr))
292 return __fread_chk_warn (__ptr, __bos0 (__ptr), __size, __n, __stream);
293 }
294 return __fread_alias (__ptr, __size, __n, __stream);
295}
296
297#ifdef __USE_GNU
298extern char *__fgets_unlocked_chk (char *__restrict __s, size_t __size,
299 int __n, FILE *__restrict __stream) __wur;
300extern char *__REDIRECT (__fgets_unlocked_alias,
301 (char *__restrict __s, int __n,
302 FILE *__restrict __stream), fgets_unlocked) __wur;
303extern char *__REDIRECT (__fgets_unlocked_chk_warn,
304 (char *__restrict __s, size_t __size, int __n,
305 FILE *__restrict __stream), __fgets_unlocked_chk)
306 __wur __warnattr ("fgets_unlocked called with bigger size than length "
307 "of destination buffer");
308
309__fortify_function __wur char *
310fgets_unlocked (char *__restrict __s, int __n, FILE *__restrict __stream)
311{
312 if (__bos (__s) != (size_t) -1)
313 {
314 if (!__builtin_constant_p (__n) || __n <= 0)
315 return __fgets_unlocked_chk (__s, __bos (__s), __n, __stream);
316
317 if ((size_t) __n > __bos (__s))
318 return __fgets_unlocked_chk_warn (__s, __bos (__s), __n, __stream);
319 }
320 return __fgets_unlocked_alias (__s, __n, __stream);
321}
322#endif
323
324#ifdef __USE_MISC
325# undef fread_unlocked
326extern size_t __fread_unlocked_chk (void *__restrict __ptr, size_t __ptrlen,
327 size_t __size, size_t __n,
328 FILE *__restrict __stream) __wur;
329extern size_t __REDIRECT (__fread_unlocked_alias,
330 (void *__restrict __ptr, size_t __size,
331 size_t __n, FILE *__restrict __stream),
332 fread_unlocked) __wur;
333extern size_t __REDIRECT (__fread_unlocked_chk_warn,
334 (void *__restrict __ptr, size_t __ptrlen,
335 size_t __size, size_t __n,
336 FILE *__restrict __stream),
337 __fread_unlocked_chk)
338 __wur __warnattr ("fread_unlocked called with bigger size * nmemb than "
339 "length of destination buffer");
340
341__fortify_function __wur size_t
342fread_unlocked (void *__restrict __ptr, size_t __size, size_t __n,
343 FILE *__restrict __stream)
344{
345 if (__bos0 (__ptr) != (size_t) -1)
346 {
347 if (!__builtin_constant_p (__size)
348 || !__builtin_constant_p (__n)
349 || (__size | __n) >= (((size_t) 1) << (8 * sizeof (size_t) / 2)))
350 return __fread_unlocked_chk (__ptr, __bos0 (__ptr), __size, __n,
351 __stream);
352
353 if (__size * __n > __bos0 (__ptr))
354 return __fread_unlocked_chk_warn (__ptr, __bos0 (__ptr), __size, __n,
355 __stream);
356 }
357
358# ifdef __USE_EXTERN_INLINES
359 if (__builtin_constant_p (__size)
360 && __builtin_constant_p (__n)
361 && (__size | __n) < (((size_t) 1) << (8 * sizeof (size_t) / 2))
362 && __size * __n <= 8)
363 {
364 size_t __cnt = __size * __n;
365 char *__cptr = (char *) __ptr;
366 if (__cnt == 0)
367 return 0;
368
369 for (; __cnt > 0; --__cnt)
370 {
371 int __c = _IO_getc_unlocked (__stream);
372 if (__c == EOF)
373 break;
374 *__cptr++ = __c;
375 }
376 return (__cptr - (char *) __ptr) / __size;
377 }
378# endif
379 return __fread_unlocked_alias (__ptr, __size, __n, __stream);
380}
381#endif
382