1 /* Checking macros for unistd functions.
2 Copyright (C) 2005-2020 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 (__bos0 (__buf) != (size_t) -1)
40 if (!__builtin_constant_p (__nbytes))
41 return __read_chk (__fd, __buf, __nbytes, __bos0 (__buf));
43 if (__nbytes > __bos0 (__buf))
44 return __read_chk_warn (__fd, __buf, __nbytes, __bos0 (__buf));
46 return __read_alias (__fd, __buf, __nbytes);
50 extern ssize_t __pread_chk (int __fd, void *__buf, size_t __nbytes,
51 __off_t __offset, size_t __bufsize)
52 __wur __attr_access ((__write_only__, 2, 3));
53 extern ssize_t __pread64_chk (int __fd, void *__buf, size_t __nbytes,
54 __off64_t __offset, size_t __bufsize)
55 __wur __attr_access ((__write_only__, 2, 3));
56 extern ssize_t __REDIRECT (__pread_alias,
57 (int __fd, void *__buf, size_t __nbytes,
58 __off_t __offset), pread)
59 __wur __attr_access ((__write_only__, 2, 3));
60 extern ssize_t __REDIRECT (__pread64_alias,
61 (int __fd, void *__buf, size_t __nbytes,
62 __off64_t __offset), pread64)
63 __wur __attr_access ((__write_only__, 2, 3));
64 extern ssize_t __REDIRECT (__pread_chk_warn,
65 (int __fd, void *__buf, size_t __nbytes,
66 __off_t __offset, size_t __bufsize), __pread_chk)
67 __wur __warnattr ("pread called with bigger length than size of "
68 "the destination buffer");
69 extern ssize_t __REDIRECT (__pread64_chk_warn,
70 (int __fd, void *__buf, size_t __nbytes,
71 __off64_t __offset, size_t __bufsize),
73 __wur __warnattr ("pread64 called with bigger length than size of "
74 "the destination buffer");
76 # ifndef __USE_FILE_OFFSET64
77 __fortify_function __wur ssize_t
78 pread (int __fd, void *__buf, size_t __nbytes, __off_t __offset)
80 if (__bos0 (__buf) != (size_t) -1)
82 if (!__builtin_constant_p (__nbytes))
83 return __pread_chk (__fd, __buf, __nbytes, __offset, __bos0 (__buf));
85 if ( __nbytes > __bos0 (__buf))
86 return __pread_chk_warn (__fd, __buf, __nbytes, __offset,
89 return __pread_alias (__fd, __buf, __nbytes, __offset);
92 __fortify_function __wur ssize_t
93 pread (int __fd, void *__buf, size_t __nbytes, __off64_t __offset)
95 if (__bos0 (__buf) != (size_t) -1)
97 if (!__builtin_constant_p (__nbytes))
98 return __pread64_chk (__fd, __buf, __nbytes, __offset, __bos0 (__buf));
100 if ( __nbytes > __bos0 (__buf))
101 return __pread64_chk_warn (__fd, __buf, __nbytes, __offset,
105 return __pread64_alias (__fd, __buf, __nbytes, __offset);
109 # ifdef __USE_LARGEFILE64
110 __fortify_function __wur ssize_t
111 pread64 (int __fd, void *__buf, size_t __nbytes, __off64_t __offset)
113 if (__bos0 (__buf) != (size_t) -1)
115 if (!__builtin_constant_p (__nbytes))
116 return __pread64_chk (__fd, __buf, __nbytes, __offset, __bos0 (__buf));
118 if ( __nbytes > __bos0 (__buf))
119 return __pread64_chk_warn (__fd, __buf, __nbytes, __offset,
123 return __pread64_alias (__fd, __buf, __nbytes, __offset);
128 #if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K
129 extern ssize_t __readlink_chk (const char *__restrict __path,
130 char *__restrict __buf, size_t __len,
132 __THROW __nonnull ((1, 2)) __wur __attr_access ((__write_only__, 2, 3));
133 extern ssize_t __REDIRECT_NTH (__readlink_alias,
134 (const char *__restrict __path,
135 char *__restrict __buf, size_t __len), readlink)
136 __nonnull ((1, 2)) __wur __attr_access ((__write_only__, 2, 3));
137 extern ssize_t __REDIRECT_NTH (__readlink_chk_warn,
138 (const char *__restrict __path,
139 char *__restrict __buf, size_t __len,
140 size_t __buflen), __readlink_chk)
141 __nonnull ((1, 2)) __wur __warnattr ("readlink called with bigger length "
142 "than size of destination buffer");
144 __fortify_function __nonnull ((1, 2)) __wur ssize_t
145 __NTH (readlink (const char *__restrict __path, char *__restrict __buf,
148 if (__bos (__buf) != (size_t) -1)
150 if (!__builtin_constant_p (__len))
151 return __readlink_chk (__path, __buf, __len, __bos (__buf));
153 if ( __len > __bos (__buf))
154 return __readlink_chk_warn (__path, __buf, __len, __bos (__buf));
156 return __readlink_alias (__path, __buf, __len);
161 extern ssize_t __readlinkat_chk (int __fd, const char *__restrict __path,
162 char *__restrict __buf, size_t __len,
164 __THROW __nonnull ((2, 3)) __wur __attr_access ((__write_only__, 3, 4));
165 extern ssize_t __REDIRECT_NTH (__readlinkat_alias,
166 (int __fd, const char *__restrict __path,
167 char *__restrict __buf, size_t __len),
169 __nonnull ((2, 3)) __wur __attr_access ((__write_only__, 3, 4));
170 extern ssize_t __REDIRECT_NTH (__readlinkat_chk_warn,
171 (int __fd, const char *__restrict __path,
172 char *__restrict __buf, size_t __len,
173 size_t __buflen), __readlinkat_chk)
174 __nonnull ((2, 3)) __wur __warnattr ("readlinkat called with bigger "
175 "length than size of destination "
178 __fortify_function __nonnull ((2, 3)) __wur ssize_t
179 __NTH (readlinkat (int __fd, const char *__restrict __path,
180 char *__restrict __buf, size_t __len))
182 if (__bos (__buf) != (size_t) -1)
184 if (!__builtin_constant_p (__len))
185 return __readlinkat_chk (__fd, __path, __buf, __len, __bos (__buf));
187 if (__len > __bos (__buf))
188 return __readlinkat_chk_warn (__fd, __path, __buf, __len,
191 return __readlinkat_alias (__fd, __path, __buf, __len);
195 extern char *__getcwd_chk (char *__buf, size_t __size, size_t __buflen)
196 __THROW __wur __attr_access ((__write_only__, 1, 2));
197 extern char *__REDIRECT_NTH (__getcwd_alias,
198 (char *__buf, size_t __size), getcwd)
199 __wur __attr_access ((__write_only__, 1, 2));
200 extern char *__REDIRECT_NTH (__getcwd_chk_warn,
201 (char *__buf, size_t __size, size_t __buflen),
203 __wur __warnattr ("getcwd caller with bigger length than size of "
204 "destination buffer");
206 __fortify_function __wur char *
207 __NTH (getcwd (char *__buf, size_t __size))
209 if (__bos (__buf) != (size_t) -1)
211 if (!__builtin_constant_p (__size))
212 return __getcwd_chk (__buf, __size, __bos (__buf));
214 if (__size > __bos (__buf))
215 return __getcwd_chk_warn (__buf, __size, __bos (__buf));
217 return __getcwd_alias (__buf, __size);
220 #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
221 extern char *__getwd_chk (char *__buf, size_t buflen)
222 __THROW __nonnull ((1)) __wur __attr_access ((__write_only__, 1, 2));
223 extern char *__REDIRECT_NTH (__getwd_warn, (char *__buf), getwd)
224 __nonnull ((1)) __wur __warnattr ("please use getcwd instead, as getwd "
225 "doesn't specify buffer size");
227 __fortify_function __nonnull ((1)) __attribute_deprecated__ __wur char *
228 __NTH (getwd (char *__buf))
230 if (__bos (__buf) != (size_t) -1)
231 return __getwd_chk (__buf, __bos (__buf));
232 return __getwd_warn (__buf);
236 extern size_t __confstr_chk (int __name, char *__buf, size_t __len,
237 size_t __buflen) __THROW
238 __attr_access ((__write_only__, 2, 3));
239 extern size_t __REDIRECT_NTH (__confstr_alias, (int __name, char *__buf,
240 size_t __len), confstr)
241 __attr_access ((__write_only__, 2, 3));
242 extern size_t __REDIRECT_NTH (__confstr_chk_warn,
243 (int __name, char *__buf, size_t __len,
244 size_t __buflen), __confstr_chk)
245 __warnattr ("confstr called with bigger length than size of destination "
248 __fortify_function size_t
249 __NTH (confstr (int __name, char *__buf, size_t __len))
251 if (__bos (__buf) != (size_t) -1)
253 if (!__builtin_constant_p (__len))
254 return __confstr_chk (__name, __buf, __len, __bos (__buf));
256 if (__bos (__buf) < __len)
257 return __confstr_chk_warn (__name, __buf, __len, __bos (__buf));
259 return __confstr_alias (__name, __buf, __len);
263 extern int __getgroups_chk (int __size, __gid_t __list[], size_t __listlen)
264 __THROW __wur __attr_access ((__write_only__, 2, 1));
265 extern int __REDIRECT_NTH (__getgroups_alias, (int __size, __gid_t __list[]),
266 getgroups) __wur __attr_access ((__write_only__, 2, 1));
267 extern int __REDIRECT_NTH (__getgroups_chk_warn,
268 (int __size, __gid_t __list[], size_t __listlen),
270 __wur __warnattr ("getgroups called with bigger group count than what "
271 "can fit into destination buffer");
273 __fortify_function int
274 __NTH (getgroups (int __size, __gid_t __list[]))
276 if (__bos (__list) != (size_t) -1)
278 if (!__builtin_constant_p (__size) || __size < 0)
279 return __getgroups_chk (__size, __list, __bos (__list));
281 if (__size * sizeof (__gid_t) > __bos (__list))
282 return __getgroups_chk_warn (__size, __list, __bos (__list));
284 return __getgroups_alias (__size, __list);
288 extern int __ttyname_r_chk (int __fd, char *__buf, size_t __buflen,
289 size_t __nreal) __THROW __nonnull ((2))
290 __attr_access ((__write_only__, 2, 3));
291 extern int __REDIRECT_NTH (__ttyname_r_alias, (int __fd, char *__buf,
292 size_t __buflen), ttyname_r)
294 extern int __REDIRECT_NTH (__ttyname_r_chk_warn,
295 (int __fd, char *__buf, size_t __buflen,
296 size_t __nreal), __ttyname_r_chk)
297 __nonnull ((2)) __warnattr ("ttyname_r called with bigger buflen than "
298 "size of destination buffer");
300 __fortify_function int
301 __NTH (ttyname_r (int __fd, char *__buf, size_t __buflen))
303 if (__bos (__buf) != (size_t) -1)
305 if (!__builtin_constant_p (__buflen))
306 return __ttyname_r_chk (__fd, __buf, __buflen, __bos (__buf));
308 if (__buflen > __bos (__buf))
309 return __ttyname_r_chk_warn (__fd, __buf, __buflen, __bos (__buf));
311 return __ttyname_r_alias (__fd, __buf, __buflen);
315 #ifdef __USE_POSIX199506
316 extern int __getlogin_r_chk (char *__buf, size_t __buflen, size_t __nreal)
317 __nonnull ((1)) __attr_access ((__write_only__, 1, 2));
318 extern int __REDIRECT (__getlogin_r_alias, (char *__buf, size_t __buflen),
319 getlogin_r) __nonnull ((1));
320 extern int __REDIRECT (__getlogin_r_chk_warn,
321 (char *__buf, size_t __buflen, size_t __nreal),
323 __nonnull ((1)) __warnattr ("getlogin_r called with bigger buflen than "
324 "size of destination buffer");
326 __fortify_function int
327 getlogin_r (char *__buf, size_t __buflen)
329 if (__bos (__buf) != (size_t) -1)
331 if (!__builtin_constant_p (__buflen))
332 return __getlogin_r_chk (__buf, __buflen, __bos (__buf));
334 if (__buflen > __bos (__buf))
335 return __getlogin_r_chk_warn (__buf, __buflen, __bos (__buf));
337 return __getlogin_r_alias (__buf, __buflen);
342 #if defined __USE_MISC || defined __USE_UNIX98
343 extern int __gethostname_chk (char *__buf, size_t __buflen, size_t __nreal)
344 __THROW __nonnull ((1)) __attr_access ((__write_only__, 1, 2));
345 extern int __REDIRECT_NTH (__gethostname_alias, (char *__buf, size_t __buflen),
347 __nonnull ((1)) __attr_access ((__write_only__, 1, 2));
348 extern int __REDIRECT_NTH (__gethostname_chk_warn,
349 (char *__buf, size_t __buflen, size_t __nreal),
351 __nonnull ((1)) __warnattr ("gethostname called with bigger buflen than "
352 "size of destination buffer");
354 __fortify_function int
355 __NTH (gethostname (char *__buf, size_t __buflen))
357 if (__bos (__buf) != (size_t) -1)
359 if (!__builtin_constant_p (__buflen))
360 return __gethostname_chk (__buf, __buflen, __bos (__buf));
362 if (__buflen > __bos (__buf))
363 return __gethostname_chk_warn (__buf, __buflen, __bos (__buf));
365 return __gethostname_alias (__buf, __buflen);
370 #if defined __USE_MISC || (defined __USE_XOPEN && !defined __USE_UNIX98)
371 extern int __getdomainname_chk (char *__buf, size_t __buflen, size_t __nreal)
372 __THROW __nonnull ((1)) __wur __attr_access ((__write_only__, 1, 2));
373 extern int __REDIRECT_NTH (__getdomainname_alias, (char *__buf,
375 getdomainname) __nonnull ((1))
376 __wur __attr_access ((__write_only__, 1, 2));
377 extern int __REDIRECT_NTH (__getdomainname_chk_warn,
378 (char *__buf, size_t __buflen, size_t __nreal),
380 __nonnull ((1)) __wur __warnattr ("getdomainname called with bigger "
381 "buflen than size of destination "
384 __fortify_function int
385 __NTH (getdomainname (char *__buf, size_t __buflen))
387 if (__bos (__buf) != (size_t) -1)
389 if (!__builtin_constant_p (__buflen))
390 return __getdomainname_chk (__buf, __buflen, __bos (__buf));
392 if (__buflen > __bos (__buf))
393 return __getdomainname_chk_warn (__buf, __buflen, __bos (__buf));
395 return __getdomainname_alias (__buf, __buflen);