The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | */ |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 28 | |
| 29 | #ifndef _STRING_H |
| 30 | #define _STRING_H |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 31 | |
| 32 | #include <sys/cdefs.h> |
| 33 | #include <stddef.h> |
Dan Albert | dfb5ce4 | 2014-07-09 22:51:34 +0000 | [diff] [blame] | 34 | #include <xlocale.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 | |
Josh Gao | c3cec27 | 2016-04-07 13:39:49 -0700 | [diff] [blame] | 36 | #include <bits/strcasecmp.h> |
| 37 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 38 | __BEGIN_DECLS |
| 39 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 40 | #if defined(__clang__) |
| 41 | #pragma clang diagnostic push |
| 42 | #pragma clang diagnostic ignored "-Wnullability-completeness" |
| 43 | #endif |
| 44 | |
Elliott Hughes | 76f8916 | 2015-01-26 13:34:58 -0800 | [diff] [blame] | 45 | #if defined(__USE_BSD) |
| 46 | #include <strings.h> |
| 47 | #endif |
| 48 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 49 | void* memccpy(void* _Nonnull __restrict, const void* _Nonnull __restrict, int, size_t); |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 50 | void* memchr(const void* _Nonnull, int, size_t) __attribute_pure__; |
| 51 | void* memrchr(const void* _Nonnull, int, size_t) __attribute_pure__; |
| 52 | int memcmp(const void* _Nonnull, const void* _Nonnull, size_t) __attribute_pure__; |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 53 | void* memcpy(void* _Nonnull __restrict, const void* _Nonnull __restrict, size_t); |
Elliott Hughes | 3cfb52a | 2015-02-18 21:29:13 -0800 | [diff] [blame] | 54 | #if defined(__USE_GNU) |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 55 | void* mempcpy(void* _Nonnull __restrict, const void* _Nonnull __restrict, size_t) __INTRODUCED_IN(23); |
Elliott Hughes | 3cfb52a | 2015-02-18 21:29:13 -0800 | [diff] [blame] | 56 | #endif |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 57 | void* memmove(void* _Nonnull, const void* _Nonnull, size_t); |
| 58 | void* memset(void* _Nonnull, int, size_t); |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 59 | void* memmem(const void* _Nonnull, size_t, const void* _Nonnull, size_t) __attribute_pure__; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 60 | |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 61 | char* strchr(const char* _Nonnull, int) __attribute_pure__; |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 62 | char* __strchr_chk(const char* _Nonnull, int, size_t) __INTRODUCED_IN(18); |
Elliott Hughes | 7ac3c12 | 2015-08-26 09:59:29 -0700 | [diff] [blame] | 63 | #if defined(__USE_GNU) |
| 64 | #if defined(__cplusplus) |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 65 | extern "C++" char* strchrnul(char* _Nonnull, int) __RENAME(strchrnul) __attribute_pure__; |
| 66 | extern "C++" const char* strchrnul(const char* _Nonnull, int) __RENAME(strchrnul) __attribute_pure__; |
Elliott Hughes | 7ac3c12 | 2015-08-26 09:59:29 -0700 | [diff] [blame] | 67 | #else |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 68 | char* strchrnul(const char* _Nonnull, int) __attribute_pure__ __INTRODUCED_IN(24); |
Elliott Hughes | 7ac3c12 | 2015-08-26 09:59:29 -0700 | [diff] [blame] | 69 | #endif |
| 70 | #endif |
Pavel Chupin | 3c4b50f | 2013-07-26 16:50:11 +0400 | [diff] [blame] | 71 | |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 72 | char* strrchr(const char* _Nonnull, int) __attribute_pure__; |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 73 | char* __strrchr_chk(const char* _Nonnull, int, size_t) __INTRODUCED_IN(18); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 74 | |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 75 | size_t strlen(const char* _Nonnull) __attribute_pure__; |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 76 | size_t __strlen_chk(const char* _Nonnull, size_t) __INTRODUCED_IN(17); |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 77 | int strcmp(const char* _Nonnull, const char* _Nonnull) __attribute_pure__; |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 78 | char* stpcpy(char* _Nonnull __restrict, const char* _Nonnull__restrict) __INTRODUCED_IN(21); |
| 79 | char* strcpy(char* _Nonnull __restrict, const char* _Nonnull __restrict); |
| 80 | char* strcat(char* _Nonnull __restrict, const char* _Nonnull __restrict); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 81 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 82 | char* strdup(const char* _Nonnull); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 83 | |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 84 | char* strstr(const char* _Nonnull, const char* _Nonnull) __attribute_pure__; |
| 85 | char* strcasestr(const char* _Nonnull, const char* _Nonnull) __attribute_pure__; |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 86 | char* strtok(char* __restrict, const char* _Nonnull __restrict); |
| 87 | char* strtok_r(char* __restrict, const char* _Nonnull __restrict, char** _Nonnull __restrict); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 88 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 89 | char* strerror(int); |
| 90 | char* strerror_l(int, locale_t) __INTRODUCED_IN(23); |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 91 | #if defined(__USE_GNU) |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 92 | char* strerror_r(int, char*, size_t) __RENAME(__gnu_strerror_r) __INTRODUCED_IN(23); |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 93 | #else /* POSIX */ |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 94 | int strerror_r(int, char*, size_t); |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 95 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 96 | |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 97 | size_t strnlen(const char* _Nonnull, size_t) __attribute_pure__; |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 98 | char* strncat(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t); |
| 99 | char* strndup(const char* _Nonnull, size_t); |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 100 | int strncmp(const char* _Nonnull, const char* _Nonnull, size_t) __attribute_pure__; |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 101 | char* stpncpy(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t) __INTRODUCED_IN(21); |
| 102 | char* strncpy(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 103 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 104 | size_t strlcat(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t); |
| 105 | size_t strlcpy(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 106 | |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 107 | size_t strcspn(const char* _Nonnull, const char* _Nonnull) __attribute_pure__; |
| 108 | char* strpbrk(const char* _Nonnull, const char* _Nonnull) __attribute_pure__; |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 109 | char* strsep(char** _Nonnull __restrict, const char* _Nonnull __restrict); |
| 110 | size_t strspn(const char* _Nonnull, const char* _Nonnull); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 111 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 112 | char* strsignal(int); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 113 | |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 114 | int strcoll(const char* _Nonnull, const char* _Nonnull) __attribute_pure__; |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 115 | size_t strxfrm(char* __restrict, const char* _Nonnull __restrict, size_t); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 116 | |
Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 117 | #if __ANDROID_API__ >= __ANDROID_API_L__ |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 118 | int strcoll_l(const char* _Nonnull, const char* _Nonnull, locale_t) __attribute_pure__ __INTRODUCED_IN(21); |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 119 | size_t strxfrm_l(char* __restrict, const char* _Nonnull __restrict, size_t, locale_t) __INTRODUCED_IN(21); |
Josh Gao | 6cd9fb0 | 2016-09-23 14:06:05 -0700 | [diff] [blame] | 120 | #else |
| 121 | // Implemented as static inlines before 21. |
| 122 | #endif |
Dan Albert | dfb5ce4 | 2014-07-09 22:51:34 +0000 | [diff] [blame] | 123 | |
Josh Gao | eb9b925 | 2015-11-03 18:46:02 -0800 | [diff] [blame] | 124 | #if defined(__USE_GNU) && !defined(basename) |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 125 | /* |
| 126 | * glibc has a basename in <string.h> that's different to the POSIX one in <libgen.h>. |
| 127 | * It doesn't modify its argument, and in C++ it's const-correct. |
| 128 | */ |
| 129 | #if defined(__cplusplus) |
Dan Albert | baa2a97 | 2015-08-13 16:58:50 -0700 | [diff] [blame] | 130 | extern "C++" char* basename(char* _Nonnull) __RENAME(__gnu_basename); |
| 131 | extern "C++" const char* basename(const char* _Nonnull) __RENAME(__gnu_basename); |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 132 | #else |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 133 | char* basename(const char* _Nonnull) __RENAME(__gnu_basename) __INTRODUCED_IN(23); |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 134 | #endif |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 135 | #endif |
| 136 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 137 | void* __memchr_chk(const void* _Nonnull, int, size_t, size_t) __INTRODUCED_IN(23); |
Daniel Micay | 4ae7736 | 2015-04-17 18:16:57 -0400 | [diff] [blame] | 138 | __errordecl(__memchr_buf_size_error, "memchr called with size bigger than buffer"); |
| 139 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 140 | void* __memrchr_chk(const void* _Nonnull, int, size_t, size_t) __INTRODUCED_IN(23); |
Daniel Micay | 4ae7736 | 2015-04-17 18:16:57 -0400 | [diff] [blame] | 141 | __errordecl(__memrchr_buf_size_error, "memrchr called with size bigger than buffer"); |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 142 | void* __memrchr_real(const void* _Nonnull, int, size_t) __RENAME(memrchr); |
Daniel Micay | 4ae7736 | 2015-04-17 18:16:57 -0400 | [diff] [blame] | 143 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 144 | char* __stpncpy_chk2(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t, size_t, size_t) |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 145 | __INTRODUCED_IN(21); |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 146 | char* __strncpy_chk2(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t, size_t, size_t) |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 147 | __INTRODUCED_IN(21); |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 148 | size_t __strlcpy_real(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t) __RENAME(strlcpy); |
| 149 | size_t __strlcpy_chk(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t, size_t) __INTRODUCED_IN(17); |
| 150 | size_t __strlcat_real(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t) __RENAME(strlcat); |
| 151 | size_t __strlcat_chk(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t, size_t) __INTRODUCED_IN(17); |
Dan Albert | 658727e | 2014-10-07 11:10:36 -0700 | [diff] [blame] | 152 | |
Elliott Hughes | 890c8ed | 2013-03-22 10:58:55 -0700 | [diff] [blame] | 153 | #if defined(__BIONIC_FORTIFY) |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 154 | |
Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 155 | #if __ANDROID_API__ >= __ANDROID_API_M__ |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 156 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 157 | void* memchr(const void* s, int c, size_t n) { |
Daniel Micay | 4ae7736 | 2015-04-17 18:16:57 -0400 | [diff] [blame] | 158 | size_t bos = __bos(s); |
| 159 | |
| 160 | #if !defined(__clang__) |
| 161 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 162 | return __builtin_memchr(s, c, n); |
| 163 | } |
| 164 | |
| 165 | if (__builtin_constant_p(n) && (n > bos)) { |
| 166 | __memchr_buf_size_error(); |
| 167 | } |
| 168 | |
| 169 | if (__builtin_constant_p(n) && (n <= bos)) { |
| 170 | return __builtin_memchr(s, c, n); |
| 171 | } |
| 172 | #endif |
| 173 | |
| 174 | return __memchr_chk(s, c, n, bos); |
| 175 | } |
| 176 | |
| 177 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 178 | void* memrchr(const void* s, int c, size_t n) { |
Daniel Micay | 4ae7736 | 2015-04-17 18:16:57 -0400 | [diff] [blame] | 179 | size_t bos = __bos(s); |
| 180 | |
| 181 | #if !defined(__clang__) |
| 182 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 183 | return __memrchr_real(s, c, n); |
| 184 | } |
| 185 | |
| 186 | if (__builtin_constant_p(n) && (n > bos)) { |
| 187 | __memrchr_buf_size_error(); |
| 188 | } |
| 189 | |
| 190 | if (__builtin_constant_p(n) && (n <= bos)) { |
| 191 | return __memrchr_real(s, c, n); |
| 192 | } |
| 193 | #endif |
| 194 | |
| 195 | return __memrchr_chk(s, c, n, bos); |
| 196 | } |
Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 197 | #endif /* __ANDROID_API__ >= __ANDROID_API_M__ */ |
Daniel Micay | 4ae7736 | 2015-04-17 18:16:57 -0400 | [diff] [blame] | 198 | |
Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 199 | #if __ANDROID_API__ >= __ANDROID_API_J_MR1__ |
Daniel Micay | 4ae7736 | 2015-04-17 18:16:57 -0400 | [diff] [blame] | 200 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 201 | void* memcpy(void* _Nonnull __restrict dst, const void* _Nonnull __restrict src, size_t copy_amount) { |
| 202 | return __builtin___memcpy_chk(dst, src, copy_amount, __bos0(dst)); |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 206 | void* memmove(void* _Nonnull dst, const void* _Nonnull src, size_t len) { |
| 207 | return __builtin___memmove_chk(dst, src, len, __bos0(dst)); |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 208 | } |
Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 209 | #endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */ |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 210 | |
Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 211 | #if __ANDROID_API__ >= __ANDROID_API_L__ |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 212 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 213 | char* stpcpy(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src) { |
| 214 | return __builtin___stpcpy_chk(dst, src, __bos(dst)); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 215 | } |
Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 216 | #endif /* __ANDROID_API__ >= __ANDROID_API_L__ */ |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 217 | |
Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 218 | #if __ANDROID_API__ >= __ANDROID_API_J_MR1__ |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 219 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 220 | char* strcpy(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src) { |
| 221 | return __builtin___strcpy_chk(dst, src, __bos(dst)); |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 222 | } |
Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 223 | #endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */ |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 224 | |
Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 225 | #if __ANDROID_API__ >= __ANDROID_API_L__ |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 226 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 227 | char* stpncpy(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src, size_t n) { |
| 228 | size_t bos_dst = __bos(dst); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 229 | size_t bos_src = __bos(src); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 230 | |
| 231 | if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 232 | return __builtin___stpncpy_chk(dst, src, n, bos_dst); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | if (__builtin_constant_p(n) && (n <= bos_src)) { |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 236 | return __builtin___stpncpy_chk(dst, src, n, bos_dst); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | size_t slen = __builtin_strlen(src); |
| 240 | if (__builtin_constant_p(slen)) { |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 241 | return __builtin___stpncpy_chk(dst, src, n, bos_dst); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 244 | return __stpncpy_chk2(dst, src, n, bos_dst, bos_src); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 247 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 248 | char* strncpy(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src, size_t n) { |
| 249 | size_t bos_dst = __bos(dst); |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 250 | size_t bos_src = __bos(src); |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 251 | |
| 252 | if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 253 | return __builtin___strncpy_chk(dst, src, n, bos_dst); |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Nick Kralevich | d13c2b1 | 2013-09-27 13:21:24 -0700 | [diff] [blame] | 256 | if (__builtin_constant_p(n) && (n <= bos_src)) { |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 257 | return __builtin___strncpy_chk(dst, src, n, bos_dst); |
Nick Kralevich | d13c2b1 | 2013-09-27 13:21:24 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 260 | size_t slen = __builtin_strlen(src); |
| 261 | if (__builtin_constant_p(slen)) { |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 262 | return __builtin___strncpy_chk(dst, src, n, bos_dst); |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 265 | return __strncpy_chk2(dst, src, n, bos_dst, bos_src); |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 266 | } |
Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 267 | #endif /* __ANDROID_API__ >= __ANDROID_API_L__ */ |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 268 | |
Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 269 | #if __ANDROID_API__ >= __ANDROID_API_J_MR1__ |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 270 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 271 | char* strcat(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src) { |
| 272 | return __builtin___strcat_chk(dst, src, __bos(dst)); |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 276 | char *strncat(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src, size_t n) { |
| 277 | return __builtin___strncat_chk(dst, src, n, __bos(dst)); |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Nick Kralevich | 71a18dd | 2012-06-07 14:01:26 -0700 | [diff] [blame] | 280 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 281 | void* memset(void* _Nonnull s, int c, size_t n) { |
Nick Kralevich | bd8e674 | 2013-08-28 13:22:52 -0700 | [diff] [blame] | 282 | return __builtin___memset_chk(s, c, n, __bos0(s)); |
Nick Kralevich | 71a18dd | 2012-06-07 14:01:26 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 285 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 286 | size_t strlcpy(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src, size_t size) { |
| 287 | size_t bos = __bos(dst); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 288 | |
Nick Kralevich | 8bafa74 | 2013-06-20 12:17:44 -0700 | [diff] [blame] | 289 | #if !defined(__clang__) |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 290 | // Compiler doesn't know destination size. Don't call __strlcpy_chk |
Nick Kralevich | 9b6cc22 | 2012-07-13 14:46:36 -0700 | [diff] [blame] | 291 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 292 | return __strlcpy_real(dst, src, size); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | // Compiler can prove, at compile time, that the passed in size |
| 296 | // is always <= the actual object size. Don't call __strlcpy_chk |
| 297 | if (__builtin_constant_p(size) && (size <= bos)) { |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 298 | return __strlcpy_real(dst, src, size); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 299 | } |
Nick Kralevich | 8bafa74 | 2013-06-20 12:17:44 -0700 | [diff] [blame] | 300 | #endif /* !defined(__clang__) */ |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 301 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 302 | return __strlcpy_chk(dst, src, size, bos); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 305 | |
| 306 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 307 | size_t strlcat(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src, size_t size) { |
| 308 | size_t bos = __bos(dst); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 309 | |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 310 | #if !defined(__clang__) |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 311 | // Compiler doesn't know destination size. Don't call __strlcat_chk |
Nick Kralevich | 9b6cc22 | 2012-07-13 14:46:36 -0700 | [diff] [blame] | 312 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 313 | return __strlcat_real(dst, src, size); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // Compiler can prove, at compile time, that the passed in size |
| 317 | // is always <= the actual object size. Don't call __strlcat_chk |
| 318 | if (__builtin_constant_p(size) && (size <= bos)) { |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 319 | return __strlcat_real(dst, src, size); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 320 | } |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 321 | #endif /* !defined(__clang__) */ |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 322 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 323 | return __strlcat_chk(dst, src, size, bos); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Nick Kralevich | 260bf8c | 2012-07-13 11:27:06 -0700 | [diff] [blame] | 326 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 327 | size_t strlen(const char* _Nonnull s) { |
Nick Kralevich | 9020fd5 | 2013-04-30 11:31:35 -0700 | [diff] [blame] | 328 | size_t bos = __bos(s); |
Nick Kralevich | 9b6cc22 | 2012-07-13 14:46:36 -0700 | [diff] [blame] | 329 | |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 330 | #if !defined(__clang__) |
Nick Kralevich | 9b6cc22 | 2012-07-13 14:46:36 -0700 | [diff] [blame] | 331 | // Compiler doesn't know destination size. Don't call __strlen_chk |
| 332 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
Nick Kralevich | a44e9af | 2013-01-17 15:41:33 -0800 | [diff] [blame] | 333 | return __builtin_strlen(s); |
| 334 | } |
| 335 | |
| 336 | size_t slen = __builtin_strlen(s); |
| 337 | if (__builtin_constant_p(slen)) { |
| 338 | return slen; |
Nick Kralevich | 260bf8c | 2012-07-13 11:27:06 -0700 | [diff] [blame] | 339 | } |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 340 | #endif /* !defined(__clang__) */ |
Nick Kralevich | 9b6cc22 | 2012-07-13 14:46:36 -0700 | [diff] [blame] | 341 | |
Nick Kralevich | 260bf8c | 2012-07-13 11:27:06 -0700 | [diff] [blame] | 342 | return __strlen_chk(s, bos); |
| 343 | } |
Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 344 | #endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */ |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 345 | |
Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 346 | #if __ANDROID_API__ >= __ANDROID_API_J_MR2__ |
Nick Kralevich | 049e583 | 2012-11-30 15:15:58 -0800 | [diff] [blame] | 347 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 348 | char* strchr(const char* _Nonnull s, int c) { |
Nick Kralevich | 9020fd5 | 2013-04-30 11:31:35 -0700 | [diff] [blame] | 349 | size_t bos = __bos(s); |
Nick Kralevich | 049e583 | 2012-11-30 15:15:58 -0800 | [diff] [blame] | 350 | |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 351 | #if !defined(__clang__) |
Nick Kralevich | 049e583 | 2012-11-30 15:15:58 -0800 | [diff] [blame] | 352 | // Compiler doesn't know destination size. Don't call __strchr_chk |
| 353 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
Nick Kralevich | a44e9af | 2013-01-17 15:41:33 -0800 | [diff] [blame] | 354 | return __builtin_strchr(s, c); |
| 355 | } |
| 356 | |
| 357 | size_t slen = __builtin_strlen(s); |
| 358 | if (__builtin_constant_p(slen) && (slen < bos)) { |
| 359 | return __builtin_strchr(s, c); |
Nick Kralevich | 049e583 | 2012-11-30 15:15:58 -0800 | [diff] [blame] | 360 | } |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 361 | #endif /* !defined(__clang__) */ |
Nick Kralevich | 049e583 | 2012-11-30 15:15:58 -0800 | [diff] [blame] | 362 | |
| 363 | return __strchr_chk(s, c, bos); |
| 364 | } |
| 365 | |
Nick Kralevich | 9a4d305 | 2012-12-03 10:36:13 -0800 | [diff] [blame] | 366 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 367 | char* strrchr(const char* _Nonnull s, int c) { |
Nick Kralevich | 3b2e6bc | 2013-04-30 14:19:23 -0700 | [diff] [blame] | 368 | size_t bos = __bos(s); |
Nick Kralevich | 9a4d305 | 2012-12-03 10:36:13 -0800 | [diff] [blame] | 369 | |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 370 | #if !defined(__clang__) |
Nick Kralevich | 9a4d305 | 2012-12-03 10:36:13 -0800 | [diff] [blame] | 371 | // Compiler doesn't know destination size. Don't call __strrchr_chk |
| 372 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
Nick Kralevich | a44e9af | 2013-01-17 15:41:33 -0800 | [diff] [blame] | 373 | return __builtin_strrchr(s, c); |
| 374 | } |
| 375 | |
| 376 | size_t slen = __builtin_strlen(s); |
| 377 | if (__builtin_constant_p(slen) && (slen < bos)) { |
| 378 | return __builtin_strrchr(s, c); |
Nick Kralevich | 9a4d305 | 2012-12-03 10:36:13 -0800 | [diff] [blame] | 379 | } |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 380 | #endif /* !defined(__clang__) */ |
Nick Kralevich | 9a4d305 | 2012-12-03 10:36:13 -0800 | [diff] [blame] | 381 | |
| 382 | return __strrchr_chk(s, c, bos); |
| 383 | } |
Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 384 | #endif /* __ANDROID_API__ >= __ANDROID_API_J_MR2__ */ |
Nick Kralevich | 049e583 | 2012-11-30 15:15:58 -0800 | [diff] [blame] | 385 | |
Elliott Hughes | 890c8ed | 2013-03-22 10:58:55 -0700 | [diff] [blame] | 386 | #endif /* defined(__BIONIC_FORTIFY) */ |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 387 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 388 | #if defined(__clang__) |
| 389 | #pragma clang diagnostic pop |
| 390 | #endif |
| 391 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 392 | __END_DECLS |
| 393 | |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 394 | #endif /* _STRING_H */ |