1 /* Checking macros for unistd functions.
2 Copyright (C) 2005-2021 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/>. */
20 # error "Never include <bits/unistd.h> directly; use <unistd.h> instead."
23 extern ssize_t
__read_chk (int __fd
, void *__buf
, size_t __nbytes
,
25 __wur
__attr_access ((__write_only__
, 2, 3));
26 extern ssize_t
__REDIRECT (__read_alias
, (int __fd
, void *__buf
,
27 size_t __nbytes
), read
)
28 __wur
__attr_access ((__write_only__
, 2, 3));
29 extern ssize_t
__REDIRECT (__read_chk_warn
,
30 (int __fd
, void *__buf
, size_t __nbytes
,
31 size_t __buflen
), __read_chk
)
32 __wur
__warnattr ("read called with bigger length than size of "
33 "the destination buffer");
35 __fortify_function __wur ssize_t
36 read (int __fd
, void *__buf
, size_t __nbytes
)
38 if (__glibc_objsize0 (__buf
) != (size_t) -1)
40 if (!__builtin_constant_p (__nbytes
))
41 return __read_chk (__fd
, __buf
, __nbytes
, __glibc_objsize0 (__buf
));
43 if (__nbytes
> __glibc_objsize0 (__buf
))
44 return __read_chk_warn (__fd
, __buf
, __nbytes
,
45 __glibc_objsize0 (__buf
));
47 return __read_alias (__fd
, __buf
, __nbytes
);
51 extern ssize_t
__pread_chk (int __fd
, void *__buf
, size_t __nbytes
,
52 __off_t __offset
, size_t __bufsize
)
53 __wur
__attr_access ((__write_only__
, 2, 3));
54 extern ssize_t
__pread64_chk (int __fd
, void *__buf
, size_t __nbytes
,
55 __off64_t __offset
, size_t __bufsize
)
56 __wur
__attr_access ((__write_only__
, 2, 3));
57 extern ssize_t
__REDIRECT (__pread_alias
,
58 (int __fd
, void *__buf
, size_t __nbytes
,
59 __off_t __offset
), pread
)
60 __wur
__attr_access ((__write_only__
, 2, 3));
61 extern ssize_t
__REDIRECT (__pread64_alias
,
62 (int __fd
, void *__buf
, size_t __nbytes
,
63 __off64_t __offset
), pread64
)
64 __wur
__attr_access ((__write_only__
, 2, 3));
65 extern ssize_t
__REDIRECT (__pread_chk_warn
,
66 (int __fd
, void *__buf
, size_t __nbytes
,
67 __off_t __offset
, size_t __bufsize
), __pread_chk
)
68 __wur
__warnattr ("pread called with bigger length than size of "
69 "the destination buffer");
70 extern ssize_t
__REDIRECT (__pread64_chk_warn
,
71 (int __fd
, void *__buf
, size_t __nbytes
,
72 __off64_t __offset
, size_t __bufsize
),
74 __wur
__warnattr ("pread64 called with bigger length than size of "
75 "the destination buffer");
77 # ifndef __USE_FILE_OFFSET64
78 __fortify_function __wur ssize_t
79 pread (int __fd
, void *__buf
, size_t __nbytes
, __off_t __offset
)
81 if (__glibc_objsize0 (__buf
) != (size_t) -1)
83 if (!__builtin_constant_p (__nbytes
))
84 return __pread_chk (__fd
, __buf
, __nbytes
, __offset
,
85 __glibc_objsize0 (__buf
));
87 if ( __nbytes
> __glibc_objsize0 (__buf
))
88 return __pread_chk_warn (__fd
, __buf
, __nbytes
, __offset
,
89 __glibc_objsize0 (__buf
));
91 return __pread_alias (__fd
, __buf
, __nbytes
, __offset
);
94 __fortify_function __wur ssize_t
95 pread (int __fd
, void *__buf
, size_t __nbytes
, __off64_t __offset
)
97 if (__glibc_objsize0 (__buf
) != (size_t) -1)
99 if (!__builtin_constant_p (__nbytes
))
100 return __pread64_chk (__fd
, __buf
, __nbytes
, __offset
,
101 __glibc_objsize0 (__buf
));
103 if ( __nbytes
> __glibc_objsize0 (__buf
))
104 return __pread64_chk_warn (__fd
, __buf
, __nbytes
, __offset
,
105 __glibc_objsize0 (__buf
));
108 return __pread64_alias (__fd
, __buf
, __nbytes
, __offset
);
112 # ifdef __USE_LARGEFILE64
113 __fortify_function __wur ssize_t
114 pread64 (int __fd
, void *__buf
, size_t __nbytes
, __off64_t __offset
)
116 if (__glibc_objsize0 (__buf
) != (size_t) -1)
118 if (!__builtin_constant_p (__nbytes
))
119 return __pread64_chk (__fd
, __buf
, __nbytes
, __offset
,
120 __glibc_objsize0 (__buf
));
122 if ( __nbytes
> __glibc_objsize0 (__buf
))
123 return __pread64_chk_warn (__fd
, __buf
, __nbytes
, __offset
,
124 __glibc_objsize0 (__buf
));
127 return __pread64_alias (__fd
, __buf
, __nbytes
, __offset
);
132 #if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K
133 extern ssize_t
__readlink_chk (const char *__restrict __path
,
134 char *__restrict __buf
, size_t __len
,
136 __THROW
__nonnull ((1, 2)) __wur
__attr_access ((__write_only__
, 2, 3));
137 extern ssize_t
__REDIRECT_NTH (__readlink_alias
,
138 (const char *__restrict __path
,
139 char *__restrict __buf
, size_t __len
), readlink
)
140 __nonnull ((1, 2)) __wur
__attr_access ((__write_only__
, 2, 3));
141 extern ssize_t
__REDIRECT_NTH (__readlink_chk_warn
,
142 (const char *__restrict __path
,
143 char *__restrict __buf
, size_t __len
,
144 size_t __buflen
), __readlink_chk
)
145 __nonnull ((1, 2)) __wur
__warnattr ("readlink called with bigger length "
146 "than size of destination buffer");
148 __fortify_function
__nonnull ((1, 2)) __wur ssize_t
149 __NTH (readlink (const char *__restrict __path
, char *__restrict __buf
,
152 if (__glibc_objsize (__buf
) != (size_t) -1)
154 if (!__builtin_constant_p (__len
))
155 return __readlink_chk (__path
, __buf
, __len
, __glibc_objsize (__buf
));
157 if ( __len
> __glibc_objsize (__buf
))
158 return __readlink_chk_warn (__path
, __buf
, __len
,
159 __glibc_objsize (__buf
));
161 return __readlink_alias (__path
, __buf
, __len
);
166 extern ssize_t
__readlinkat_chk (int __fd
, const char *__restrict __path
,
167 char *__restrict __buf
, size_t __len
,
169 __THROW
__nonnull ((2, 3)) __wur
__attr_access ((__write_only__
, 3, 4));
170 extern ssize_t
__REDIRECT_NTH (__readlinkat_alias
,
171 (int __fd
, const char *__restrict __path
,
172 char *__restrict __buf
, size_t __len
),
174 __nonnull ((2, 3)) __wur
__attr_access ((__write_only__
, 3, 4));
175 extern ssize_t
__REDIRECT_NTH (__readlinkat_chk_warn
,
176 (int __fd
, const char *__restrict __path
,
177 char *__restrict __buf
, size_t __len
,
178 size_t __buflen
), __readlinkat_chk
)
179 __nonnull ((2, 3)) __wur
__warnattr ("readlinkat called with bigger "
180 "length than size of destination "
183 __fortify_function
__nonnull ((2, 3)) __wur ssize_t
184 __NTH (readlinkat (int __fd
, const char *__restrict __path
,
185 char *__restrict __buf
, size_t __len
))
187 if (__glibc_objsize (__buf
) != (size_t) -1)
189 if (!__builtin_constant_p (__len
))
190 return __readlinkat_chk (__fd
, __path
, __buf
, __len
,
191 __glibc_objsize (__buf
));
193 if (__len
> __glibc_objsize (__buf
))
194 return __readlinkat_chk_warn (__fd
, __path
, __buf
, __len
,
195 __glibc_objsize (__buf
));
197 return __readlinkat_alias (__fd
, __path
, __buf
, __len
);
201 extern char *__getcwd_chk (char *__buf
, size_t __size
, size_t __buflen
)
202 __THROW __wur
__attr_access ((__write_only__
, 1, 2));
203 extern char *__REDIRECT_NTH (__getcwd_alias
,
204 (char *__buf
, size_t __size
), getcwd
)
205 __wur
__attr_access ((__write_only__
, 1, 2));
206 extern char *__REDIRECT_NTH (__getcwd_chk_warn
,
207 (char *__buf
, size_t __size
, size_t __buflen
),
209 __wur
__warnattr ("getcwd caller with bigger length than size of "
210 "destination buffer");
212 __fortify_function __wur
char *
213 __NTH (getcwd (char *__buf
, size_t __size
))
215 if (__glibc_objsize (__buf
) != (size_t) -1)
217 if (!__builtin_constant_p (__size
))
218 return __getcwd_chk (__buf
, __size
, __glibc_objsize (__buf
));
220 if (__size
> __glibc_objsize (__buf
))
221 return __getcwd_chk_warn (__buf
, __size
, __glibc_objsize (__buf
));
223 return __getcwd_alias (__buf
, __size
);
226 #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
227 extern char *__getwd_chk (char *__buf
, size_t buflen
)
228 __THROW
__nonnull ((1)) __wur
__attr_access ((__write_only__
, 1, 2));
229 extern char *__REDIRECT_NTH (__getwd_warn
, (char *__buf
), getwd
)
230 __nonnull ((1)) __wur
__warnattr ("please use getcwd instead, as getwd "
231 "doesn't specify buffer size");
233 __fortify_function
__nonnull ((1)) __attribute_deprecated__ __wur
char *
234 __NTH (getwd (char *__buf
))
236 if (__glibc_objsize (__buf
) != (size_t) -1)
237 return __getwd_chk (__buf
, __glibc_objsize (__buf
));
238 return __getwd_warn (__buf
);
242 extern size_t __confstr_chk (int __name
, char *__buf
, size_t __len
,
243 size_t __buflen
) __THROW
244 __attr_access ((__write_only__
, 2, 3));
245 extern size_t __REDIRECT_NTH (__confstr_alias
, (int __name
, char *__buf
,
246 size_t __len
), confstr
)
247 __attr_access ((__write_only__
, 2, 3));
248 extern size_t __REDIRECT_NTH (__confstr_chk_warn
,
249 (int __name
, char *__buf
, size_t __len
,
250 size_t __buflen
), __confstr_chk
)
251 __warnattr ("confstr called with bigger length than size of destination "
254 __fortify_function
size_t
255 __NTH (confstr (int __name
, char *__buf
, size_t __len
))
257 if (__glibc_objsize (__buf
) != (size_t) -1)
259 if (!__builtin_constant_p (__len
))
260 return __confstr_chk (__name
, __buf
, __len
, __glibc_objsize (__buf
));
262 if (__glibc_objsize (__buf
) < __len
)
263 return __confstr_chk_warn (__name
, __buf
, __len
,
264 __glibc_objsize (__buf
));
266 return __confstr_alias (__name
, __buf
, __len
);
270 extern int __getgroups_chk (int __size
, __gid_t __list
[], size_t __listlen
)
271 __THROW __wur
__attr_access ((__write_only__
, 2, 1));
272 extern int __REDIRECT_NTH (__getgroups_alias
, (int __size
, __gid_t __list
[]),
273 getgroups
) __wur
__attr_access ((__write_only__
, 2, 1));
274 extern int __REDIRECT_NTH (__getgroups_chk_warn
,
275 (int __size
, __gid_t __list
[], size_t __listlen
),
277 __wur
__warnattr ("getgroups called with bigger group count than what "
278 "can fit into destination buffer");
280 __fortify_function
int
281 __NTH (getgroups (int __size
, __gid_t __list
[]))
283 if (__glibc_objsize (__list
) != (size_t) -1)
285 if (!__builtin_constant_p (__size
) || __size
< 0)
286 return __getgroups_chk (__size
, __list
, __glibc_objsize (__list
));
288 if (__size
* sizeof (__gid_t
) > __glibc_objsize (__list
))
289 return __getgroups_chk_warn (__size
, __list
, __glibc_objsize (__list
));
291 return __getgroups_alias (__size
, __list
);
295 extern int __ttyname_r_chk (int __fd
, char *__buf
, size_t __buflen
,
296 size_t __nreal
) __THROW
__nonnull ((2))
297 __attr_access ((__write_only__
, 2, 3));
298 extern int __REDIRECT_NTH (__ttyname_r_alias
, (int __fd
, char *__buf
,
299 size_t __buflen
), ttyname_r
)
301 extern int __REDIRECT_NTH (__ttyname_r_chk_warn
,
302 (int __fd
, char *__buf
, size_t __buflen
,
303 size_t __nreal
), __ttyname_r_chk
)
304 __nonnull ((2)) __warnattr ("ttyname_r called with bigger buflen than "
305 "size of destination buffer");
307 __fortify_function
int
308 __NTH (ttyname_r (int __fd
, char *__buf
, size_t __buflen
))
310 if (__glibc_objsize (__buf
) != (size_t) -1)
312 if (!__builtin_constant_p (__buflen
))
313 return __ttyname_r_chk (__fd
, __buf
, __buflen
,
314 __glibc_objsize (__buf
));
316 if (__buflen
> __glibc_objsize (__buf
))
317 return __ttyname_r_chk_warn (__fd
, __buf
, __buflen
,
318 __glibc_objsize (__buf
));
320 return __ttyname_r_alias (__fd
, __buf
, __buflen
);
324 #ifdef __USE_POSIX199506
325 extern int __getlogin_r_chk (char *__buf
, size_t __buflen
, size_t __nreal
)
326 __nonnull ((1)) __attr_access ((__write_only__
, 1, 2));
327 extern int __REDIRECT (__getlogin_r_alias
, (char *__buf
, size_t __buflen
),
328 getlogin_r
) __nonnull ((1));
329 extern int __REDIRECT (__getlogin_r_chk_warn
,
330 (char *__buf
, size_t __buflen
, size_t __nreal
),
332 __nonnull ((1)) __warnattr ("getlogin_r called with bigger buflen than "
333 "size of destination buffer");
335 __fortify_function
int
336 getlogin_r (char *__buf
, size_t __buflen
)
338 if (__glibc_objsize (__buf
) != (size_t) -1)
340 if (!__builtin_constant_p (__buflen
))
341 return __getlogin_r_chk (__buf
, __buflen
, __glibc_objsize (__buf
));
343 if (__buflen
> __glibc_objsize (__buf
))
344 return __getlogin_r_chk_warn (__buf
, __buflen
,
345 __glibc_objsize (__buf
));
347 return __getlogin_r_alias (__buf
, __buflen
);
352 #if defined __USE_MISC || defined __USE_UNIX98
353 extern int __gethostname_chk (char *__buf
, size_t __buflen
, size_t __nreal
)
354 __THROW
__nonnull ((1)) __attr_access ((__write_only__
, 1, 2));
355 extern int __REDIRECT_NTH (__gethostname_alias
, (char *__buf
, size_t __buflen
),
357 __nonnull ((1)) __attr_access ((__write_only__
, 1, 2));
358 extern int __REDIRECT_NTH (__gethostname_chk_warn
,
359 (char *__buf
, size_t __buflen
, size_t __nreal
),
361 __nonnull ((1)) __warnattr ("gethostname called with bigger buflen than "
362 "size of destination buffer");
364 __fortify_function
int
365 __NTH (gethostname (char *__buf
, size_t __buflen
))
367 if (__glibc_objsize (__buf
) != (size_t) -1)
369 if (!__builtin_constant_p (__buflen
))
370 return __gethostname_chk (__buf
, __buflen
, __glibc_objsize (__buf
));
372 if (__buflen
> __glibc_objsize (__buf
))
373 return __gethostname_chk_warn (__buf
, __buflen
,
374 __glibc_objsize (__buf
));
376 return __gethostname_alias (__buf
, __buflen
);
381 #if defined __USE_MISC || (defined __USE_XOPEN && !defined __USE_UNIX98)
382 extern int __getdomainname_chk (char *__buf
, size_t __buflen
, size_t __nreal
)
383 __THROW
__nonnull ((1)) __wur
__attr_access ((__write_only__
, 1, 2));
384 extern int __REDIRECT_NTH (__getdomainname_alias
, (char *__buf
,
386 getdomainname
) __nonnull ((1))
387 __wur
__attr_access ((__write_only__
, 1, 2));
388 extern int __REDIRECT_NTH (__getdomainname_chk_warn
,
389 (char *__buf
, size_t __buflen
, size_t __nreal
),
391 __nonnull ((1)) __wur
__warnattr ("getdomainname called with bigger "
392 "buflen than size of destination "
395 __fortify_function
int
396 __NTH (getdomainname (char *__buf
, size_t __buflen
))
398 if (__glibc_objsize (__buf
) != (size_t) -1)
400 if (!__builtin_constant_p (__buflen
))
401 return __getdomainname_chk (__buf
, __buflen
, __glibc_objsize (__buf
));
403 if (__buflen
> __glibc_objsize (__buf
))
404 return __getdomainname_chk_warn (__buf
, __buflen
,
405 __glibc_objsize (__buf
));
407 return __getdomainname_alias (__buf
, __buflen
);