1 | /* Checking macros for stdlib functions. |
2 | Copyright (C) 2005-2021 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 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #ifndef _STDLIB_H |
20 | # error "Never include <bits/stdlib.h> directly; use <stdlib.h> instead." |
21 | #endif |
22 | |
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" ); |
35 | |
36 | __fortify_function __wur char * |
37 | __NTH (realpath (const char *__restrict __name, char *__restrict __resolved)) |
38 | { |
39 | if (__glibc_objsize (__resolved) != (size_t) -1) |
40 | { |
41 | #if defined _LIBC_LIMITS_H_ && defined PATH_MAX |
42 | if (__glibc_objsize (__resolved) < PATH_MAX) |
43 | return __realpath_chk_warn (__name, __resolved, |
44 | __glibc_objsize (__resolved)); |
45 | #endif |
46 | return __realpath_chk (__name, __resolved, __glibc_objsize (__resolved)); |
47 | } |
48 | |
49 | return __realpath_alias (__name, __resolved); |
50 | } |
51 | |
52 | |
53 | extern int __ptsname_r_chk (int __fd, char *__buf, size_t __buflen, |
54 | size_t __nreal) __THROW __nonnull ((2)) |
55 | __attr_access ((__write_only__, 2, 3)); |
56 | extern int __REDIRECT_NTH (__ptsname_r_alias, (int __fd, char *__buf, |
57 | size_t __buflen), ptsname_r) |
58 | __nonnull ((2)) __attr_access ((__write_only__, 2, 3)); |
59 | extern int __REDIRECT_NTH (__ptsname_r_chk_warn, |
60 | (int __fd, char *__buf, size_t __buflen, |
61 | size_t __nreal), __ptsname_r_chk) |
62 | __nonnull ((2)) __warnattr ("ptsname_r called with buflen bigger than " |
63 | "size of buf" ); |
64 | |
65 | __fortify_function int |
66 | __NTH (ptsname_r (int __fd, char *__buf, size_t __buflen)) |
67 | { |
68 | if (__glibc_objsize (__buf) != (size_t) -1) |
69 | { |
70 | if (!__builtin_constant_p (__buflen)) |
71 | return __ptsname_r_chk (__fd, __buf, __buflen, |
72 | __glibc_objsize (__buf)); |
73 | if (__buflen > __glibc_objsize (__buf)) |
74 | return __ptsname_r_chk_warn (__fd, __buf, __buflen, |
75 | __glibc_objsize (__buf)); |
76 | } |
77 | return __ptsname_r_alias (__fd, __buf, __buflen); |
78 | } |
79 | |
80 | |
81 | extern int __wctomb_chk (char *__s, wchar_t __wchar, size_t __buflen) |
82 | __THROW __wur; |
83 | extern int __REDIRECT_NTH (__wctomb_alias, (char *__s, wchar_t __wchar), |
84 | wctomb) __wur; |
85 | |
86 | __fortify_function __wur int |
87 | __NTH (wctomb (char *__s, wchar_t __wchar)) |
88 | { |
89 | /* We would have to include <limits.h> to get a definition of MB_LEN_MAX. |
90 | But this would only disturb the namespace. So we define our own |
91 | version here. */ |
92 | #define __STDLIB_MB_LEN_MAX 16 |
93 | #if defined MB_LEN_MAX && MB_LEN_MAX != __STDLIB_MB_LEN_MAX |
94 | # error "Assumed value of MB_LEN_MAX wrong" |
95 | #endif |
96 | if (__glibc_objsize (__s) != (size_t) -1 |
97 | && __STDLIB_MB_LEN_MAX > __glibc_objsize (__s)) |
98 | return __wctomb_chk (__s, __wchar, __glibc_objsize (__s)); |
99 | return __wctomb_alias (__s, __wchar); |
100 | } |
101 | |
102 | |
103 | extern size_t __mbstowcs_chk (wchar_t *__restrict __dst, |
104 | const char *__restrict __src, |
105 | size_t __len, size_t __dstlen) __THROW |
106 | __attr_access ((__write_only__, 1, 3)) __attr_access ((__read_only__, 2)); |
107 | extern size_t __REDIRECT_NTH (__mbstowcs_alias, |
108 | (wchar_t *__restrict __dst, |
109 | const char *__restrict __src, |
110 | size_t __len), mbstowcs) |
111 | __attr_access ((__write_only__, 1, 3)) __attr_access ((__read_only__, 2)); |
112 | extern size_t __REDIRECT_NTH (__mbstowcs_chk_warn, |
113 | (wchar_t *__restrict __dst, |
114 | const char *__restrict __src, |
115 | size_t __len, size_t __dstlen), __mbstowcs_chk) |
116 | __warnattr ("mbstowcs called with dst buffer smaller than len " |
117 | "* sizeof (wchar_t)" ); |
118 | |
119 | __fortify_function size_t |
120 | __NTH (mbstowcs (wchar_t *__restrict __dst, const char *__restrict __src, |
121 | size_t __len)) |
122 | { |
123 | if (__glibc_objsize (__dst) != (size_t) -1) |
124 | { |
125 | if (!__builtin_constant_p (__len)) |
126 | return __mbstowcs_chk (__dst, __src, __len, |
127 | __glibc_objsize (__dst) / sizeof (wchar_t)); |
128 | |
129 | if (__len > __glibc_objsize (__dst) / sizeof (wchar_t)) |
130 | return __mbstowcs_chk_warn (__dst, __src, __len, |
131 | (__glibc_objsize (__dst) |
132 | / sizeof (wchar_t))); |
133 | } |
134 | return __mbstowcs_alias (__dst, __src, __len); |
135 | } |
136 | |
137 | |
138 | extern size_t __wcstombs_chk (char *__restrict __dst, |
139 | const wchar_t *__restrict __src, |
140 | size_t __len, size_t __dstlen) __THROW |
141 | __attr_access ((__write_only__, 1, 3)) __attr_access ((__read_only__, 2)); |
142 | extern size_t __REDIRECT_NTH (__wcstombs_alias, |
143 | (char *__restrict __dst, |
144 | const wchar_t *__restrict __src, |
145 | size_t __len), wcstombs) |
146 | __attr_access ((__write_only__, 1, 3)) __attr_access ((__read_only__, 2)); |
147 | extern size_t __REDIRECT_NTH (__wcstombs_chk_warn, |
148 | (char *__restrict __dst, |
149 | const wchar_t *__restrict __src, |
150 | size_t __len, size_t __dstlen), __wcstombs_chk) |
151 | __warnattr ("wcstombs called with dst buffer smaller than len" ); |
152 | |
153 | __fortify_function size_t |
154 | __NTH (wcstombs (char *__restrict __dst, const wchar_t *__restrict __src, |
155 | size_t __len)) |
156 | { |
157 | if (__glibc_objsize (__dst) != (size_t) -1) |
158 | { |
159 | if (!__builtin_constant_p (__len)) |
160 | return __wcstombs_chk (__dst, __src, __len, __glibc_objsize (__dst)); |
161 | if (__len > __glibc_objsize (__dst)) |
162 | return __wcstombs_chk_warn (__dst, __src, __len, |
163 | __glibc_objsize (__dst)); |
164 | } |
165 | return __wcstombs_alias (__dst, __src, __len); |
166 | } |
167 | |