string3.h 4.5 KB

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