1 /* Checking macros for stdlib functions.
2 Copyright (C) 2005-2024 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/stdlib.h> directly; use <stdlib.h> instead."
23 extern char *__realpath_chk (const char *__restrict __name
,
24 char *__restrict __resolved
,
25 size_t __resolvedlen
) __THROW __wur
;
26 extern char *__REDIRECT_NTH (__realpath_alias
,
27 (const char *__restrict __name
,
28 char *__restrict __resolved
), realpath
) __wur
;
29 extern char *__REDIRECT_NTH (__realpath_chk_warn
,
30 (const char *__restrict __name
,
31 char *__restrict __resolved
,
32 size_t __resolvedlen
), __realpath_chk
) __wur
33 __warnattr ("second argument of realpath must be either NULL or at "
34 "least PATH_MAX bytes long buffer");
36 __fortify_function __attribute_overloadable__ __wur
char *
37 __NTH (realpath (const char *__restrict __name
,
38 __fortify_clang_overload_arg (char *, __restrict
, __resolved
)))
39 #if defined _LIBC_LIMITS_H_ && defined PATH_MAX
40 __fortify_clang_warning_only_if_bos_lt (PATH_MAX
, __resolved
,
41 "second argument of realpath must be "
42 "either NULL or at least PATH_MAX "
46 size_t __sz
= __glibc_objsize (__resolved
);
48 if (__sz
== (size_t) -1)
49 return __realpath_alias (__name
, __resolved
);
51 #if !__fortify_use_clang && defined _LIBC_LIMITS_H_ && defined PATH_MAX
52 if (__glibc_unsafe_len (PATH_MAX
, sizeof (char), __sz
))
53 return __realpath_chk_warn (__name
, __resolved
, __sz
);
55 return __realpath_chk (__name
, __resolved
, __sz
);
59 extern int __ptsname_r_chk (int __fd
, char *__buf
, size_t __buflen
,
60 size_t __nreal
) __THROW
__nonnull ((2))
61 __attr_access ((__write_only__
, 2, 3));
62 extern int __REDIRECT_NTH (__ptsname_r_alias
, (int __fd
, char *__buf
,
63 size_t __buflen
), ptsname_r
)
64 __nonnull ((2)) __attr_access ((__write_only__
, 2, 3));
65 extern int __REDIRECT_NTH (__ptsname_r_chk_warn
,
66 (int __fd
, char *__buf
, size_t __buflen
,
67 size_t __nreal
), __ptsname_r_chk
)
68 __nonnull ((2)) __warnattr ("ptsname_r called with buflen bigger than "
71 __fortify_function __attribute_overloadable__
int
72 __NTH (ptsname_r (int __fd
,
73 __fortify_clang_overload_arg (char *, ,__buf
),
75 __fortify_clang_warning_only_if_bos_lt (__buflen
, __buf
,
76 "ptsname_r called with buflen "
77 "bigger than size of buf")
79 return __glibc_fortify (ptsname_r
, __buflen
, sizeof (char),
80 __glibc_objsize (__buf
),
81 __fd
, __buf
, __buflen
);
85 extern int __wctomb_chk (char *__s
, wchar_t __wchar
, size_t __buflen
)
87 extern int __REDIRECT_NTH (__wctomb_alias
, (char *__s
, wchar_t __wchar
),
90 __fortify_function __attribute_overloadable__ __wur
int
91 __NTH (wctomb (__fortify_clang_overload_arg (char *, ,__s
), wchar_t __wchar
))
93 /* We would have to include <limits.h> to get a definition of MB_LEN_MAX.
94 But this would only disturb the namespace. So we define our own
96 #define __STDLIB_MB_LEN_MAX 16
97 #if defined MB_LEN_MAX && MB_LEN_MAX != __STDLIB_MB_LEN_MAX
98 # error "Assumed value of MB_LEN_MAX wrong"
100 if (__glibc_objsize (__s
) != (size_t) -1
101 && __STDLIB_MB_LEN_MAX
> __glibc_objsize (__s
))
102 return __wctomb_chk (__s
, __wchar
, __glibc_objsize (__s
));
103 return __wctomb_alias (__s
, __wchar
);
107 extern size_t __mbstowcs_chk (wchar_t *__restrict __dst
,
108 const char *__restrict __src
,
109 size_t __len
, size_t __dstlen
) __THROW
110 __attr_access ((__write_only__
, 1, 3)) __attr_access ((__read_only__
, 2));
111 extern size_t __REDIRECT_NTH (__mbstowcs_nulldst
,
112 (wchar_t *__restrict __dst
,
113 const char *__restrict __src
,
114 size_t __len
), mbstowcs
)
115 __attr_access ((__read_only__
, 2));
116 extern size_t __REDIRECT_NTH (__mbstowcs_alias
,
117 (wchar_t *__restrict __dst
,
118 const char *__restrict __src
,
119 size_t __len
), mbstowcs
)
120 __attr_access ((__write_only__
, 1, 3)) __attr_access ((__read_only__
, 2));
121 extern size_t __REDIRECT_NTH (__mbstowcs_chk_warn
,
122 (wchar_t *__restrict __dst
,
123 const char *__restrict __src
,
124 size_t __len
, size_t __dstlen
), __mbstowcs_chk
)
125 __warnattr ("mbstowcs called with dst buffer smaller than len "
126 "* sizeof (wchar_t)");
128 __fortify_function __attribute_overloadable__
size_t
129 __NTH (mbstowcs (__fortify_clang_overload_arg (wchar_t *, __restrict
, __dst
),
130 const char *__restrict __src
,
132 __fortify_clang_warning_only_if_bos0_lt2 (__len
, __dst
, sizeof (wchar_t),
133 "mbstowcs called with dst buffer "
134 "smaller than len * sizeof (wchar_t)")
136 if (__builtin_constant_p (__dst
== NULL
) && __dst
== NULL
)
137 return __mbstowcs_nulldst (__dst
, __src
, __len
);
139 return __glibc_fortify_n (mbstowcs
, __len
, sizeof (wchar_t),
140 __glibc_objsize (__dst
), __dst
, __src
, __len
);
143 extern size_t __wcstombs_chk (char *__restrict __dst
,
144 const wchar_t *__restrict __src
,
145 size_t __len
, size_t __dstlen
) __THROW
146 __attr_access ((__write_only__
, 1, 3)) __attr_access ((__read_only__
, 2));
147 extern size_t __REDIRECT_NTH (__wcstombs_alias
,
148 (char *__restrict __dst
,
149 const wchar_t *__restrict __src
,
150 size_t __len
), wcstombs
)
151 __attr_access ((__write_only__
, 1, 3)) __attr_access ((__read_only__
, 2));
152 extern size_t __REDIRECT_NTH (__wcstombs_chk_warn
,
153 (char *__restrict __dst
,
154 const wchar_t *__restrict __src
,
155 size_t __len
, size_t __dstlen
), __wcstombs_chk
)
156 __warnattr ("wcstombs called with dst buffer smaller than len");
158 __fortify_function __attribute_overloadable__
size_t
159 __NTH (wcstombs (__fortify_clang_overload_arg (char *, __restrict
, __dst
),
160 const wchar_t *__restrict __src
,
163 return __glibc_fortify (wcstombs
, __len
, sizeof (char),
164 __glibc_objsize (__dst
),
165 __dst
, __src
, __len
);