1 /* Copyright (C) 2004, 2005, 2007 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 # error "Never use <bits/string3.h> directly; include <string.h> instead."
23 __warndecl (__warn_memset_zero_len
,
24 "memset used with constant zero length parameter; this could be due to transposed parameters");
27 /* XXX This is temporarily. We should not redefine any of the symbols
28 and instead integrate the error checking into the original
49 __extern_always_inline
void *
50 __NTH (memcpy (void *__restrict __dest
, __const
void *__restrict __src
,
53 return __builtin___memcpy_chk (__dest
, __src
, __len
, __bos0 (__dest
));
56 # define memcpy(dest, src, len) \
57 ((__bos0 (dest) != (size_t) -1) \
58 ? __builtin___memcpy_chk (dest, src, len, __bos0 (dest)) \
59 : __memcpy_ichk (dest, src, len))
60 static __always_inline
void *
61 __NTH (__memcpy_ichk (void *__restrict __dest
, __const
void *__restrict __src
,
64 return __builtin___memcpy_chk (__dest
, __src
, __len
, __bos0 (__dest
));
69 __extern_always_inline
void *
70 __NTH (memmove (void *__restrict __dest
, __const
void *__restrict __src
,
73 return __builtin___memmove_chk (__dest
, __src
, __len
, __bos0 (__dest
));
76 # define memmove(dest, src, len) \
77 ((__bos0 (dest) != (size_t) -1) \
78 ? __builtin___memmove_chk (dest, src, len, __bos0 (dest)) \
79 : __memmove_ichk (dest, src, len))
80 static __always_inline
void *
81 __NTH (__memmove_ichk (void *__dest
, __const
void *__src
, size_t __len
))
83 return __builtin___memmove_chk (__dest
, __src
, __len
, __bos0 (__dest
));
89 __extern_always_inline
void *
90 __NTH (mempcpy (void *__restrict __dest
, __const
void *__restrict __src
,
93 return __builtin___mempcpy_chk (__dest
, __src
, __len
, __bos0 (__dest
));
96 # define mempcpy(dest, src, len) \
97 ((__bos0 (dest) != (size_t) -1) \
98 ? __builtin___mempcpy_chk (dest, src, len, __bos0 (dest)) \
99 : __mempcpy_ichk (dest, src, len))
100 static __always_inline
void *
101 __NTH (__mempcpy_ichk (void *__restrict __dest
,
102 __const
void *__restrict __src
, size_t __len
))
104 return __builtin___mempcpy_chk (__dest
, __src
, __len
, __bos0 (__dest
));
110 /* The first two tests here help to catch a somewhat common problem
111 where the second and third parameter are transposed. This is
112 especially problematic if the intended fill value is zero. In this
113 case no work is done at all. We detect these problems by referring
114 non-existing functions. */
116 __extern_always_inline
void *
117 __NTH (memset (void *__dest
, int __ch
, size_t __len
))
119 if (__builtin_constant_p (__len
) && __len
== 0)
121 __warn_memset_zero_len ();
124 return __builtin___memset_chk (__dest
, __ch
, __len
, __bos0 (__dest
));
127 # define memset(dest, ch, len) \
128 (__builtin_constant_p (len) && (len) == 0 \
129 ? (__warn_memset_zero_len (), (void) (ch), (void) (len), (void *) (dest)) \
130 : ((__bos0 (dest) != (size_t) -1) \
131 ? __builtin___memset_chk (dest, ch, len, __bos0 (dest)) \
132 : __memset_ichk (dest, ch, len)))
133 static __always_inline
void *
134 __NTH (__memset_ichk (void *__dest
, int __ch
, size_t __len
))
136 return __builtin___memset_chk (__dest
, __ch
, __len
, __bos0 (__dest
));
142 __extern_always_inline
void
143 __NTH (bcopy (__const
void *__restrict __src
, void *__restrict __dest
,
146 __builtin___memmove_chk (__dest
, __src
, __len
, __bos0 (__dest
));
148 __extern_always_inline
void
149 __NTH (bzero (void *__dest
, size_t __len
))
151 __builtin___memset_chk (__dest
, '\0', __len
, __bos0 (__dest
));
154 # define bcopy(src, dest, len) ((void) \
155 ((__bos0 (dest) != (size_t) -1) \
156 ? __builtin___memmove_chk (dest, src, len, __bos0 (dest)) \
157 : __memmove_ichk (dest, src, len)))
158 # define bzero(dest, len) ((void) \
159 ((__bos0 (dest) != (size_t) -1) \
160 ? __builtin___memset_chk (dest, '\0', len, __bos0 (dest)) \
161 : __memset_ichk (dest, '\0', len)))
166 __extern_always_inline
char *
167 __NTH (strcpy (char *__restrict __dest
, __const
char *__restrict __src
))
169 return __builtin___strcpy_chk (__dest
, __src
, __bos (__dest
));
172 # define strcpy(dest, src) \
173 ((__bos (dest) != (size_t) -1) \
174 ? __builtin___strcpy_chk (dest, src, __bos (dest)) \
175 : __strcpy_ichk (dest, src))
176 static __always_inline
char *
177 __NTH (__strcpy_ichk (char *__restrict __dest
, __const
char *__restrict __src
))
179 return __builtin___strcpy_chk (__dest
, __src
, __bos (__dest
));
185 __extern_always_inline
char *
186 __NTH (stpcpy (char *__restrict __dest
, __const
char *__restrict __src
))
188 return __builtin___stpcpy_chk (__dest
, __src
, __bos (__dest
));
191 # define stpcpy(dest, src) \
192 ((__bos (dest) != (size_t) -1) \
193 ? __builtin___stpcpy_chk (dest, src, __bos (dest)) \
194 : __stpcpy_ichk (dest, src))
195 static __always_inline
char *
196 __NTH (__stpcpy_ichk (char *__restrict __dest
, __const
char *__restrict __src
))
198 return __builtin___stpcpy_chk (__dest
, __src
, __bos (__dest
));
205 __extern_always_inline
char *
206 __NTH (strncpy (char *__restrict __dest
, __const
char *__restrict __src
,
209 return __builtin___strncpy_chk (__dest
, __src
, __len
, __bos (__dest
));
212 # define strncpy(dest, src, len) \
213 ((__bos (dest) != (size_t) -1) \
214 ? __builtin___strncpy_chk (dest, src, len, __bos (dest)) \
215 : __strncpy_ichk (dest, src, len))
216 static __always_inline
char *
217 __NTH (__strncpy_ichk (char *__restrict __dest
, __const
char *__restrict __src
,
220 return __builtin___strncpy_chk (__dest
, __src
, __len
, __bos (__dest
));
224 // XXX We have no corresponding builtin yet.
225 extern char *__stpncpy_chk (char *__dest
, __const
char *__src
, size_t __n
,
226 size_t __destlen
) __THROW
;
227 extern char *__REDIRECT_NTH (__stpncpy_alias
, (char *__dest
,
229 size_t __n
), stpncpy
);
231 __extern_always_inline
char *
232 __NTH (stpncpy (char *__dest
, __const
char *__src
, size_t __n
))
234 if (__bos (__dest
) != (size_t) -1
235 && (!__builtin_constant_p (__n
) || __n
<= __bos (__dest
)))
236 return __stpncpy_chk (__dest
, __src
, __n
, __bos (__dest
));
237 return __stpncpy_alias (__dest
, __src
, __n
);
242 __extern_always_inline
char *
243 __NTH (strcat (char *__restrict __dest
, __const
char *__restrict __src
))
245 return __builtin___strcat_chk (__dest
, __src
, __bos (__dest
));
248 # define strcat(dest, src) \
249 ((__bos (dest) != (size_t) -1) \
250 ? __builtin___strcat_chk (dest, src, __bos (dest)) \
251 : __strcat_ichk (dest, src))
252 static __always_inline
char *
253 __NTH (__strcat_ichk (char *__restrict __dest
, __const
char *__restrict __src
))
255 return __builtin___strcat_chk (__dest
, __src
, __bos (__dest
));
261 __extern_always_inline
char *
262 __NTH (strncat (char *__restrict __dest
, __const
char *__restrict __src
,
265 return __builtin___strncat_chk (__dest
, __src
, __len
, __bos (__dest
));
268 # define strncat(dest, src, len) \
269 ((__bos (dest) != (size_t) -1) \
270 ? __builtin___strncat_chk (dest, src, len, __bos (dest)) \
271 : __strncat_ichk (dest, src, len))
272 static __always_inline
char *
273 __NTH (__strncat_ichk (char *__restrict __dest
, __const
char *__restrict __src
,
276 return __builtin___strncat_chk (__dest
, __src
, __len
, __bos (__dest
));