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