string3.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Copyright (C) 2004, 2005, 2007, 2009 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. #ifndef _STRING_H
  16. # error "Never use <bits/string3.h> directly; include <string.h> instead."
  17. #endif
  18. __warndecl (__warn_memset_zero_len,
  19. "memset used with constant zero length parameter; this could be due to transposed parameters");
  20. #ifndef __cplusplus
  21. /* XXX This is temporarily. We should not redefine any of the symbols
  22. and instead integrate the error checking into the original
  23. definitions. */
  24. # undef memcpy
  25. # undef memmove
  26. # undef memset
  27. # undef strcat
  28. # undef strcpy
  29. # undef strncat
  30. # undef strncpy
  31. # ifdef __USE_GNU
  32. # undef mempcpy
  33. # undef stpcpy
  34. # endif
  35. # ifdef __USE_BSD
  36. # undef bcopy
  37. # undef bzero
  38. # endif
  39. #endif
  40. __extern_always_inline void *
  41. __NTH (memcpy (void *__restrict __dest, __const void *__restrict __src,
  42. size_t __len))
  43. {
  44. return __builtin___memcpy_chk (__dest, __src, __len, __bos0 (__dest));
  45. }
  46. __extern_always_inline void *
  47. __NTH (memmove (void *__dest, __const void *__src, size_t __len))
  48. {
  49. return __builtin___memmove_chk (__dest, __src, __len, __bos0 (__dest));
  50. }
  51. #ifdef __USE_GNU
  52. __extern_always_inline void *
  53. __NTH (mempcpy (void *__restrict __dest, __const void *__restrict __src,
  54. size_t __len))
  55. {
  56. return __builtin___mempcpy_chk (__dest, __src, __len, __bos0 (__dest));
  57. }
  58. #endif
  59. /* The first two tests here help to catch a somewhat common problem
  60. where the second and third parameter are transposed. This is
  61. especially problematic if the intended fill value is zero. In this
  62. case no work is done at all. We detect these problems by referring
  63. non-existing functions. */
  64. __extern_always_inline void *
  65. __NTH (memset (void *__dest, int __ch, size_t __len))
  66. {
  67. if (__builtin_constant_p (__len) && __len == 0
  68. && (!__builtin_constant_p (__ch) || __ch != 0))
  69. {
  70. __warn_memset_zero_len ();
  71. return __dest;
  72. }
  73. return __builtin___memset_chk (__dest, __ch, __len, __bos0 (__dest));
  74. }
  75. #ifdef __USE_BSD
  76. __extern_always_inline void
  77. __NTH (bcopy (__const void *__src, void *__dest, size_t __len))
  78. {
  79. (void) __builtin___memmove_chk (__dest, __src, __len, __bos0 (__dest));
  80. }
  81. __extern_always_inline void
  82. __NTH (bzero (void *__dest, size_t __len))
  83. {
  84. (void) __builtin___memset_chk (__dest, '\0', __len, __bos0 (__dest));
  85. }
  86. #endif
  87. __extern_always_inline char *
  88. __NTH (strcpy (char *__restrict __dest, __const char *__restrict __src))
  89. {
  90. return __builtin___strcpy_chk (__dest, __src, __bos (__dest));
  91. }
  92. #ifdef __USE_GNU
  93. __extern_always_inline char *
  94. __NTH (stpcpy (char *__restrict __dest, __const char *__restrict __src))
  95. {
  96. return __builtin___stpcpy_chk (__dest, __src, __bos (__dest));
  97. }
  98. #endif
  99. __extern_always_inline char *
  100. __NTH (strncpy (char *__restrict __dest, __const char *__restrict __src,
  101. size_t __len))
  102. {
  103. return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
  104. }
  105. // XXX We have no corresponding builtin yet.
  106. extern char *__stpncpy_chk (char *__dest, __const char *__src, size_t __n,
  107. size_t __destlen) __THROW;
  108. extern char *__REDIRECT_NTH (__stpncpy_alias, (char *__dest,
  109. __const char *__src,
  110. size_t __n), stpncpy);
  111. __extern_always_inline char *
  112. __NTH (stpncpy (char *__dest, __const char *__src, size_t __n))
  113. {
  114. if (__bos (__dest) != (size_t) -1
  115. && (!__builtin_constant_p (__n) || __n <= __bos (__dest)))
  116. return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
  117. return __stpncpy_alias (__dest, __src, __n);
  118. }
  119. __extern_always_inline char *
  120. __NTH (strcat (char *__restrict __dest, __const char *__restrict __src))
  121. {
  122. return __builtin___strcat_chk (__dest, __src, __bos (__dest));
  123. }
  124. __extern_always_inline char *
  125. __NTH (strncat (char *__restrict __dest, __const char *__restrict __src,
  126. size_t __len))
  127. {
  128. return __builtin___strncat_chk (__dest, __src, __len, __bos (__dest));
  129. }