string_fortified.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // Copyright 2026 Aarav Ravindra Kharade
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. /* Copyright (C) 2004-2026 Free Software Foundation, Inc.
  17. This file is part of the GNU C Library.
  18. The GNU C Library is free software; you can redistribute it and/or
  19. modify it under the terms of the GNU Lesser General Public
  20. License as published by the Free Software Foundation; either
  21. version 2.1 of the License, or (at your option) any later version.
  22. The GNU C Library is distributed in the hope that it will be useful,
  23. but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. Lesser General Public License for more details.
  26. You should have received a copy of the GNU Lesser General Public
  27. License along with the GNU C Library; if not, see
  28. <https://www.gnu.org/licenses/>. */
  29. #ifndef _BITS_STRING_FORTIFIED_H
  30. #define _BITS_STRING_FORTIFIED_H 1
  31. #ifndef _STRING_H
  32. # error "Never use <bits/string_fortified.h> directly; include <string.h> instead."
  33. #endif
  34. __fortify_function void *
  35. __NTH (memcpy (void *__restrict __dest, const void *__restrict __src,
  36. size_t __len))
  37. {
  38. return __builtin___memcpy_chk (__dest, __src, __len,
  39. __glibc_objsize0 (__dest));
  40. }
  41. __fortify_function void *
  42. __NTH (memmove (void *__dest, const void *__src, size_t __len))
  43. {
  44. return __builtin___memmove_chk (__dest, __src, __len,
  45. __glibc_objsize0 (__dest));
  46. }
  47. #ifdef __USE_GNU
  48. __fortify_function void *
  49. __NTH (mempcpy (void *__restrict __dest, const void *__restrict __src,
  50. size_t __len))
  51. {
  52. return __builtin___mempcpy_chk (__dest, __src, __len,
  53. __glibc_objsize0 (__dest));
  54. }
  55. #endif
  56. /* The first two tests here help to catch a somewhat common problem
  57. where the second and third parameter are transposed. This is
  58. especially problematic if the intended fill value is zero. In this
  59. case no work is done at all. We detect these problems by referring
  60. non-existing functions. */
  61. __fortify_function void *
  62. __NTH (memset (void *__dest, int __ch, size_t __len))
  63. {
  64. return __builtin___memset_chk (__dest, __ch, __len,
  65. __glibc_objsize0 (__dest));
  66. }
  67. #if defined __USE_MISC || __GLIBC_USE (ISOC23)
  68. void *__memset_explicit_chk (void *__s, int __c, size_t __n, size_t __destlen)
  69. __THROW __nonnull ((1)) __fortified_attr_access (__write_only__, 1, 3);
  70. __fortify_function void *
  71. __NTH (memset_explicit (void *__dest, int __ch, size_t __len))
  72. {
  73. return __memset_explicit_chk (__dest, __ch, __len,
  74. __glibc_objsize0 (__dest));
  75. }
  76. #endif
  77. #ifdef __USE_MISC
  78. # include <bits/strings_fortified.h>
  79. void __explicit_bzero_chk (void *__dest, size_t __len, size_t __destlen)
  80. __THROW __nonnull ((1)) __fortified_attr_access (__write_only__, 1, 2);
  81. __fortify_function void
  82. __NTH (explicit_bzero (void *__dest, size_t __len))
  83. {
  84. __explicit_bzero_chk (__dest, __len, __glibc_objsize0 (__dest));
  85. }
  86. #endif
  87. __fortify_function __attribute_overloadable__ char *
  88. __NTH (strcpy (__fortify_clang_overload_arg (char *, __restrict, __dest),
  89. const char *__restrict __src))
  90. __fortify_clang_warn_if_src_too_large (__dest, __src)
  91. {
  92. return __builtin___strcpy_chk (__dest, __src, __glibc_objsize (__dest));
  93. }
  94. #ifdef __USE_XOPEN2K8
  95. __fortify_function __attribute_overloadable__ char *
  96. __NTH (stpcpy (__fortify_clang_overload_arg (char *, __restrict, __dest),
  97. const char *__restrict __src))
  98. __fortify_clang_warn_if_src_too_large (__dest, __src)
  99. {
  100. return __builtin___stpcpy_chk (__dest, __src, __glibc_objsize (__dest));
  101. }
  102. #endif
  103. __fortify_function __attribute_overloadable__ char *
  104. __NTH (strncpy (__fortify_clang_overload_arg (char *, __restrict, __dest),
  105. const char *__restrict __src, size_t __len))
  106. __fortify_clang_warn_if_dest_too_small (__dest, __len)
  107. {
  108. return __builtin___strncpy_chk (__dest, __src, __len,
  109. __glibc_objsize (__dest));
  110. }
  111. #ifdef __USE_XOPEN2K8
  112. # if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
  113. __fortify_function __attribute_overloadable__ char *
  114. __NTH (stpncpy (__fortify_clang_overload_arg (char *, ,__dest),
  115. const char *__src, size_t __n))
  116. __fortify_clang_warn_if_dest_too_small (__dest, __n)
  117. {
  118. return __builtin___stpncpy_chk (__dest, __src, __n,
  119. __glibc_objsize (__dest));
  120. }
  121. # else
  122. extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
  123. size_t __destlen) __THROW
  124. __fortified_attr_access (__write_only__, 1, 3)
  125. __attr_access ((__read_only__, 2));
  126. extern char *__REDIRECT_NTH (__stpncpy_alias, (char *__dest, const char *__src,
  127. size_t __n), stpncpy);
  128. __fortify_function __attribute_overloadable__ char *
  129. __NTH (stpncpy (__fortify_clang_overload_arg (char *, ,__dest),
  130. const char *__src, size_t __n))
  131. {
  132. if (__bos (__dest) != (size_t) -1
  133. && (!__builtin_constant_p (__n) || __n > __bos (__dest)))
  134. return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
  135. return __stpncpy_alias (__dest, __src, __n);
  136. }
  137. # endif
  138. #endif
  139. __fortify_function __attribute_overloadable__ char *
  140. __NTH (strcat (__fortify_clang_overload_arg (char *, __restrict, __dest),
  141. const char *__restrict __src))
  142. __fortify_clang_warn_if_src_too_large (__dest, __src)
  143. {
  144. return __builtin___strcat_chk (__dest, __src, __glibc_objsize (__dest));
  145. }
  146. __fortify_function __attribute_overloadable__ char *
  147. __NTH (strncat (__fortify_clang_overload_arg (char *, __restrict, __dest),
  148. const char *__restrict __src, size_t __len))
  149. __fortify_clang_warn_if_src_too_large (__dest, __src)
  150. {
  151. return __builtin___strncat_chk (__dest, __src, __len,
  152. __glibc_objsize (__dest));
  153. }
  154. #ifdef __USE_MISC
  155. extern size_t __strlcpy_chk (char *__dest, const char *__src, size_t __n,
  156. size_t __destlen) __THROW;
  157. extern size_t __REDIRECT_NTH (__strlcpy_alias,
  158. (char *__dest, const char *__src, size_t __n),
  159. strlcpy);
  160. __fortify_function __attribute_overloadable__ size_t
  161. __NTH (strlcpy (__fortify_clang_overload_arg (char *, __restrict, __dest),
  162. const char *__restrict __src, size_t __n))
  163. __fortify_clang_warn_if_dest_too_small (__dest, __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 __strlcpy_chk (__dest, __src, __n, __glibc_objsize (__dest));
  169. return __strlcpy_alias (__dest, __src, __n);
  170. }
  171. extern size_t __strlcat_chk (char *__dest, const char *__src, size_t __n,
  172. size_t __destlen) __THROW;
  173. extern size_t __REDIRECT_NTH (__strlcat_alias,
  174. (char *__dest, const char *__src, size_t __n),
  175. strlcat);
  176. __fortify_function __attribute_overloadable__ size_t
  177. __NTH (strlcat (__fortify_clang_overload_arg (char *, __restrict, __dest),
  178. const char *__restrict __src, size_t __n))
  179. {
  180. if (__glibc_objsize (__dest) != (size_t) -1
  181. && (!__builtin_constant_p (__n > __glibc_objsize (__dest))
  182. || __n > __glibc_objsize (__dest)))
  183. return __strlcat_chk (__dest, __src, __n, __glibc_objsize (__dest));
  184. return __strlcat_alias (__dest, __src, __n);
  185. }
  186. #endif /* __USE_MISC */
  187. #endif /* bits/string_fortified.h */