1 /* Copyright (C) 2004, 2005, 2007, 2009, 2010, 2012 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    <http://www.gnu.org/licenses/>.  */
17 
18 #ifndef _STRING_H
19 # error "Never use <bits/string3.h> directly; include <string.h> instead."
20 #endif
21 
22 __warndecl (__warn_memset_zero_len,
23 	    "memset used with constant zero length parameter; this could be due to transposed parameters");
24 
25 #ifndef __cplusplus
26 /* XXX This is temporarily.  We should not redefine any of the symbols
27    and instead integrate the error checking into the original
28    definitions.  */
29 # undef memcpy
30 # undef memmove
31 # undef memset
32 # undef strcat
33 # undef strcpy
34 # undef strncat
35 # undef strncpy
36 # ifdef __USE_GNU
37 #  undef mempcpy
38 #  undef stpcpy
39 # endif
40 # ifdef __USE_BSD
41 #  undef bcopy
42 #  undef bzero
43 # endif
44 #endif
45 
46 
47 __fortify_function void *
__NTH(memcpy (void * __restrict __dest,const void * __restrict __src,size_t __len))48 __NTH (memcpy (void *__restrict __dest, const void *__restrict __src,
49 	       size_t __len))
50 {
51   return __builtin___memcpy_chk (__dest, __src, __len, __bos0 (__dest));
52 }
53 
54 __fortify_function void *
__NTH(memmove (void * __dest,const void * __src,size_t __len))55 __NTH (memmove (void *__dest, const void *__src, size_t __len))
56 {
57   return __builtin___memmove_chk (__dest, __src, __len, __bos0 (__dest));
58 }
59 
60 #ifdef __USE_GNU
61 __fortify_function void *
__NTH(mempcpy (void * __restrict __dest,const void * __restrict __src,size_t __len))62 __NTH (mempcpy (void *__restrict __dest, const void *__restrict __src,
63 		size_t __len))
64 {
65   return __builtin___mempcpy_chk (__dest, __src, __len, __bos0 (__dest));
66 }
67 #endif
68 
69 
70 /* The first two tests here help to catch a somewhat common problem
71    where the second and third parameter are transposed.  This is
72    especially problematic if the intended fill value is zero.  In this
73    case no work is done at all.  We detect these problems by referring
74    non-existing functions.  */
75 __fortify_function void *
__NTH(memset (void * __dest,int __ch,size_t __len))76 __NTH (memset (void *__dest, int __ch, size_t __len))
77 {
78   if (__builtin_constant_p (__len) && __len == 0
79       && (!__builtin_constant_p (__ch) || __ch != 0))
80     {
81       __warn_memset_zero_len ();
82       return __dest;
83     }
84   return __builtin___memset_chk (__dest, __ch, __len, __bos0 (__dest));
85 }
86 
87 #ifdef __USE_BSD
88 __fortify_function void
__NTH(bcopy (const void * __src,void * __dest,size_t __len))89 __NTH (bcopy (const void *__src, void *__dest, size_t __len))
90 {
91   (void) __builtin___memmove_chk (__dest, __src, __len, __bos0 (__dest));
92 }
93 
94 __fortify_function void
__NTH(bzero (void * __dest,size_t __len))95 __NTH (bzero (void *__dest, size_t __len))
96 {
97   (void) __builtin___memset_chk (__dest, '\0', __len, __bos0 (__dest));
98 }
99 #endif
100 
101 __fortify_function char *
__NTH(strcpy (char * __restrict __dest,const char * __restrict __src))102 __NTH (strcpy (char *__restrict __dest, const char *__restrict __src))
103 {
104   return __builtin___strcpy_chk (__dest, __src, __bos (__dest));
105 }
106 
107 #ifdef __USE_GNU
108 __fortify_function char *
__NTH(stpcpy (char * __restrict __dest,const char * __restrict __src))109 __NTH (stpcpy (char *__restrict __dest, const char *__restrict __src))
110 {
111   return __builtin___stpcpy_chk (__dest, __src, __bos (__dest));
112 }
113 #endif
114 
115 
116 __fortify_function char *
__NTH(strncpy (char * __restrict __dest,const char * __restrict __src,size_t __len))117 __NTH (strncpy (char *__restrict __dest, const char *__restrict __src,
118 		size_t __len))
119 {
120   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
121 }
122 
123 // XXX We have no corresponding builtin yet.
124 extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
125 			    size_t __destlen) __THROW;
126 extern char *__REDIRECT_NTH (__stpncpy_alias, (char *__dest, const char *__src,
127 					       size_t __n), stpncpy);
128 
129 __fortify_function char *
__NTH(stpncpy (char * __dest,const char * __src,size_t __n))130 __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
131 {
132   if (__bos (__dest) != (size_t) -1
133       && (!__builtin_constant_p (__n) || __n <= __bos (__dest)))
134     return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
135   return __stpncpy_alias (__dest, __src, __n);
136 }
137 
138 
139 __fortify_function char *
__NTH(strcat (char * __restrict __dest,const char * __restrict __src))140 __NTH (strcat (char *__restrict __dest, const char *__restrict __src))
141 {
142   return __builtin___strcat_chk (__dest, __src, __bos (__dest));
143 }
144 
145 
146 __fortify_function char *
__NTH(strncat (char * __restrict __dest,const char * __restrict __src,size_t __len))147 __NTH (strncat (char *__restrict __dest, const char *__restrict __src,
148 		size_t __len))
149 {
150   return __builtin___strncat_chk (__dest, __src, __len, __bos (__dest));
151 }
152