1 /* Copyright (C) 2004-2022 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #ifndef _BITS_STRING_FORTIFIED_H
19 #define _BITS_STRING_FORTIFIED_H 1
20 
21 #ifndef _STRING_H
22 # error "Never use <bits/string_fortified.h> directly; include <string.h> instead."
23 #endif
24 
25 __fortify_function void *
__NTH(memcpy (void * __restrict __dest,const void * __restrict __src,size_t __len))26 __NTH (memcpy (void *__restrict __dest, const void *__restrict __src,
27 	       size_t __len))
28 {
29   return __builtin___memcpy_chk (__dest, __src, __len,
30 				 __glibc_objsize0 (__dest));
31 }
32 
33 __fortify_function void *
__NTH(memmove (void * __dest,const void * __src,size_t __len))34 __NTH (memmove (void *__dest, const void *__src, size_t __len))
35 {
36   return __builtin___memmove_chk (__dest, __src, __len,
37 				  __glibc_objsize0 (__dest));
38 }
39 
40 #ifdef __USE_GNU
41 __fortify_function void *
__NTH(mempcpy (void * __restrict __dest,const void * __restrict __src,size_t __len))42 __NTH (mempcpy (void *__restrict __dest, const void *__restrict __src,
43 		size_t __len))
44 {
45   return __builtin___mempcpy_chk (__dest, __src, __len,
46 				  __glibc_objsize0 (__dest));
47 }
48 #endif
49 
50 
51 /* The first two tests here help to catch a somewhat common problem
52    where the second and third parameter are transposed.  This is
53    especially problematic if the intended fill value is zero.  In this
54    case no work is done at all.  We detect these problems by referring
55    non-existing functions.  */
56 __fortify_function void *
__NTH(memset (void * __dest,int __ch,size_t __len))57 __NTH (memset (void *__dest, int __ch, size_t __len))
58 {
59   return __builtin___memset_chk (__dest, __ch, __len,
60 				 __glibc_objsize0 (__dest));
61 }
62 
63 #ifdef __USE_MISC
64 # include <bits/strings_fortified.h>
65 
66 void __explicit_bzero_chk (void *__dest, size_t __len, size_t __destlen)
67   __THROW __nonnull ((1)) __fortified_attr_access (__write_only__, 1, 2);
68 
69 __fortify_function void
__NTH(explicit_bzero (void * __dest,size_t __len))70 __NTH (explicit_bzero (void *__dest, size_t __len))
71 {
72   __explicit_bzero_chk (__dest, __len, __glibc_objsize0 (__dest));
73 }
74 #endif
75 
76 __fortify_function char *
__NTH(strcpy (char * __restrict __dest,const char * __restrict __src))77 __NTH (strcpy (char *__restrict __dest, const char *__restrict __src))
78 {
79   return __builtin___strcpy_chk (__dest, __src, __glibc_objsize (__dest));
80 }
81 
82 #ifdef __USE_XOPEN2K8
83 __fortify_function char *
__NTH(stpcpy (char * __restrict __dest,const char * __restrict __src))84 __NTH (stpcpy (char *__restrict __dest, const char *__restrict __src))
85 {
86   return __builtin___stpcpy_chk (__dest, __src, __glibc_objsize (__dest));
87 }
88 #endif
89 
90 
91 __fortify_function char *
__NTH(strncpy (char * __restrict __dest,const char * __restrict __src,size_t __len))92 __NTH (strncpy (char *__restrict __dest, const char *__restrict __src,
93 		size_t __len))
94 {
95   return __builtin___strncpy_chk (__dest, __src, __len,
96 				  __glibc_objsize (__dest));
97 }
98 
99 #ifdef __USE_XOPEN2K8
100 # if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
101 __fortify_function char *
__NTH(stpncpy (char * __dest,const char * __src,size_t __n))102 __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
103 {
104   return __builtin___stpncpy_chk (__dest, __src, __n,
105 				  __glibc_objsize (__dest));
106 }
107 # else
108 extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
109 			    size_t __destlen) __THROW
110   __fortified_attr_access (__write_only__, 1, 3)
111   __attr_access ((__read_only__, 2));
112 extern char *__REDIRECT_NTH (__stpncpy_alias, (char *__dest, const char *__src,
113 					       size_t __n), stpncpy);
114 
115 __fortify_function char *
__NTH(stpncpy (char * __dest,const char * __src,size_t __n))116 __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
117 {
118   if (__bos (__dest) != (size_t) -1
119       && (!__builtin_constant_p (__n) || __n > __bos (__dest)))
120     return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
121   return __stpncpy_alias (__dest, __src, __n);
122 }
123 # endif
124 #endif
125 
126 
127 __fortify_function char *
__NTH(strcat (char * __restrict __dest,const char * __restrict __src))128 __NTH (strcat (char *__restrict __dest, const char *__restrict __src))
129 {
130   return __builtin___strcat_chk (__dest, __src, __glibc_objsize (__dest));
131 }
132 
133 
134 __fortify_function char *
__NTH(strncat (char * __restrict __dest,const char * __restrict __src,size_t __len))135 __NTH (strncat (char *__restrict __dest, const char *__restrict __src,
136 		size_t __len))
137 {
138   return __builtin___strncat_chk (__dest, __src, __len,
139 				  __glibc_objsize (__dest));
140 }
141 
142 #endif /* bits/string_fortified.h */
143