1/* Checking macros for unistd functions.
2 Copyright (C) 2005-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 _UNISTD_H
20# error "Never include <bits/unistd.h> directly; use <unistd.h> instead."
21#endif
22
23extern ssize_t __read_chk (int __fd, void *__buf, size_t __nbytes,
24 size_t __buflen) __wur;
25extern ssize_t __REDIRECT (__read_alias, (int __fd, void *__buf,
26 size_t __nbytes), read) __wur;
27extern ssize_t __REDIRECT (__read_chk_warn,
28 (int __fd, void *__buf, size_t __nbytes,
29 size_t __buflen), __read_chk)
30 __wur __warnattr ("read called with bigger length than size of "
31 "the destination buffer");
32
33__fortify_function __wur ssize_t
34read (int __fd, void *__buf, size_t __nbytes)
35{
36 if (__bos0 (__buf) != (size_t) -1)
37 {
38 if (!__builtin_constant_p (__nbytes))
39 return __read_chk (__fd, __buf, __nbytes, __bos0 (__buf));
40
41 if (__nbytes > __bos0 (__buf))
42 return __read_chk_warn (__fd, __buf, __nbytes, __bos0 (__buf));
43 }
44 return __read_alias (__fd, __buf, __nbytes);
45}
46
47#ifdef __USE_UNIX98
48extern ssize_t __pread_chk (int __fd, void *__buf, size_t __nbytes,
49 __off_t __offset, size_t __bufsize) __wur;
50extern ssize_t __pread64_chk (int __fd, void *__buf, size_t __nbytes,
51 __off64_t __offset, size_t __bufsize) __wur;
52extern ssize_t __REDIRECT (__pread_alias,
53 (int __fd, void *__buf, size_t __nbytes,
54 __off_t __offset), pread) __wur;
55extern ssize_t __REDIRECT (__pread64_alias,
56 (int __fd, void *__buf, size_t __nbytes,
57 __off64_t __offset), pread64) __wur;
58extern ssize_t __REDIRECT (__pread_chk_warn,
59 (int __fd, void *__buf, size_t __nbytes,
60 __off_t __offset, size_t __bufsize), __pread_chk)
61 __wur __warnattr ("pread called with bigger length than size of "
62 "the destination buffer");
63extern ssize_t __REDIRECT (__pread64_chk_warn,
64 (int __fd, void *__buf, size_t __nbytes,
65 __off64_t __offset, size_t __bufsize),
66 __pread64_chk)
67 __wur __warnattr ("pread64 called with bigger length than size of "
68 "the destination buffer");
69
70# ifndef __USE_FILE_OFFSET64
71__fortify_function __wur ssize_t
72pread (int __fd, void *__buf, size_t __nbytes, __off_t __offset)
73{
74 if (__bos0 (__buf) != (size_t) -1)
75 {
76 if (!__builtin_constant_p (__nbytes))
77 return __pread_chk (__fd, __buf, __nbytes, __offset, __bos0 (__buf));
78
79 if ( __nbytes > __bos0 (__buf))
80 return __pread_chk_warn (__fd, __buf, __nbytes, __offset,
81 __bos0 (__buf));
82 }
83 return __pread_alias (__fd, __buf, __nbytes, __offset);
84}
85# else
86__fortify_function __wur ssize_t
87pread (int __fd, void *__buf, size_t __nbytes, __off64_t __offset)
88{
89 if (__bos0 (__buf) != (size_t) -1)
90 {
91 if (!__builtin_constant_p (__nbytes))
92 return __pread64_chk (__fd, __buf, __nbytes, __offset, __bos0 (__buf));
93
94 if ( __nbytes > __bos0 (__buf))
95 return __pread64_chk_warn (__fd, __buf, __nbytes, __offset,
96 __bos0 (__buf));
97 }
98
99 return __pread64_alias (__fd, __buf, __nbytes, __offset);
100}
101# endif
102
103# ifdef __USE_LARGEFILE64
104__fortify_function __wur ssize_t
105pread64 (int __fd, void *__buf, size_t __nbytes, __off64_t __offset)
106{
107 if (__bos0 (__buf) != (size_t) -1)
108 {
109 if (!__builtin_constant_p (__nbytes))
110 return __pread64_chk (__fd, __buf, __nbytes, __offset, __bos0 (__buf));
111
112 if ( __nbytes > __bos0 (__buf))
113 return __pread64_chk_warn (__fd, __buf, __nbytes, __offset,
114 __bos0 (__buf));
115 }
116
117 return __pread64_alias (__fd, __buf, __nbytes, __offset);
118}
119# endif
120#endif
121
122#if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K
123extern ssize_t __readlink_chk (const char *__restrict __path,
124 char *__restrict __buf, size_t __len,
125 size_t __buflen)
126 __THROW __nonnull ((1, 2)) __wur;
127extern ssize_t __REDIRECT_NTH (__readlink_alias,
128 (const char *__restrict __path,
129 char *__restrict __buf, size_t __len), readlink)
130 __nonnull ((1, 2)) __wur;
131extern ssize_t __REDIRECT_NTH (__readlink_chk_warn,
132 (const char *__restrict __path,
133 char *__restrict __buf, size_t __len,
134 size_t __buflen), __readlink_chk)
135 __nonnull ((1, 2)) __wur __warnattr ("readlink called with bigger length "
136 "than size of destination buffer");
137
138__fortify_function __nonnull ((1, 2)) __wur ssize_t
139__NTH (readlink (const char *__restrict __path, char *__restrict __buf,
140 size_t __len))
141{
142 if (__bos (__buf) != (size_t) -1)
143 {
144 if (!__builtin_constant_p (__len))
145 return __readlink_chk (__path, __buf, __len, __bos (__buf));
146
147 if ( __len > __bos (__buf))
148 return __readlink_chk_warn (__path, __buf, __len, __bos (__buf));
149 }
150 return __readlink_alias (__path, __buf, __len);
151}
152#endif
153
154#ifdef __USE_ATFILE
155extern ssize_t __readlinkat_chk (int __fd, const char *__restrict __path,
156 char *__restrict __buf, size_t __len,
157 size_t __buflen)
158 __THROW __nonnull ((2, 3)) __wur;
159extern ssize_t __REDIRECT_NTH (__readlinkat_alias,
160 (int __fd, const char *__restrict __path,
161 char *__restrict __buf, size_t __len),
162 readlinkat)
163 __nonnull ((2, 3)) __wur;
164extern ssize_t __REDIRECT_NTH (__readlinkat_chk_warn,
165 (int __fd, const char *__restrict __path,
166 char *__restrict __buf, size_t __len,
167 size_t __buflen), __readlinkat_chk)
168 __nonnull ((2, 3)) __wur __warnattr ("readlinkat called with bigger "
169 "length than size of destination "
170 "buffer");
171
172__fortify_function __nonnull ((2, 3)) __wur ssize_t
173__NTH (readlinkat (int __fd, const char *__restrict __path,
174 char *__restrict __buf, size_t __len))
175{
176 if (__bos (__buf) != (size_t) -1)
177 {
178 if (!__builtin_constant_p (__len))
179 return __readlinkat_chk (__fd, __path, __buf, __len, __bos (__buf));
180
181 if (__len > __bos (__buf))
182 return __readlinkat_chk_warn (__fd, __path, __buf, __len,
183 __bos (__buf));
184 }
185 return __readlinkat_alias (__fd, __path, __buf, __len);
186}
187#endif
188
189extern char *__getcwd_chk (char *__buf, size_t __size, size_t __buflen)
190 __THROW __wur;
191extern char *__REDIRECT_NTH (__getcwd_alias,
192 (char *__buf, size_t __size), getcwd) __wur;
193extern char *__REDIRECT_NTH (__getcwd_chk_warn,
194 (char *__buf, size_t __size, size_t __buflen),
195 __getcwd_chk)
196 __wur __warnattr ("getcwd caller with bigger length than size of "
197 "destination buffer");
198
199__fortify_function __wur char *
200__NTH (getcwd (char *__buf, size_t __size))
201{
202 if (__bos (__buf) != (size_t) -1)
203 {
204 if (!__builtin_constant_p (__size))
205 return __getcwd_chk (__buf, __size, __bos (__buf));
206
207 if (__size > __bos (__buf))
208 return __getcwd_chk_warn (__buf, __size, __bos (__buf));
209 }
210 return __getcwd_alias (__buf, __size);
211}
212
213#if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
214extern char *__getwd_chk (char *__buf, size_t buflen)
215 __THROW __nonnull ((1)) __wur;
216extern char *__REDIRECT_NTH (__getwd_warn, (char *__buf), getwd)
217 __nonnull ((1)) __wur __warnattr ("please use getcwd instead, as getwd "
218 "doesn't specify buffer size");
219
220__fortify_function __nonnull ((1)) __attribute_deprecated__ __wur char *
221__NTH (getwd (char *__buf))
222{
223 if (__bos (__buf) != (size_t) -1)
224 return __getwd_chk (__buf, __bos (__buf));
225 return __getwd_warn (__buf);
226}
227#endif
228
229extern size_t __confstr_chk (int __name, char *__buf, size_t __len,
230 size_t __buflen) __THROW;
231extern size_t __REDIRECT_NTH (__confstr_alias, (int __name, char *__buf,
232 size_t __len), confstr);
233extern size_t __REDIRECT_NTH (__confstr_chk_warn,
234 (int __name, char *__buf, size_t __len,
235 size_t __buflen), __confstr_chk)
236 __warnattr ("confstr called with bigger length than size of destination "
237 "buffer");
238
239__fortify_function size_t
240__NTH (confstr (int __name, char *__buf, size_t __len))
241{
242 if (__bos (__buf) != (size_t) -1)
243 {
244 if (!__builtin_constant_p (__len))
245 return __confstr_chk (__name, __buf, __len, __bos (__buf));
246
247 if (__bos (__buf) < __len)
248 return __confstr_chk_warn (__name, __buf, __len, __bos (__buf));
249 }
250 return __confstr_alias (__name, __buf, __len);
251}
252
253
254extern int __getgroups_chk (int __size, __gid_t __list[], size_t __listlen)
255 __THROW __wur;
256extern int __REDIRECT_NTH (__getgroups_alias, (int __size, __gid_t __list[]),
257 getgroups) __wur;
258extern int __REDIRECT_NTH (__getgroups_chk_warn,
259 (int __size, __gid_t __list[], size_t __listlen),
260 __getgroups_chk)
261 __wur __warnattr ("getgroups called with bigger group count than what "
262 "can fit into destination buffer");
263
264__fortify_function int
265__NTH (getgroups (int __size, __gid_t __list[]))
266{
267 if (__bos (__list) != (size_t) -1)
268 {
269 if (!__builtin_constant_p (__size) || __size < 0)
270 return __getgroups_chk (__size, __list, __bos (__list));
271
272 if (__size * sizeof (__gid_t) > __bos (__list))
273 return __getgroups_chk_warn (__size, __list, __bos (__list));
274 }
275 return __getgroups_alias (__size, __list);
276}
277
278
279extern int __ttyname_r_chk (int __fd, char *__buf, size_t __buflen,
280 size_t __nreal) __THROW __nonnull ((2));
281extern int __REDIRECT_NTH (__ttyname_r_alias, (int __fd, char *__buf,
282 size_t __buflen), ttyname_r)
283 __nonnull ((2));
284extern int __REDIRECT_NTH (__ttyname_r_chk_warn,
285 (int __fd, char *__buf, size_t __buflen,
286 size_t __nreal), __ttyname_r_chk)
287 __nonnull ((2)) __warnattr ("ttyname_r called with bigger buflen than "
288 "size of destination buffer");
289
290__fortify_function int
291__NTH (ttyname_r (int __fd, char *__buf, size_t __buflen))
292{
293 if (__bos (__buf) != (size_t) -1)
294 {
295 if (!__builtin_constant_p (__buflen))
296 return __ttyname_r_chk (__fd, __buf, __buflen, __bos (__buf));
297
298 if (__buflen > __bos (__buf))
299 return __ttyname_r_chk_warn (__fd, __buf, __buflen, __bos (__buf));
300 }
301 return __ttyname_r_alias (__fd, __buf, __buflen);
302}
303
304
305#ifdef __USE_POSIX199506
306extern int __getlogin_r_chk (char *__buf, size_t __buflen, size_t __nreal)
307 __nonnull ((1));
308extern int __REDIRECT (__getlogin_r_alias, (char *__buf, size_t __buflen),
309 getlogin_r) __nonnull ((1));
310extern int __REDIRECT (__getlogin_r_chk_warn,
311 (char *__buf, size_t __buflen, size_t __nreal),
312 __getlogin_r_chk)
313 __nonnull ((1)) __warnattr ("getlogin_r called with bigger buflen than "
314 "size of destination buffer");
315
316__fortify_function int
317getlogin_r (char *__buf, size_t __buflen)
318{
319 if (__bos (__buf) != (size_t) -1)
320 {
321 if (!__builtin_constant_p (__buflen))
322 return __getlogin_r_chk (__buf, __buflen, __bos (__buf));
323
324 if (__buflen > __bos (__buf))
325 return __getlogin_r_chk_warn (__buf, __buflen, __bos (__buf));
326 }
327 return __getlogin_r_alias (__buf, __buflen);
328}
329#endif
330
331
332#if defined __USE_MISC || defined __USE_UNIX98
333extern int __gethostname_chk (char *__buf, size_t __buflen, size_t __nreal)
334 __THROW __nonnull ((1));
335extern int __REDIRECT_NTH (__gethostname_alias, (char *__buf, size_t __buflen),
336 gethostname) __nonnull ((1));
337extern int __REDIRECT_NTH (__gethostname_chk_warn,
338 (char *__buf, size_t __buflen, size_t __nreal),
339 __gethostname_chk)
340 __nonnull ((1)) __warnattr ("gethostname called with bigger buflen than "
341 "size of destination buffer");
342
343__fortify_function int
344__NTH (gethostname (char *__buf, size_t __buflen))
345{
346 if (__bos (__buf) != (size_t) -1)
347 {
348 if (!__builtin_constant_p (__buflen))
349 return __gethostname_chk (__buf, __buflen, __bos (__buf));
350
351 if (__buflen > __bos (__buf))
352 return __gethostname_chk_warn (__buf, __buflen, __bos (__buf));
353 }
354 return __gethostname_alias (__buf, __buflen);
355}
356#endif
357
358
359#if defined __USE_MISC || (defined __USE_XOPEN && !defined __USE_UNIX98)
360extern int __getdomainname_chk (char *__buf, size_t __buflen, size_t __nreal)
361 __THROW __nonnull ((1)) __wur;
362extern int __REDIRECT_NTH (__getdomainname_alias, (char *__buf,
363 size_t __buflen),
364 getdomainname) __nonnull ((1)) __wur;
365extern int __REDIRECT_NTH (__getdomainname_chk_warn,
366 (char *__buf, size_t __buflen, size_t __nreal),
367 __getdomainname_chk)
368 __nonnull ((1)) __wur __warnattr ("getdomainname called with bigger "
369 "buflen than size of destination "
370 "buffer");
371
372__fortify_function int
373__NTH (getdomainname (char *__buf, size_t __buflen))
374{
375 if (__bos (__buf) != (size_t) -1)
376 {
377 if (!__builtin_constant_p (__buflen))
378 return __getdomainname_chk (__buf, __buflen, __bos (__buf));
379
380 if (__buflen > __bos (__buf))
381 return __getdomainname_chk_warn (__buf, __buflen, __bos (__buf));
382 }
383 return __getdomainname_alias (__buf, __buflen);
384}
385#endif
386