George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #ifndef _STRING_H |
| 30 | #error "Never include this file directly; instead, include <string.h>" |
| 31 | #endif |
| 32 | |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 33 | void* _Nullable __memchr_chk(const void* _Nonnull, int, size_t, size_t) __INTRODUCED_IN(23); |
| 34 | void* _Nullable __memrchr_chk(const void* _Nonnull, int, size_t, size_t) __INTRODUCED_IN(23); |
Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 35 | char* _Nonnull __stpncpy_chk2(char* _Nonnull, const char* _Nonnull, size_t, size_t, size_t); |
| 36 | char* _Nonnull __strncpy_chk2(char* _Nonnull, const char* _Nonnull, size_t, size_t, size_t); |
| 37 | size_t __strlcpy_chk(char* _Nonnull, const char* _Nonnull, size_t, size_t); |
| 38 | size_t __strlcat_chk(char* _Nonnull, const char* _Nonnull, size_t, size_t); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 39 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 40 | #if defined(__BIONIC_FORTIFY) |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 41 | extern void* _Nullable __memrchr_real(const void* _Nonnull, int, size_t) __RENAME(memrchr); |
Elliott Hughes | df9a489 | 2017-08-23 14:34:03 -0700 | [diff] [blame] | 42 | |
Elliott Hughes | f4ace9d | 2023-02-23 17:38:37 +0000 | [diff] [blame] | 43 | #if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | 36926f4 | 2019-09-15 16:57:00 -0700 | [diff] [blame] | 44 | /* No diag -- clang diagnoses misuses of this on its own. */ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 45 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 46 | void* _Nonnull memcpy(void* _Nonnull const dst __pass_object_size0, const void* _Nonnull src, size_t copy_amount) |
Pirama Arumuga Nainar | db71def | 2022-01-06 15:46:55 -0800 | [diff] [blame] | 47 | __diagnose_as_builtin(__builtin_memcpy, 1, 2, 3) |
George Burgess IV | 36926f4 | 2019-09-15 16:57:00 -0700 | [diff] [blame] | 48 | __overloadable { |
George Burgess IV | 2f78765 | 2020-02-04 21:40:40 -0800 | [diff] [blame] | 49 | return __builtin___memcpy_chk(dst, src, copy_amount, __bos0(dst)); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 50 | } |
| 51 | |
George Burgess IV | 36926f4 | 2019-09-15 16:57:00 -0700 | [diff] [blame] | 52 | /* No diag -- clang diagnoses misuses of this on its own. */ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 53 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 54 | void* _Nonnull memmove(void* _Nonnull const dst __pass_object_size0, const void* _Nonnull src, size_t len) |
Pirama Arumuga Nainar | db71def | 2022-01-06 15:46:55 -0800 | [diff] [blame] | 55 | __diagnose_as_builtin(__builtin_memmove, 1, 2, 3) |
| 56 | __overloadable { |
George Burgess IV | 2f78765 | 2020-02-04 21:40:40 -0800 | [diff] [blame] | 57 | return __builtin___memmove_chk(dst, src, len, __bos0(dst)); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 58 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 59 | #endif |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 60 | |
George Burgess IV | 849c0b9 | 2019-06-10 16:22:09 -0700 | [diff] [blame] | 61 | #if defined(__USE_GNU) |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 62 | #if __ANDROID_API__ >= 30 |
George Burgess IV | 849c0b9 | 2019-06-10 16:22:09 -0700 | [diff] [blame] | 63 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 64 | void* _Nonnull mempcpy(void* _Nonnull const dst __pass_object_size0, const void* _Nonnull src, size_t copy_amount) |
Pirama Arumuga Nainar | db71def | 2022-01-06 15:46:55 -0800 | [diff] [blame] | 65 | __diagnose_as_builtin(__builtin_mempcpy, 1, 2, 3) |
George Burgess IV | 849c0b9 | 2019-06-10 16:22:09 -0700 | [diff] [blame] | 66 | __overloadable |
| 67 | __clang_error_if(__bos_unevaluated_lt(__bos0(dst), copy_amount), |
| 68 | "'mempcpy' called with size bigger than buffer") { |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 69 | #if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | 849c0b9 | 2019-06-10 16:22:09 -0700 | [diff] [blame] | 70 | size_t bos_dst = __bos0(dst); |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 71 | if (!__bos_trivially_ge(bos_dst, copy_amount)) { |
| 72 | return __builtin___mempcpy_chk(dst, src, copy_amount, bos_dst); |
George Burgess IV | 849c0b9 | 2019-06-10 16:22:09 -0700 | [diff] [blame] | 73 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 74 | #endif |
| 75 | return __builtin_mempcpy(dst, src, copy_amount); |
George Burgess IV | 849c0b9 | 2019-06-10 16:22:09 -0700 | [diff] [blame] | 76 | } |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 77 | #endif /* __ANDROID_API__ >= 30 */ |
George Burgess IV | fd1ff4b | 2019-09-18 17:29:55 -0700 | [diff] [blame] | 78 | #endif /* __USE_GNU */ |
George Burgess IV | 849c0b9 | 2019-06-10 16:22:09 -0700 | [diff] [blame] | 79 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 80 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 81 | char* _Nonnull stpcpy(char* _Nonnull const dst __pass_object_size, const char* _Nonnull src) |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 82 | __overloadable |
George Burgess IV | c03d596 | 2019-05-23 15:22:01 -0700 | [diff] [blame] | 83 | __clang_error_if(__bos_unevaluated_le(__bos(dst), __builtin_strlen(src)), |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 84 | "'stpcpy' called with string bigger than buffer") { |
Elliott Hughes | f4ace9d | 2023-02-23 17:38:37 +0000 | [diff] [blame] | 85 | #if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | 2f78765 | 2020-02-04 21:40:40 -0800 | [diff] [blame] | 86 | return __builtin___stpcpy_chk(dst, src, __bos(dst)); |
| 87 | #else |
George Burgess IV | fd1ff4b | 2019-09-18 17:29:55 -0700 | [diff] [blame] | 88 | return __builtin_stpcpy(dst, src); |
George Burgess IV | 2f78765 | 2020-02-04 21:40:40 -0800 | [diff] [blame] | 89 | #endif |
George Burgess IV | fd1ff4b | 2019-09-18 17:29:55 -0700 | [diff] [blame] | 90 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 91 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 92 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 93 | char* _Nonnull strcpy(char* _Nonnull const dst __pass_object_size, const char* _Nonnull src) |
Pirama Arumuga Nainar | db71def | 2022-01-06 15:46:55 -0800 | [diff] [blame] | 94 | __diagnose_as_builtin(__builtin_strcpy, 1, 2) |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 95 | __overloadable |
George Burgess IV | c03d596 | 2019-05-23 15:22:01 -0700 | [diff] [blame] | 96 | __clang_error_if(__bos_unevaluated_le(__bos(dst), __builtin_strlen(src)), |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 97 | "'strcpy' called with string bigger than buffer") { |
Elliott Hughes | f4ace9d | 2023-02-23 17:38:37 +0000 | [diff] [blame] | 98 | #if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | 2f78765 | 2020-02-04 21:40:40 -0800 | [diff] [blame] | 99 | return __builtin___strcpy_chk(dst, src, __bos(dst)); |
| 100 | #else |
George Burgess IV | fd1ff4b | 2019-09-18 17:29:55 -0700 | [diff] [blame] | 101 | return __builtin_strcpy(dst, src); |
George Burgess IV | 2f78765 | 2020-02-04 21:40:40 -0800 | [diff] [blame] | 102 | #endif |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 106 | char* _Nonnull strcat(char* _Nonnull const dst __pass_object_size, const char* _Nonnull src) |
George Burgess IV | 77f99aa | 2019-06-06 14:14:52 -0700 | [diff] [blame] | 107 | __overloadable |
| 108 | __clang_error_if(__bos_unevaluated_le(__bos(dst), __builtin_strlen(src)), |
| 109 | "'strcat' called with string bigger than buffer") { |
Elliott Hughes | f4ace9d | 2023-02-23 17:38:37 +0000 | [diff] [blame] | 110 | #if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 111 | return __builtin___strcat_chk(dst, src, __bos(dst)); |
George Burgess IV | fd1ff4b | 2019-09-18 17:29:55 -0700 | [diff] [blame] | 112 | #else |
| 113 | return __builtin_strcat(dst, src); |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 114 | #endif |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Elliott Hughes | f4ace9d | 2023-02-23 17:38:37 +0000 | [diff] [blame] | 117 | #if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | 36926f4 | 2019-09-15 16:57:00 -0700 | [diff] [blame] | 118 | /* No diag -- clang diagnoses misuses of this on its own. */ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 119 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 120 | char* _Nonnull strncat(char* _Nonnull const dst __pass_object_size, const char* _Nonnull src, size_t n) |
Pirama Arumuga Nainar | db71def | 2022-01-06 15:46:55 -0800 | [diff] [blame] | 121 | __diagnose_as_builtin(__builtin_strncat, 1, 2, 3) |
| 122 | __overloadable { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 123 | return __builtin___strncat_chk(dst, src, n, __bos(dst)); |
| 124 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 125 | #endif |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 126 | |
George Burgess IV | 36926f4 | 2019-09-15 16:57:00 -0700 | [diff] [blame] | 127 | /* No diag -- clang diagnoses misuses of this on its own. */ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 128 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 129 | void* _Nonnull memset(void* _Nonnull const s __pass_object_size0, int c, size_t n) __overloadable |
Pirama Arumuga Nainar | db71def | 2022-01-06 15:46:55 -0800 | [diff] [blame] | 130 | __diagnose_as_builtin(__builtin_memset, 1, 2, 3) |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 131 | /* If you're a user who wants this warning to go away: use `(&memset)(foo, bar, baz)`. */ |
| 132 | __clang_warning_if(c && !n, "'memset' will set 0 bytes; maybe the arguments got flipped?") { |
Elliott Hughes | f4ace9d | 2023-02-23 17:38:37 +0000 | [diff] [blame] | 133 | #if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | 2f78765 | 2020-02-04 21:40:40 -0800 | [diff] [blame] | 134 | return __builtin___memset_chk(s, c, n, __bos0(s)); |
| 135 | #else |
George Burgess IV | fd1ff4b | 2019-09-18 17:29:55 -0700 | [diff] [blame] | 136 | return __builtin_memset(s, c, n); |
George Burgess IV | 2f78765 | 2020-02-04 21:40:40 -0800 | [diff] [blame] | 137 | #endif |
George Burgess IV | fd1ff4b | 2019-09-18 17:29:55 -0700 | [diff] [blame] | 138 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 139 | |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 140 | #if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 141 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 142 | void* _Nullable memchr(const void* _Nonnull const s __pass_object_size, int c, size_t n) __overloadable { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 143 | size_t bos = __bos(s); |
| 144 | |
George Burgess IV | c03d596 | 2019-05-23 15:22:01 -0700 | [diff] [blame] | 145 | if (__bos_trivially_ge(bos, n)) { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 146 | return __builtin_memchr(s, c, n); |
| 147 | } |
| 148 | |
| 149 | return __memchr_chk(s, c, n, bos); |
| 150 | } |
| 151 | |
| 152 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 153 | void* _Nullable __memrchr_fortify(const void* _Nonnull const __pass_object_size s, int c, size_t n) __overloadable { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 154 | size_t bos = __bos(s); |
| 155 | |
George Burgess IV | c03d596 | 2019-05-23 15:22:01 -0700 | [diff] [blame] | 156 | if (__bos_trivially_ge(bos, n)) { |
Elliott Hughes | df9a489 | 2017-08-23 14:34:03 -0700 | [diff] [blame] | 157 | return __memrchr_real(s, c, n); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | return __memrchr_chk(s, c, n, bos); |
| 161 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 162 | #endif |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 163 | |
Elliott Hughes | f4ace9d | 2023-02-23 17:38:37 +0000 | [diff] [blame] | 164 | #if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | 36926f4 | 2019-09-15 16:57:00 -0700 | [diff] [blame] | 165 | /* No diag -- clang diagnoses misuses of this on its own. */ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 166 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 167 | char* _Nonnull stpncpy(char* _Nonnull const dst __pass_object_size, const char* _Nonnull const src __pass_object_size, size_t n) |
Pirama Arumuga Nainar | db71def | 2022-01-06 15:46:55 -0800 | [diff] [blame] | 168 | __diagnose_as_builtin(__builtin_stpncpy, 1, 2, 3) |
George Burgess IV | 36926f4 | 2019-09-15 16:57:00 -0700 | [diff] [blame] | 169 | __overloadable { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 170 | size_t bos_dst = __bos(dst); |
| 171 | size_t bos_src = __bos(src); |
| 172 | |
| 173 | /* Ignore dst size checks; they're handled in strncpy_chk */ |
| 174 | if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 175 | return __builtin___stpncpy_chk(dst, src, n, bos_dst); |
| 176 | } |
| 177 | |
| 178 | return __stpncpy_chk2(dst, src, n, bos_dst, bos_src); |
| 179 | } |
| 180 | |
George Burgess IV | 36926f4 | 2019-09-15 16:57:00 -0700 | [diff] [blame] | 181 | /* No diag -- clang diagnoses misuses of this on its own. */ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 182 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 183 | char* _Nonnull strncpy(char* _Nonnull const dst __pass_object_size, const char* _Nonnull const src __pass_object_size, size_t n) |
Pirama Arumuga Nainar | db71def | 2022-01-06 15:46:55 -0800 | [diff] [blame] | 184 | __diagnose_as_builtin(__builtin_strncpy, 1, 2, 3) |
George Burgess IV | 36926f4 | 2019-09-15 16:57:00 -0700 | [diff] [blame] | 185 | __overloadable { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 186 | size_t bos_dst = __bos(dst); |
| 187 | size_t bos_src = __bos(src); |
| 188 | |
| 189 | /* Ignore dst size checks; they're handled in strncpy_chk */ |
| 190 | if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 191 | return __builtin___strncpy_chk(dst, src, n, bos_dst); |
| 192 | } |
| 193 | |
| 194 | return __strncpy_chk2(dst, src, n, bos_dst, bos_src); |
| 195 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 196 | #endif |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 197 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 198 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 199 | size_t strlcpy(char* _Nonnull const dst __pass_object_size, const char* _Nonnull src, size_t size) |
George Burgess IV | 77f99aa | 2019-06-06 14:14:52 -0700 | [diff] [blame] | 200 | __overloadable |
| 201 | __clang_error_if(__bos_unevaluated_lt(__bos(dst), size), |
| 202 | "'strlcpy' called with size bigger than buffer") { |
Elliott Hughes | f4ace9d | 2023-02-23 17:38:37 +0000 | [diff] [blame] | 203 | #if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | 2f78765 | 2020-02-04 21:40:40 -0800 | [diff] [blame] | 204 | return __strlcpy_chk(dst, src, size, __bos(dst)); |
| 205 | #else |
George Burgess IV | fd1ff4b | 2019-09-18 17:29:55 -0700 | [diff] [blame] | 206 | return __call_bypassing_fortify(strlcpy)(dst, src, size); |
George Burgess IV | 2f78765 | 2020-02-04 21:40:40 -0800 | [diff] [blame] | 207 | #endif |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 211 | size_t strlcat(char* _Nonnull const dst __pass_object_size, const char* _Nonnull src, size_t size) |
George Burgess IV | 77f99aa | 2019-06-06 14:14:52 -0700 | [diff] [blame] | 212 | __overloadable |
| 213 | __clang_error_if(__bos_unevaluated_lt(__bos(dst), size), |
| 214 | "'strlcat' called with size bigger than buffer") { |
Elliott Hughes | f4ace9d | 2023-02-23 17:38:37 +0000 | [diff] [blame] | 215 | #if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | 2f78765 | 2020-02-04 21:40:40 -0800 | [diff] [blame] | 216 | return __strlcat_chk(dst, src, size, __bos(dst)); |
George Burgess IV | d02e7b1 | 2020-02-13 11:44:20 -0800 | [diff] [blame] | 217 | #else |
George Burgess IV | fd1ff4b | 2019-09-18 17:29:55 -0700 | [diff] [blame] | 218 | return __call_bypassing_fortify(strlcat)(dst, src, size); |
George Burgess IV | d02e7b1 | 2020-02-13 11:44:20 -0800 | [diff] [blame] | 219 | #endif |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Elliott Hughes | f4ace9d | 2023-02-23 17:38:37 +0000 | [diff] [blame] | 222 | #if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 223 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 224 | size_t strlen(const char* _Nonnull const s __pass_object_size0) __overloadable { |
George Burgess IV | d02e7b1 | 2020-02-13 11:44:20 -0800 | [diff] [blame] | 225 | return __strlen_chk(s, __bos0(s)); |
George Burgess IV | fd1ff4b | 2019-09-18 17:29:55 -0700 | [diff] [blame] | 226 | } |
George Burgess IV | d02e7b1 | 2020-02-13 11:44:20 -0800 | [diff] [blame] | 227 | #endif |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 228 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 229 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 230 | char* _Nullable strchr(const char* _Nonnull const s __pass_object_size, int c) __overloadable { |
Elliott Hughes | f4ace9d | 2023-02-23 17:38:37 +0000 | [diff] [blame] | 231 | #if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 232 | size_t bos = __bos(s); |
| 233 | |
George Burgess IV | fd1ff4b | 2019-09-18 17:29:55 -0700 | [diff] [blame] | 234 | if (bos != __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 235 | return __strchr_chk(s, c, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 236 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 237 | #endif |
George Burgess IV | fd1ff4b | 2019-09-18 17:29:55 -0700 | [diff] [blame] | 238 | return __builtin_strchr(s, c); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 242 | char* _Nullable strrchr(const char* _Nonnull const s __pass_object_size, int c) __overloadable { |
Elliott Hughes | f4ace9d | 2023-02-23 17:38:37 +0000 | [diff] [blame] | 243 | #if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 244 | size_t bos = __bos(s); |
| 245 | |
George Burgess IV | fd1ff4b | 2019-09-18 17:29:55 -0700 | [diff] [blame] | 246 | if (bos != __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 247 | return __strrchr_chk(s, c, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 248 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 249 | #endif |
George Burgess IV | fd1ff4b | 2019-09-18 17:29:55 -0700 | [diff] [blame] | 250 | return __builtin_strrchr(s, c); |
| 251 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 252 | |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 253 | #if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
Elliott Hughes | df9a489 | 2017-08-23 14:34:03 -0700 | [diff] [blame] | 254 | #if defined(__cplusplus) |
| 255 | extern "C++" { |
| 256 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 257 | void* _Nullable memrchr(void* _Nonnull const __pass_object_size s, int c, size_t n) { |
Elliott Hughes | df9a489 | 2017-08-23 14:34:03 -0700 | [diff] [blame] | 258 | return __memrchr_fortify(s, c, n); |
| 259 | } |
| 260 | |
| 261 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 262 | const void* _Nullable memrchr(const void* _Nonnull const __pass_object_size s, int c, size_t n) { |
Elliott Hughes | df9a489 | 2017-08-23 14:34:03 -0700 | [diff] [blame] | 263 | return __memrchr_fortify(s, c, n); |
| 264 | } |
| 265 | } |
| 266 | #else |
| 267 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 6d23a08 | 2023-06-09 21:34:24 +0000 | [diff] [blame] | 268 | void* _Nullable memrchr(const void* _Nonnull const __pass_object_size s, int c, size_t n) __overloadable { |
Elliott Hughes | df9a489 | 2017-08-23 14:34:03 -0700 | [diff] [blame] | 269 | return __memrchr_fortify(s, c, n); |
| 270 | } |
| 271 | #endif |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 272 | #endif /* __ANDROID_API__ >= 23 */ |
Elliott Hughes | df9a489 | 2017-08-23 14:34:03 -0700 | [diff] [blame] | 273 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 274 | #endif /* defined(__BIONIC_FORTIFY) */ |