1 /* Checking macros for stdio functions.
2 Copyright (C) 2004-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #ifndef _BITS_STDIO2_H
20 #define _BITS_STDIO2_H 1
23 # error "Never include <bits/stdio2.h> directly; use <stdio.h> instead."
27 __fortify_function
int
28 __NTH (sprintf (char *__restrict __s
, const char *__restrict __fmt
, ...))
30 return __builtin___sprintf_chk (__s
, __USE_FORTIFY_LEVEL
- 1,
31 __glibc_objsize (__s
), __fmt
,
34 #elif !defined __cplusplus
35 # define sprintf(str, ...) \
36 __builtin___sprintf_chk (str, __USE_FORTIFY_LEVEL - 1, \
37 __glibc_objsize (str), __VA_ARGS__)
40 __fortify_function
int
41 __NTH (vsprintf (char *__restrict __s
, const char *__restrict __fmt
,
44 return __builtin___vsprintf_chk (__s
, __USE_FORTIFY_LEVEL
- 1,
45 __glibc_objsize (__s
), __fmt
, __ap
);
48 #if defined __USE_ISOC99 || defined __USE_UNIX98
50 __fortify_function
int
51 __NTH (snprintf (char *__restrict __s
, size_t __n
,
52 const char *__restrict __fmt
, ...))
54 return __builtin___snprintf_chk (__s
, __n
, __USE_FORTIFY_LEVEL
- 1,
55 __glibc_objsize (__s
), __fmt
,
58 # elif !defined __cplusplus
59 # define snprintf(str, len, ...) \
60 __builtin___snprintf_chk (str, len, __USE_FORTIFY_LEVEL - 1, \
61 __glibc_objsize (str), __VA_ARGS__)
64 __fortify_function
int
65 __NTH (vsnprintf (char *__restrict __s
, size_t __n
,
66 const char *__restrict __fmt
, __gnuc_va_list __ap
))
68 return __builtin___vsnprintf_chk (__s
, __n
, __USE_FORTIFY_LEVEL
- 1,
69 __glibc_objsize (__s
), __fmt
, __ap
);
74 #if __USE_FORTIFY_LEVEL > 1
76 __fortify_function
__nonnull ((1)) int
77 fprintf (FILE *__restrict __stream
, const char *__restrict __fmt
, ...)
79 return __fprintf_chk (__stream
, __USE_FORTIFY_LEVEL
- 1, __fmt
,
83 __fortify_function
int
84 printf (const char *__restrict __fmt
, ...)
86 return __printf_chk (__USE_FORTIFY_LEVEL
- 1, __fmt
, __va_arg_pack ());
88 # elif !defined __cplusplus
89 # define printf(...) \
90 __printf_chk (__USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
91 # define fprintf(stream, ...) \
92 __fprintf_chk (stream, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
95 __fortify_function
int
96 vprintf (const char *__restrict __fmt
, __gnuc_va_list __ap
)
98 #ifdef __USE_EXTERN_INLINES
99 return __vfprintf_chk (stdout
, __USE_FORTIFY_LEVEL
- 1, __fmt
, __ap
);
101 return __vprintf_chk (__USE_FORTIFY_LEVEL
- 1, __fmt
, __ap
);
105 __fortify_function
__nonnull ((1)) int
106 vfprintf (FILE *__restrict __stream
,
107 const char *__restrict __fmt
, __gnuc_va_list __ap
)
109 return __vfprintf_chk (__stream
, __USE_FORTIFY_LEVEL
- 1, __fmt
, __ap
);
112 # ifdef __USE_XOPEN2K8
113 # ifdef __va_arg_pack
114 __fortify_function
int
115 dprintf (int __fd
, const char *__restrict __fmt
, ...)
117 return __dprintf_chk (__fd
, __USE_FORTIFY_LEVEL
- 1, __fmt
,
120 # elif !defined __cplusplus
121 # define dprintf(fd, ...) \
122 __dprintf_chk (fd, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
125 __fortify_function
int
126 vdprintf (int __fd
, const char *__restrict __fmt
, __gnuc_va_list __ap
)
128 return __vdprintf_chk (__fd
, __USE_FORTIFY_LEVEL
- 1, __fmt
, __ap
);
133 # ifdef __va_arg_pack
134 __fortify_function
int
135 __NTH (asprintf (char **__restrict __ptr
, const char *__restrict __fmt
, ...))
137 return __asprintf_chk (__ptr
, __USE_FORTIFY_LEVEL
- 1, __fmt
,
141 __fortify_function
int
142 __NTH (__asprintf (char **__restrict __ptr
, const char *__restrict __fmt
,
145 return __asprintf_chk (__ptr
, __USE_FORTIFY_LEVEL
- 1, __fmt
,
149 __fortify_function
int
150 __NTH (obstack_printf (struct obstack
*__restrict __obstack
,
151 const char *__restrict __fmt
, ...))
153 return __obstack_printf_chk (__obstack
, __USE_FORTIFY_LEVEL
- 1, __fmt
,
156 # elif !defined __cplusplus
157 # define asprintf(ptr, ...) \
158 __asprintf_chk (ptr, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
159 # define __asprintf(ptr, ...) \
160 __asprintf_chk (ptr, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
161 # define obstack_printf(obstack, ...) \
162 __obstack_printf_chk (obstack, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
165 __fortify_function
int
166 __NTH (vasprintf (char **__restrict __ptr
, const char *__restrict __fmt
,
167 __gnuc_va_list __ap
))
169 return __vasprintf_chk (__ptr
, __USE_FORTIFY_LEVEL
- 1, __fmt
, __ap
);
172 __fortify_function
int
173 __NTH (obstack_vprintf (struct obstack
*__restrict __obstack
,
174 const char *__restrict __fmt
, __gnuc_va_list __ap
))
176 return __obstack_vprintf_chk (__obstack
, __USE_FORTIFY_LEVEL
- 1, __fmt
,
184 #if __GLIBC_USE (DEPRECATED_GETS)
185 __fortify_function __wur
char *
188 if (__glibc_objsize (__str
) != (size_t) -1)
189 return __gets_chk (__str
, __glibc_objsize (__str
));
190 return __gets_warn (__str
);
194 __fortify_function __wur
__fortified_attr_access (__write_only__
, 1, 2)
195 __nonnull ((3)) char *
196 fgets (char *__restrict __s
, int __n
, FILE *__restrict __stream
)
198 size_t sz
= __glibc_objsize (__s
);
199 if (__glibc_safe_or_unknown_len (__n
, sizeof (char), sz
))
200 return __fgets_alias (__s
, __n
, __stream
);
201 if (__glibc_unsafe_len (__n
, sizeof (char), sz
))
202 return __fgets_chk_warn (__s
, sz
, __n
, __stream
);
203 return __fgets_chk (__s
, sz
, __n
, __stream
);
206 __fortify_function __wur
__nonnull ((4)) size_t
207 fread (void *__restrict __ptr
, size_t __size
, size_t __n
,
208 FILE *__restrict __stream
)
210 size_t sz
= __glibc_objsize0 (__ptr
);
211 if (__glibc_safe_or_unknown_len (__n
, __size
, sz
))
212 return __fread_alias (__ptr
, __size
, __n
, __stream
);
213 if (__glibc_unsafe_len (__n
, __size
, sz
))
214 return __fread_chk_warn (__ptr
, sz
, __size
, __n
, __stream
);
215 return __fread_chk (__ptr
, sz
, __size
, __n
, __stream
);
219 __fortify_function __wur
__fortified_attr_access (__write_only__
, 1, 2)
220 __nonnull ((3)) char *
221 fgets_unlocked (char *__restrict __s
, int __n
, FILE *__restrict __stream
)
223 size_t sz
= __glibc_objsize (__s
);
224 if (__glibc_safe_or_unknown_len (__n
, sizeof (char), sz
))
225 return __fgets_unlocked_alias (__s
, __n
, __stream
);
226 if (__glibc_unsafe_len (__n
, sizeof (char), sz
))
227 return __fgets_unlocked_chk_warn (__s
, sz
, __n
, __stream
);
228 return __fgets_unlocked_chk (__s
, sz
, __n
, __stream
);
233 # undef fread_unlocked
234 __fortify_function __wur
__nonnull ((4)) size_t
235 fread_unlocked (void *__restrict __ptr
, size_t __size
, size_t __n
,
236 FILE *__restrict __stream
)
238 size_t sz
= __glibc_objsize0 (__ptr
);
239 if (__glibc_safe_or_unknown_len (__n
, __size
, sz
))
241 # ifdef __USE_EXTERN_INLINES
242 if (__builtin_constant_p (__size
)
243 && __builtin_constant_p (__n
)
244 && (__size
| __n
) < (((size_t) 1) << (8 * sizeof (size_t) / 2))
245 && __size
* __n
<= 8)
247 size_t __cnt
= __size
* __n
;
248 char *__cptr
= (char *) __ptr
;
252 for (; __cnt
> 0; --__cnt
)
254 int __c
= getc_unlocked (__stream
);
259 return (__cptr
- (char *) __ptr
) / __size
;
262 return __fread_unlocked_alias (__ptr
, __size
, __n
, __stream
);
264 if (__glibc_unsafe_len (__n
, __size
, sz
))
265 return __fread_unlocked_chk_warn (__ptr
, sz
, __size
, __n
, __stream
);
266 return __fread_unlocked_chk (__ptr
, sz
, __size
, __n
, __stream
);
271 #endif /* bits/stdio2.h. */