string_fortified.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* Copyright (C) 2004-2026 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. <https://www.gnu.org/licenses/>. */
  14. #ifndef _BITS_STRING_FORTIFIED_H
  15. #define _BITS_STRING_FORTIFIED_H 1
  16. #ifndef _STRING_H
  17. # error "Never use <bits/string_fortified.h> directly; include <string.h> instead."
  18. #endif
  19. __fortify_function void *
  20. __NTH (memcpy (void *__restrict __dest, const void *__restrict __src,
  21. size_t __len))
  22. {
  23. return __builtin___memcpy_chk (__dest, __src, __len,
  24. __glibc_objsize0 (__dest));
  25. }
  26. __fortify_function void *
  27. __NTH (memmove (void *__dest, const void *__src, size_t __len))
  28. {
  29. return __builtin___memmove_chk (__dest, __src, __len,
  30. __glibc_objsize0 (__dest));
  31. }
  32. #ifdef __USE_GNU
  33. __fortify_function void *
  34. __NTH (mempcpy (void *__restrict __dest, const void *__restrict __src,
  35. size_t __len))
  36. {
  37. return __builtin___mempcpy_chk (__dest, __src, __len,
  38. __glibc_objsize0 (__dest));
  39. }
  40. #endif
  41. /* The first two tests here help to catch a somewhat common problem
  42. where the second and third parameter are transposed. This is
  43. especially problematic if the intended fill value is zero. In this
  44. case no work is done at all. We detect these problems by referring
  45. non-existing functions. */
  46. __fortify_function void *
  47. __NTH (memset (void *__dest, int __ch, size_t __len))
  48. {
  49. return __builtin___memset_chk (__dest, __ch, __len,
  50. __glibc_objsize0 (__dest));
  51. }
  52. #if defined __USE_MISC || __GLIBC_USE (ISOC23)
  53. void *__memset_explicit_chk (void *__s, int __c, size_t __n, size_t __destlen)
  54. __THROW __nonnull ((1)) __fortified_attr_access (__write_only__, 1, 3);
  55. __fortify_function void *
  56. __NTH (memset_explicit (void *__dest, int __ch, size_t __len))
  57. {
  58. return __memset_explicit_chk (__dest, __ch, __len,
  59. __glibc_objsize0 (__dest));
  60. }
  61. #endif
  62. #ifdef __USE_MISC
  63. # include <bits/strings_fortified.h>
  64. void __explicit_bzero_chk (void *__dest, size_t __len, size_t __destlen)
  65. __THROW __nonnull ((1)) __fortified_attr_access (__write_only__, 1, 2);
  66. __fortify_function void
  67. __NTH (explicit_bzero (void *__dest, size_t __len))
  68. {
  69. __explicit_bzero_chk (__dest, __len, __glibc_objsize0 (__dest));
  70. }
  71. #endif
  72. __fortify_function __attribute_overloadable__ char *
  73. __NTH (strcpy (__fortify_clang_overload_arg (char *, __restrict, __dest),
  74. const char *__restrict __src))
  75. __fortify_clang_warn_if_src_too_large (__dest, __src)
  76. {
  77. return __builtin___strcpy_chk (__dest, __src, __glibc_objsize (__dest));
  78. }
  79. #ifdef __USE_XOPEN2K8
  80. __fortify_function __attribute_overloadable__ char *
  81. __NTH (stpcpy (__fortify_clang_overload_arg (char *, __restrict, __dest),
  82. const char *__restrict __src))
  83. __fortify_clang_warn_if_src_too_large (__dest, __src)
  84. {
  85. return __builtin___stpcpy_chk (__dest, __src, __glibc_objsize (__dest));
  86. }
  87. #endif
  88. __fortify_function __attribute_overloadable__ char *
  89. __NTH (strncpy (__fortify_clang_overload_arg (char *, __restrict, __dest),
  90. const char *__restrict __src, size_t __len))
  91. __fortify_clang_warn_if_dest_too_small (__dest, __len)
  92. {
  93. return __builtin___strncpy_chk (__dest, __src, __len,
  94. __glibc_objsize (__dest));
  95. }
  96. #ifdef __USE_XOPEN2K8
  97. # if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
  98. __fortify_function __attribute_overloadable__ char *
  99. __NTH (stpncpy (__fortify_clang_overload_arg (char *, ,__dest),
  100. const char *__src, size_t __n))
  101. __fortify_clang_warn_if_dest_too_small (__dest, __n)
  102. {
  103. return __builtin___stpncpy_chk (__dest, __src, __n,
  104. __glibc_objsize (__dest));
  105. }
  106. # else
  107. extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
  108. size_t __destlen) __THROW
  109. __fortified_attr_access (__write_only__, 1, 3)
  110. __attr_access ((__read_only__, 2));
  111. extern char *__REDIRECT_NTH (__stpncpy_alias, (char *__dest, const char *__src,
  112. size_t __n), stpncpy);
  113. __fortify_function __attribute_overloadable__ char *
  114. __NTH (stpncpy (__fortify_clang_overload_arg (char *, ,__dest),
  115. const char *__src, size_t __n))
  116. {
  117. if (__bos (__dest) != (size_t) -1
  118. && (!__builtin_constant_p (__n) || __n > __bos (__dest)))
  119. return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
  120. return __stpncpy_alias (__dest, __src, __n);
  121. }
  122. # endif
  123. #endif
  124. __fortify_function __attribute_overloadable__ char *
  125. __NTH (strcat (__fortify_clang_overload_arg (char *, __restrict, __dest),
  126. const char *__restrict __src))
  127. __fortify_clang_warn_if_src_too_large (__dest, __src)
  128. {
  129. return __builtin___strcat_chk (__dest, __src, __glibc_objsize (__dest));
  130. }
  131. __fortify_function __attribute_overloadable__ char *
  132. __NTH (strncat (__fortify_clang_overload_arg (char *, __restrict, __dest),
  133. const char *__restrict __src, size_t __len))
  134. __fortify_clang_warn_if_src_too_large (__dest, __src)
  135. {
  136. return __builtin___strncat_chk (__dest, __src, __len,
  137. __glibc_objsize (__dest));
  138. }
  139. #ifdef __USE_MISC
  140. extern size_t __strlcpy_chk (char *__dest, const char *__src, size_t __n,
  141. size_t __destlen) __THROW;
  142. extern size_t __REDIRECT_NTH (__strlcpy_alias,
  143. (char *__dest, const char *__src, size_t __n),
  144. strlcpy);
  145. __fortify_function __attribute_overloadable__ size_t
  146. __NTH (strlcpy (__fortify_clang_overload_arg (char *, __restrict, __dest),
  147. const char *__restrict __src, size_t __n))
  148. __fortify_clang_warn_if_dest_too_small (__dest, __n)
  149. {
  150. if (__glibc_objsize (__dest) != (size_t) -1
  151. && (!__builtin_constant_p (__n > __glibc_objsize (__dest))
  152. || __n > __glibc_objsize (__dest)))
  153. return __strlcpy_chk (__dest, __src, __n, __glibc_objsize (__dest));
  154. return __strlcpy_alias (__dest, __src, __n);
  155. }
  156. extern size_t __strlcat_chk (char *__dest, const char *__src, size_t __n,
  157. size_t __destlen) __THROW;
  158. extern size_t __REDIRECT_NTH (__strlcat_alias,
  159. (char *__dest, const char *__src, size_t __n),
  160. strlcat);
  161. __fortify_function __attribute_overloadable__ size_t
  162. __NTH (strlcat (__fortify_clang_overload_arg (char *, __restrict, __dest),
  163. const char *__restrict __src, size_t __n))
  164. {
  165. if (__glibc_objsize (__dest) != (size_t) -1
  166. && (!__builtin_constant_p (__n > __glibc_objsize (__dest))
  167. || __n > __glibc_objsize (__dest)))
  168. return __strlcat_chk (__dest, __src, __n, __glibc_objsize (__dest));
  169. return __strlcat_alias (__dest, __src, __n);
  170. }
  171. #endif /* __USE_MISC */
  172. #endif /* bits/string_fortified.h */