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