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