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