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 | */ |
| 28 | #ifndef _STRING_H_ |
| 29 | #define _STRING_H_ |
| 30 | |
| 31 | #include <sys/cdefs.h> |
| 32 | #include <stddef.h> |
| 33 | #include <malloc.h> |
| 34 | |
| 35 | __BEGIN_DECLS |
| 36 | |
Nick Kralevich | 1c462b7 | 2013-05-07 10:00:21 -0700 | [diff] [blame] | 37 | extern void* memccpy(void* __restrict, const void* __restrict, int, size_t); |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 38 | extern void* memchr(const void *, int, size_t) __purefunc; |
| 39 | extern void* memrchr(const void *, int, size_t) __purefunc; |
| 40 | extern int memcmp(const void *, const void *, size_t) __purefunc; |
Nick Kralevich | 1c462b7 | 2013-05-07 10:00:21 -0700 | [diff] [blame] | 41 | extern void* memcpy(void* __restrict, const void* __restrict, size_t); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 42 | extern void* memmove(void *, const void *, size_t); |
| 43 | extern void* memset(void *, int, size_t); |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 44 | extern void* memmem(const void *, size_t, const void *, size_t) __purefunc; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 45 | extern void memswap(void *, void *, size_t); |
| 46 | |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 47 | extern char* index(const char *, int) __purefunc; |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 48 | extern char* strchr(const char *, int) __purefunc; |
Pavel Chupin | 3c4b50f | 2013-07-26 16:50:11 +0400 | [diff] [blame] | 49 | extern char* __strchr_chk(const char *, int, size_t); |
| 50 | |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 51 | extern char* strrchr(const char *, int) __purefunc; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 52 | |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 53 | extern size_t strlen(const char *) __purefunc; |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 54 | extern size_t __strlen_chk(const char *, size_t); |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 55 | extern int strcmp(const char *, const char *) __purefunc; |
Nick Kralevich | 1c462b7 | 2013-05-07 10:00:21 -0700 | [diff] [blame] | 56 | extern char* strcpy(char* __restrict, const char* __restrict); |
| 57 | extern char* strcat(char* __restrict, const char* __restrict); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 58 | |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 59 | extern int strcasecmp(const char *, const char *) __purefunc; |
| 60 | extern int strncasecmp(const char *, const char *, size_t) __purefunc; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 61 | extern char* strdup(const char *); |
| 62 | |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 63 | extern char* strstr(const char *, const char *) __purefunc; |
| 64 | extern char* strcasestr(const char *haystack, const char *needle) __purefunc; |
Nick Kralevich | 1c462b7 | 2013-05-07 10:00:21 -0700 | [diff] [blame] | 65 | extern char* strtok(char* __restrict, const char* __restrict); |
| 66 | extern char* strtok_r(char* __restrict, const char* __restrict, char** __restrict); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 67 | |
| 68 | extern char* strerror(int); |
| 69 | extern int strerror_r(int errnum, char *buf, size_t n); |
| 70 | |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 71 | extern size_t strnlen(const char *, size_t) __purefunc; |
Nick Kralevich | 1c462b7 | 2013-05-07 10:00:21 -0700 | [diff] [blame] | 72 | extern char* strncat(char* __restrict, const char* __restrict, size_t); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 73 | extern char* strndup(const char *, size_t); |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 74 | extern int strncmp(const char *, const char *, size_t) __purefunc; |
Nick Kralevich | 1c462b7 | 2013-05-07 10:00:21 -0700 | [diff] [blame] | 75 | extern char* strncpy(char* __restrict, const char* __restrict, size_t); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 76 | |
Nick Kralevich | 1c462b7 | 2013-05-07 10:00:21 -0700 | [diff] [blame] | 77 | extern size_t strlcat(char* __restrict, const char* __restrict, size_t); |
| 78 | extern size_t strlcpy(char* __restrict, const char* __restrict, size_t); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 79 | |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 80 | extern size_t strcspn(const char *, const char *) __purefunc; |
| 81 | extern char* strpbrk(const char *, const char *) __purefunc; |
Nick Kralevich | 1c462b7 | 2013-05-07 10:00:21 -0700 | [diff] [blame] | 82 | extern char* strsep(char** __restrict, const char* __restrict); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 83 | extern size_t strspn(const char *, const char *); |
| 84 | |
| 85 | extern char* strsignal(int sig); |
| 86 | |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 87 | extern int strcoll(const char *, const char *) __purefunc; |
Nick Kralevich | 1c462b7 | 2013-05-07 10:00:21 -0700 | [diff] [blame] | 88 | extern size_t strxfrm(char* __restrict, const char* __restrict, size_t); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 89 | |
Elliott Hughes | 890c8ed | 2013-03-22 10:58:55 -0700 | [diff] [blame] | 90 | #if defined(__BIONIC_FORTIFY) |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 91 | |
Nick Kralevich | b24c063 | 2013-06-18 10:46:02 -0700 | [diff] [blame] | 92 | __errordecl(__memcpy_dest_size_error, "memcpy called with size bigger than destination"); |
| 93 | __errordecl(__memcpy_src_size_error, "memcpy called with size bigger than source"); |
Nick Kralevich | f3913b5 | 2012-07-12 15:10:03 -0700 | [diff] [blame] | 94 | |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 95 | __BIONIC_FORTIFY_INLINE |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 96 | void* memcpy(void* __restrict dest, const void* __restrict src, size_t copy_amount) { |
Nick Kralevich | f3913b5 | 2012-07-12 15:10:03 -0700 | [diff] [blame] | 97 | char *d = (char *) dest; |
| 98 | const char *s = (const char *) src; |
Nick Kralevich | bd8e674 | 2013-08-28 13:22:52 -0700 | [diff] [blame] | 99 | size_t s_len = __bos0(s); |
| 100 | size_t d_len = __bos0(d); |
Nick Kralevich | f3913b5 | 2012-07-12 15:10:03 -0700 | [diff] [blame] | 101 | |
| 102 | if (__builtin_constant_p(copy_amount) && (copy_amount > d_len)) { |
| 103 | __memcpy_dest_size_error(); |
| 104 | } |
| 105 | |
| 106 | if (__builtin_constant_p(copy_amount) && (copy_amount > s_len)) { |
| 107 | __memcpy_src_size_error(); |
| 108 | } |
| 109 | |
Nick Kralevich | c37fc1a | 2012-07-13 17:49:10 -0700 | [diff] [blame] | 110 | return __builtin___memcpy_chk(dest, src, copy_amount, d_len); |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | __BIONIC_FORTIFY_INLINE |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 114 | void* memmove(void *dest, const void *src, size_t len) { |
Nick Kralevich | bd8e674 | 2013-08-28 13:22:52 -0700 | [diff] [blame] | 115 | return __builtin___memmove_chk(dest, src, len, __bos0(dest)); |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | __BIONIC_FORTIFY_INLINE |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 119 | char* strcpy(char* __restrict dest, const char* __restrict src) { |
Nick Kralevich | 9020fd5 | 2013-04-30 11:31:35 -0700 | [diff] [blame] | 120 | return __builtin___strcpy_chk(dest, src, __bos(dest)); |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Nick Kralevich | b24c063 | 2013-06-18 10:46:02 -0700 | [diff] [blame] | 123 | __errordecl(__strncpy_error, "strncpy called with size bigger than buffer"); |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 124 | extern char* __strncpy_chk2(char* __restrict, const char* __restrict, size_t, size_t, size_t); |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 125 | |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 126 | __BIONIC_FORTIFY_INLINE |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 127 | char* strncpy(char* __restrict dest, const char* __restrict src, size_t n) { |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 128 | size_t bos_dest = __bos(dest); |
| 129 | size_t bos_src = __bos(src); |
| 130 | if (__builtin_constant_p(n) && (n > bos_dest)) { |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 131 | __strncpy_error(); |
| 132 | } |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 133 | |
| 134 | if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 135 | return __builtin___strncpy_chk(dest, src, n, bos_dest); |
| 136 | } |
| 137 | |
Nick Kralevich | d13c2b1 | 2013-09-27 13:21:24 -0700 | [diff] [blame] | 138 | if (__builtin_constant_p(n) && (n <= bos_src)) { |
| 139 | return __builtin___strncpy_chk(dest, src, n, bos_dest); |
| 140 | } |
| 141 | |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 142 | size_t slen = __builtin_strlen(src); |
| 143 | if (__builtin_constant_p(slen)) { |
| 144 | return __builtin___strncpy_chk(dest, src, n, bos_dest); |
| 145 | } |
| 146 | |
| 147 | return __strncpy_chk2(dest, src, n, bos_dest, bos_src); |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | __BIONIC_FORTIFY_INLINE |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 151 | char* strcat(char* __restrict dest, const char* __restrict src) { |
Nick Kralevich | 9020fd5 | 2013-04-30 11:31:35 -0700 | [diff] [blame] | 152 | return __builtin___strcat_chk(dest, src, __bos(dest)); |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | __BIONIC_FORTIFY_INLINE |
Nick Kralevich | 1c462b7 | 2013-05-07 10:00:21 -0700 | [diff] [blame] | 156 | char *strncat(char* __restrict dest, const char* __restrict src, size_t n) { |
Nick Kralevich | 9020fd5 | 2013-04-30 11:31:35 -0700 | [diff] [blame] | 157 | return __builtin___strncat_chk(dest, src, n, __bos(dest)); |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Nick Kralevich | 71a18dd | 2012-06-07 14:01:26 -0700 | [diff] [blame] | 160 | __BIONIC_FORTIFY_INLINE |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 161 | void* memset(void *s, int c, size_t n) { |
Nick Kralevich | bd8e674 | 2013-08-28 13:22:52 -0700 | [diff] [blame] | 162 | return __builtin___memset_chk(s, c, n, __bos0(s)); |
Nick Kralevich | 71a18dd | 2012-06-07 14:01:26 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Nick Kralevich | 1c462b7 | 2013-05-07 10:00:21 -0700 | [diff] [blame] | 165 | extern size_t __strlcpy_real(char* __restrict, const char* __restrict, size_t) |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 166 | __asm__(__USER_LABEL_PREFIX__ "strlcpy"); |
Nick Kralevich | b24c063 | 2013-06-18 10:46:02 -0700 | [diff] [blame] | 167 | __errordecl(__strlcpy_error, "strlcpy called with size bigger than buffer"); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 168 | extern size_t __strlcpy_chk(char *, const char *, size_t, size_t); |
| 169 | |
| 170 | __BIONIC_FORTIFY_INLINE |
Nick Kralevich | 1c462b7 | 2013-05-07 10:00:21 -0700 | [diff] [blame] | 171 | size_t strlcpy(char* __restrict dest, const char* __restrict src, size_t size) { |
Nick Kralevich | 9020fd5 | 2013-04-30 11:31:35 -0700 | [diff] [blame] | 172 | size_t bos = __bos(dest); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 173 | |
Nick Kralevich | 8bafa74 | 2013-06-20 12:17:44 -0700 | [diff] [blame] | 174 | #if !defined(__clang__) |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 175 | // Compiler doesn't know destination size. Don't call __strlcpy_chk |
Nick Kralevich | 9b6cc22 | 2012-07-13 14:46:36 -0700 | [diff] [blame] | 176 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
Nick Kralevich | cb228fb | 2012-06-26 16:05:19 -0700 | [diff] [blame] | 177 | return __strlcpy_real(dest, src, size); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // Compiler can prove, at compile time, that the passed in size |
| 181 | // is always <= the actual object size. Don't call __strlcpy_chk |
| 182 | if (__builtin_constant_p(size) && (size <= bos)) { |
Nick Kralevich | cb228fb | 2012-06-26 16:05:19 -0700 | [diff] [blame] | 183 | return __strlcpy_real(dest, src, size); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // Compiler can prove, at compile time, that the passed in size |
| 187 | // is always > the actual object size. Force a compiler error. |
| 188 | if (__builtin_constant_p(size) && (size > bos)) { |
| 189 | __strlcpy_error(); |
| 190 | } |
Nick Kralevich | 8bafa74 | 2013-06-20 12:17:44 -0700 | [diff] [blame] | 191 | #endif /* !defined(__clang__) */ |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 192 | |
| 193 | return __strlcpy_chk(dest, src, size, bos); |
| 194 | } |
| 195 | |
Nick Kralevich | 1c462b7 | 2013-05-07 10:00:21 -0700 | [diff] [blame] | 196 | extern size_t __strlcat_real(char* __restrict, const char* __restrict, size_t) |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 197 | __asm__(__USER_LABEL_PREFIX__ "strlcat"); |
Nick Kralevich | b24c063 | 2013-06-18 10:46:02 -0700 | [diff] [blame] | 198 | __errordecl(__strlcat_error, "strlcat called with size bigger than buffer"); |
Nick Kralevich | 1c462b7 | 2013-05-07 10:00:21 -0700 | [diff] [blame] | 199 | extern size_t __strlcat_chk(char* __restrict, const char* __restrict, size_t, size_t); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 200 | |
| 201 | |
| 202 | __BIONIC_FORTIFY_INLINE |
Nick Kralevich | 1c462b7 | 2013-05-07 10:00:21 -0700 | [diff] [blame] | 203 | size_t strlcat(char* __restrict dest, const char* __restrict src, size_t size) { |
Nick Kralevich | 9020fd5 | 2013-04-30 11:31:35 -0700 | [diff] [blame] | 204 | size_t bos = __bos(dest); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 205 | |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 206 | #if !defined(__clang__) |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 207 | // Compiler doesn't know destination size. Don't call __strlcat_chk |
Nick Kralevich | 9b6cc22 | 2012-07-13 14:46:36 -0700 | [diff] [blame] | 208 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
Nick Kralevich | cb228fb | 2012-06-26 16:05:19 -0700 | [diff] [blame] | 209 | return __strlcat_real(dest, src, size); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // Compiler can prove, at compile time, that the passed in size |
| 213 | // is always <= the actual object size. Don't call __strlcat_chk |
| 214 | if (__builtin_constant_p(size) && (size <= bos)) { |
Nick Kralevich | cb228fb | 2012-06-26 16:05:19 -0700 | [diff] [blame] | 215 | return __strlcat_real(dest, src, size); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // Compiler can prove, at compile time, that the passed in size |
| 219 | // is always > the actual object size. Force a compiler error. |
| 220 | if (__builtin_constant_p(size) && (size > bos)) { |
| 221 | __strlcat_error(); |
| 222 | } |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 223 | #endif /* !defined(__clang__) */ |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 224 | |
| 225 | return __strlcat_chk(dest, src, size, bos); |
| 226 | } |
| 227 | |
Nick Kralevich | 260bf8c | 2012-07-13 11:27:06 -0700 | [diff] [blame] | 228 | __BIONIC_FORTIFY_INLINE |
| 229 | size_t strlen(const char *s) { |
Nick Kralevich | 9020fd5 | 2013-04-30 11:31:35 -0700 | [diff] [blame] | 230 | size_t bos = __bos(s); |
Nick Kralevich | 9b6cc22 | 2012-07-13 14:46:36 -0700 | [diff] [blame] | 231 | |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 232 | #if !defined(__clang__) |
Nick Kralevich | 9b6cc22 | 2012-07-13 14:46:36 -0700 | [diff] [blame] | 233 | // Compiler doesn't know destination size. Don't call __strlen_chk |
| 234 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
Nick Kralevich | a44e9af | 2013-01-17 15:41:33 -0800 | [diff] [blame] | 235 | return __builtin_strlen(s); |
| 236 | } |
| 237 | |
| 238 | size_t slen = __builtin_strlen(s); |
| 239 | if (__builtin_constant_p(slen)) { |
| 240 | return slen; |
Nick Kralevich | 260bf8c | 2012-07-13 11:27:06 -0700 | [diff] [blame] | 241 | } |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 242 | #endif /* !defined(__clang__) */ |
Nick Kralevich | 9b6cc22 | 2012-07-13 14:46:36 -0700 | [diff] [blame] | 243 | |
Nick Kralevich | 260bf8c | 2012-07-13 11:27:06 -0700 | [diff] [blame] | 244 | return __strlen_chk(s, bos); |
| 245 | } |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 246 | |
Nick Kralevich | 049e583 | 2012-11-30 15:15:58 -0800 | [diff] [blame] | 247 | __BIONIC_FORTIFY_INLINE |
| 248 | char* strchr(const char *s, int c) { |
Nick Kralevich | 9020fd5 | 2013-04-30 11:31:35 -0700 | [diff] [blame] | 249 | size_t bos = __bos(s); |
Nick Kralevich | 049e583 | 2012-11-30 15:15:58 -0800 | [diff] [blame] | 250 | |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 251 | #if !defined(__clang__) |
Nick Kralevich | 049e583 | 2012-11-30 15:15:58 -0800 | [diff] [blame] | 252 | // Compiler doesn't know destination size. Don't call __strchr_chk |
| 253 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
Nick Kralevich | a44e9af | 2013-01-17 15:41:33 -0800 | [diff] [blame] | 254 | return __builtin_strchr(s, c); |
| 255 | } |
| 256 | |
| 257 | size_t slen = __builtin_strlen(s); |
| 258 | if (__builtin_constant_p(slen) && (slen < bos)) { |
| 259 | return __builtin_strchr(s, c); |
Nick Kralevich | 049e583 | 2012-11-30 15:15:58 -0800 | [diff] [blame] | 260 | } |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 261 | #endif /* !defined(__clang__) */ |
Nick Kralevich | 049e583 | 2012-11-30 15:15:58 -0800 | [diff] [blame] | 262 | |
| 263 | return __strchr_chk(s, c, bos); |
| 264 | } |
| 265 | |
Nick Kralevich | 9a4d305 | 2012-12-03 10:36:13 -0800 | [diff] [blame] | 266 | extern char* __strrchr_chk(const char *, int, size_t); |
| 267 | |
| 268 | __BIONIC_FORTIFY_INLINE |
| 269 | char* strrchr(const char *s, int c) { |
Nick Kralevich | 3b2e6bc | 2013-04-30 14:19:23 -0700 | [diff] [blame] | 270 | size_t bos = __bos(s); |
Nick Kralevich | 9a4d305 | 2012-12-03 10:36:13 -0800 | [diff] [blame] | 271 | |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 272 | #if !defined(__clang__) |
Nick Kralevich | 9a4d305 | 2012-12-03 10:36:13 -0800 | [diff] [blame] | 273 | // Compiler doesn't know destination size. Don't call __strrchr_chk |
| 274 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
Nick Kralevich | a44e9af | 2013-01-17 15:41:33 -0800 | [diff] [blame] | 275 | return __builtin_strrchr(s, c); |
| 276 | } |
| 277 | |
| 278 | size_t slen = __builtin_strlen(s); |
| 279 | if (__builtin_constant_p(slen) && (slen < bos)) { |
| 280 | return __builtin_strrchr(s, c); |
Nick Kralevich | 9a4d305 | 2012-12-03 10:36:13 -0800 | [diff] [blame] | 281 | } |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 282 | #endif /* !defined(__clang__) */ |
Nick Kralevich | 9a4d305 | 2012-12-03 10:36:13 -0800 | [diff] [blame] | 283 | |
| 284 | return __strrchr_chk(s, c, bos); |
| 285 | } |
| 286 | |
Nick Kralevich | 049e583 | 2012-11-30 15:15:58 -0800 | [diff] [blame] | 287 | |
Elliott Hughes | 890c8ed | 2013-03-22 10:58:55 -0700 | [diff] [blame] | 288 | #endif /* defined(__BIONIC_FORTIFY) */ |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 289 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 290 | __END_DECLS |
| 291 | |
| 292 | #endif /* _STRING_H_ */ |