Back to index

glibc  2.9
string3.h
Go to the documentation of this file.
00001 /* Copyright (C) 2004, 2005, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #ifndef _STRING_H
00020 # error "Never use <bits/string3.h> directly; include <string.h> instead."
00021 #endif
00022 
00023 __warndecl (__warn_memset_zero_len,
00024            "memset used with constant zero length parameter; this could be due to transposed parameters");
00025 
00026 #ifndef __cplusplus
00027 /* XXX This is temporarily.  We should not redefine any of the symbols
00028    and instead integrate the error checking into the original
00029    definitions.  */
00030 # undef memcpy
00031 # undef memmove
00032 # undef memset
00033 # undef strcat
00034 # undef strcpy
00035 # undef strncat
00036 # undef strncpy
00037 # ifdef __USE_GNU
00038 #  undef mempcpy
00039 #  undef stpcpy
00040 # endif
00041 # ifdef __USE_BSD
00042 #  undef bcopy
00043 #  undef bzero
00044 # endif
00045 #endif
00046 
00047 
00048 __extern_always_inline void *
00049 __NTH (memcpy (void *__restrict __dest, __const void *__restrict __src,
00050               size_t __len))
00051 {
00052   return __builtin___memcpy_chk (__dest, __src, __len, __bos0 (__dest));
00053 }
00054 
00055 __extern_always_inline void *
00056 __NTH (memmove (void *__restrict __dest, __const void *__restrict __src,
00057               size_t __len))
00058 {
00059   return __builtin___memmove_chk (__dest, __src, __len, __bos0 (__dest));
00060 }
00061 
00062 #ifdef __USE_GNU
00063 __extern_always_inline void *
00064 __NTH (mempcpy (void *__restrict __dest, __const void *__restrict __src,
00065               size_t __len))
00066 {
00067   return __builtin___mempcpy_chk (__dest, __src, __len, __bos0 (__dest));
00068 }
00069 #endif
00070 
00071 
00072 /* The first two tests here help to catch a somewhat common problem
00073    where the second and third parameter are transposed.  This is
00074    especially problematic if the intended fill value is zero.  In this
00075    case no work is done at all.  We detect these problems by referring
00076    non-existing functions.  */
00077 __extern_always_inline void *
00078 __NTH (memset (void *__dest, int __ch, size_t __len))
00079 {
00080   if (__builtin_constant_p (__len) && __len == 0)
00081     {
00082       __warn_memset_zero_len ();
00083       return __dest;
00084     }
00085   return __builtin___memset_chk (__dest, __ch, __len, __bos0 (__dest));
00086 }
00087 
00088 #ifdef __USE_BSD
00089 __extern_always_inline void
00090 __NTH (bcopy (__const void *__restrict __src, void *__restrict __dest,
00091              size_t __len))
00092 {
00093   (void) __builtin___memmove_chk (__dest, __src, __len, __bos0 (__dest));
00094 }
00095 
00096 __extern_always_inline void
00097 __NTH (bzero (void *__dest, size_t __len))
00098 {
00099   (void) __builtin___memset_chk (__dest, '\0', __len, __bos0 (__dest));
00100 }
00101 #endif
00102 
00103 __extern_always_inline char *
00104 __NTH (strcpy (char *__restrict __dest, __const char *__restrict __src))
00105 {
00106   return __builtin___strcpy_chk (__dest, __src, __bos (__dest));
00107 }
00108 
00109 #ifdef __USE_GNU
00110 __extern_always_inline char *
00111 __NTH (stpcpy (char *__restrict __dest, __const char *__restrict __src))
00112 {
00113   return __builtin___stpcpy_chk (__dest, __src, __bos (__dest));
00114 }
00115 #endif
00116 
00117 
00118 __extern_always_inline char *
00119 __NTH (strncpy (char *__restrict __dest, __const char *__restrict __src,
00120               size_t __len))
00121 {
00122   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
00123 }
00124 
00125 // XXX We have no corresponding builtin yet.
00126 extern char *__stpncpy_chk (char *__dest, __const char *__src, size_t __n,
00127                          size_t __destlen) __THROW;
00128 extern char *__REDIRECT_NTH (__stpncpy_alias, (char *__dest,
00129                                           __const char *__src,
00130                                           size_t __n), stpncpy);
00131 
00132 __extern_always_inline char *
00133 __NTH (stpncpy (char *__dest, __const char *__src, size_t __n))
00134 {
00135   if (__bos (__dest) != (size_t) -1
00136       && (!__builtin_constant_p (__n) || __n <= __bos (__dest)))
00137     return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
00138   return __stpncpy_alias (__dest, __src, __n);
00139 }
00140 
00141 
00142 __extern_always_inline char *
00143 __NTH (strcat (char *__restrict __dest, __const char *__restrict __src))
00144 {
00145   return __builtin___strcat_chk (__dest, __src, __bos (__dest));
00146 }
00147 
00148 
00149 __extern_always_inline char *
00150 __NTH (strncat (char *__restrict __dest, __const char *__restrict __src,
00151               size_t __len))
00152 {
00153   return __builtin___strncat_chk (__dest, __src, __len, __bos (__dest));
00154 }