blob: 3f98af1ea4cbeb585b0d510d649f56ebda514bb4 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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 Hughes09c39d62014-08-19 14:30:30 -070028
29#ifndef _STRING_H
30#define _STRING_H
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080031
32#include <sys/cdefs.h>
33#include <stddef.h>
Dan Albertdfb5ce42014-07-09 22:51:34 +000034#include <xlocale.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035
Josh Gaoc3cec272016-04-07 13:39:49 -070036#include <bits/strcasecmp.h>
37
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080038__BEGIN_DECLS
39
Elliott Hughes76f89162015-01-26 13:34:58 -080040#if defined(__USE_BSD)
41#include <strings.h>
42#endif
43
Nick Kralevich1c462b72013-05-07 10:00:21 -070044extern void* memccpy(void* __restrict, const void* __restrict, int, size_t);
Nick Kralevicha6779072012-03-21 08:48:18 -070045extern void* memchr(const void *, int, size_t) __purefunc;
46extern void* memrchr(const void *, int, size_t) __purefunc;
47extern int memcmp(const void *, const void *, size_t) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070048extern void* memcpy(void* __restrict, const void* __restrict, size_t);
Elliott Hughes3cfb52a2015-02-18 21:29:13 -080049#if defined(__USE_GNU)
Josh Gao14adff12016-04-29 12:00:55 -070050extern void* mempcpy(void* __restrict, const void* __restrict, size_t) __INTRODUCED_IN(23);
Elliott Hughes3cfb52a2015-02-18 21:29:13 -080051#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052extern void* memmove(void *, const void *, size_t);
53extern void* memset(void *, int, size_t);
Nick Kralevicha6779072012-03-21 08:48:18 -070054extern void* memmem(const void *, size_t, const void *, size_t) __purefunc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080055
Nick Kralevicha6779072012-03-21 08:48:18 -070056extern char* strchr(const char *, int) __purefunc;
Josh Gao14adff12016-04-29 12:00:55 -070057extern char* __strchr_chk(const char*, int, size_t) __INTRODUCED_IN(21);
Elliott Hughes7ac3c122015-08-26 09:59:29 -070058#if defined(__USE_GNU)
59#if defined(__cplusplus)
60extern "C++" char* strchrnul(char*, int) __RENAME(strchrnul) __purefunc;
61extern "C++" const char* strchrnul(const char*, int) __RENAME(strchrnul) __purefunc;
62#else
Josh Gao14adff12016-04-29 12:00:55 -070063char* strchrnul(const char*, int) __purefunc __INTRODUCED_IN(24);
Elliott Hughes7ac3c122015-08-26 09:59:29 -070064#endif
65#endif
Pavel Chupin3c4b50f2013-07-26 16:50:11 +040066
Nick Kralevicha6779072012-03-21 08:48:18 -070067extern char* strrchr(const char *, int) __purefunc;
Josh Gao14adff12016-04-29 12:00:55 -070068extern char* __strrchr_chk(const char*, int, size_t) __INTRODUCED_IN(21);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080069
Nick Kralevicha6779072012-03-21 08:48:18 -070070extern size_t strlen(const char *) __purefunc;
Josh Gao14adff12016-04-29 12:00:55 -070071extern size_t __strlen_chk(const char*, size_t) __INTRODUCED_IN(21);
Nick Kralevicha6779072012-03-21 08:48:18 -070072extern int strcmp(const char *, const char *) __purefunc;
Josh Gao14adff12016-04-29 12:00:55 -070073extern char* stpcpy(char* __restrict, const char* __restrict) __INTRODUCED_IN(21);
Nick Kralevich1c462b72013-05-07 10:00:21 -070074extern char* strcpy(char* __restrict, const char* __restrict);
75extern char* strcat(char* __restrict, const char* __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080076
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080077extern char* strdup(const char *);
78
Nick Kralevicha6779072012-03-21 08:48:18 -070079extern char* strstr(const char *, const char *) __purefunc;
80extern char* strcasestr(const char *haystack, const char *needle) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070081extern char* strtok(char* __restrict, const char* __restrict);
82extern char* strtok_r(char* __restrict, const char* __restrict, char** __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083
Elliott Hughes416d7dd2014-08-18 17:28:32 -070084extern char* strerror(int);
Josh Gao14adff12016-04-29 12:00:55 -070085extern char* strerror_l(int, locale_t) __INTRODUCED_IN(23);
Elliott Hughes416d7dd2014-08-18 17:28:32 -070086#if defined(__USE_GNU)
Josh Gao14adff12016-04-29 12:00:55 -070087extern char* strerror_r(int, char*, size_t) __RENAME(__gnu_strerror_r) __INTRODUCED_IN(23);
Elliott Hughes416d7dd2014-08-18 17:28:32 -070088#else /* POSIX */
89extern int strerror_r(int, char*, size_t);
90#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080091
Nick Kralevicha6779072012-03-21 08:48:18 -070092extern size_t strnlen(const char *, size_t) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070093extern char* strncat(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080094extern char* strndup(const char *, size_t);
Nick Kralevicha6779072012-03-21 08:48:18 -070095extern int strncmp(const char *, const char *, size_t) __purefunc;
Josh Gao14adff12016-04-29 12:00:55 -070096extern char* stpncpy(char* __restrict, const char* __restrict, size_t) __INTRODUCED_IN(21);
Nick Kralevich1c462b72013-05-07 10:00:21 -070097extern char* strncpy(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080098
Nick Kralevich1c462b72013-05-07 10:00:21 -070099extern size_t strlcat(char* __restrict, const char* __restrict, size_t);
100extern size_t strlcpy(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800101
Nick Kralevicha6779072012-03-21 08:48:18 -0700102extern size_t strcspn(const char *, const char *) __purefunc;
103extern char* strpbrk(const char *, const char *) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -0700104extern char* strsep(char** __restrict, const char* __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800105extern size_t strspn(const char *, const char *);
106
107extern char* strsignal(int sig);
108
Nick Kralevicha6779072012-03-21 08:48:18 -0700109extern int strcoll(const char *, const char *) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -0700110extern size_t strxfrm(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800111
Josh Gao14adff12016-04-29 12:00:55 -0700112extern int strcoll_l(const char*, const char*, locale_t) __purefunc __INTRODUCED_IN(21);
113extern size_t strxfrm_l(char* __restrict, const char* __restrict, size_t, locale_t)
114 __INTRODUCED_IN(21);
Dan Albertdfb5ce42014-07-09 22:51:34 +0000115
Josh Gaoeb9b9252015-11-03 18:46:02 -0800116#if defined(__USE_GNU) && !defined(basename)
Elliott Hughes09c39d62014-08-19 14:30:30 -0700117/*
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 Gaoeb9b9252015-11-03 18:46:02 -0800121
Elliott Hughes09c39d62014-08-19 14:30:30 -0700122#if defined(__cplusplus)
123extern "C++" char* basename(char*) __RENAME(__gnu_basename) __nonnull((1));
124extern "C++" const char* basename(const char*) __RENAME(__gnu_basename) __nonnull((1));
125#else
Josh Gao14adff12016-04-29 12:00:55 -0700126extern char* basename(const char*) __RENAME(__gnu_basename) __nonnull((1)) __INTRODUCED_IN(23);
Elliott Hughes09c39d62014-08-19 14:30:30 -0700127#endif
Elliott Hughes09c39d62014-08-19 14:30:30 -0700128#endif
129
Josh Gao14adff12016-04-29 12:00:55 -0700130extern void* __memchr_chk(const void*, int, size_t, size_t) __INTRODUCED_IN(23);
Daniel Micay4ae77362015-04-17 18:16:57 -0400131__errordecl(__memchr_buf_size_error, "memchr called with size bigger than buffer");
132
Josh Gao14adff12016-04-29 12:00:55 -0700133extern void* __memrchr_chk(const void*, int, size_t, size_t) __INTRODUCED_IN(23);
Daniel Micay4ae77362015-04-17 18:16:57 -0400134__errordecl(__memrchr_buf_size_error, "memrchr called with size bigger than buffer");
135extern void* __memrchr_real(const void*, int, size_t) __RENAME(memrchr);
136
Josh Gao14adff12016-04-29 12:00:55 -0700137extern char* __stpncpy_chk2(char* __restrict, const char* __restrict, size_t, size_t, size_t)
138 __INTRODUCED_IN(21);
139extern char* __strncpy_chk2(char* __restrict, const char* __restrict, size_t, size_t, size_t)
140 __INTRODUCED_IN(21);
Dan Albert658727e2014-10-07 11:10:36 -0700141extern size_t __strlcpy_real(char* __restrict, const char* __restrict, size_t) __RENAME(strlcpy);
Josh Gao14adff12016-04-29 12:00:55 -0700142extern size_t __strlcpy_chk(char*, const char*, size_t, size_t) __INTRODUCED_IN(21);
Dan Albert658727e2014-10-07 11:10:36 -0700143extern size_t __strlcat_real(char* __restrict, const char* __restrict, size_t) __RENAME(strlcat);
Josh Gao14adff12016-04-29 12:00:55 -0700144extern size_t __strlcat_chk(char* __restrict, const char* __restrict, size_t, size_t)
145 __INTRODUCED_IN(21);
Dan Albert658727e2014-10-07 11:10:36 -0700146
Elliott Hughes890c8ed2013-03-22 10:58:55 -0700147#if defined(__BIONIC_FORTIFY)
Nick Kralevich0a230152012-06-04 15:20:25 -0700148
149__BIONIC_FORTIFY_INLINE
Daniel Micay4ae77362015-04-17 18:16:57 -0400150void* 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
171void* 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 Kralevich16d1af12013-06-17 14:49:19 -0700192void* memcpy(void* __restrict dest, const void* __restrict src, size_t copy_amount) {
Nick Kralevichb84f6672014-10-05 06:52:24 -0700193 return __builtin___memcpy_chk(dest, src, copy_amount, __bos0(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700194}
195
196__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700197void* memmove(void *dest, const void *src, size_t len) {
Nick Kralevichbd8e6742013-08-28 13:22:52 -0700198 return __builtin___memmove_chk(dest, src, len, __bos0(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700199}
200
201__BIONIC_FORTIFY_INLINE
Christopher Ferris950a58e2014-04-04 14:38:18 -0700202char* stpcpy(char* __restrict dest, const char* __restrict src) {
203 return __builtin___stpcpy_chk(dest, src, __bos(dest));
204}
205
206__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700207char* strcpy(char* __restrict dest, const char* __restrict src) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700208 return __builtin___strcpy_chk(dest, src, __bos(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700209}
210
Christopher Ferris950a58e2014-04-04 14:38:18 -0700211__BIONIC_FORTIFY_INLINE
212char* 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 Ferris950a58e2014-04-04 14:38:18 -0700215
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 Kralevich0a230152012-06-04 15:20:25 -0700232__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700233char* strncpy(char* __restrict dest, const char* __restrict src, size_t n) {
Nick Kralevich93501d32013-08-28 10:47:43 -0700234 size_t bos_dest = __bos(dest);
235 size_t bos_src = __bos(src);
Nick Kralevich93501d32013-08-28 10:47:43 -0700236
237 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
238 return __builtin___strncpy_chk(dest, src, n, bos_dest);
239 }
240
Nick Kralevichd13c2b12013-09-27 13:21:24 -0700241 if (__builtin_constant_p(n) && (n <= bos_src)) {
242 return __builtin___strncpy_chk(dest, src, n, bos_dest);
243 }
244
Nick Kralevich93501d32013-08-28 10:47:43 -0700245 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 Kralevich0a230152012-06-04 15:20:25 -0700251}
252
253__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700254char* strcat(char* __restrict dest, const char* __restrict src) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700255 return __builtin___strcat_chk(dest, src, __bos(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700256}
257
258__BIONIC_FORTIFY_INLINE
Nick Kralevich1c462b72013-05-07 10:00:21 -0700259char *strncat(char* __restrict dest, const char* __restrict src, size_t n) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700260 return __builtin___strncat_chk(dest, src, n, __bos(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700261}
262
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700263__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700264void* memset(void *s, int c, size_t n) {
Nick Kralevichbd8e6742013-08-28 13:22:52 -0700265 return __builtin___memset_chk(s, c, n, __bos0(s));
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700266}
267
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700268__BIONIC_FORTIFY_INLINE
Nick Kralevich1c462b72013-05-07 10:00:21 -0700269size_t strlcpy(char* __restrict dest, const char* __restrict src, size_t size) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700270 size_t bos = __bos(dest);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700271
Nick Kralevich8bafa742013-06-20 12:17:44 -0700272#if !defined(__clang__)
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700273 // Compiler doesn't know destination size. Don't call __strlcpy_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700274 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700275 return __strlcpy_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700276 }
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 Kralevichcb228fb2012-06-26 16:05:19 -0700281 return __strlcpy_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700282 }
Nick Kralevich8bafa742013-06-20 12:17:44 -0700283#endif /* !defined(__clang__) */
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700284
285 return __strlcpy_chk(dest, src, size, bos);
286}
287
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700288
289__BIONIC_FORTIFY_INLINE
Nick Kralevich1c462b72013-05-07 10:00:21 -0700290size_t strlcat(char* __restrict dest, const char* __restrict src, size_t size) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700291 size_t bos = __bos(dest);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700292
Nick Kralevicha6cde392013-06-29 08:15:25 -0700293#if !defined(__clang__)
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700294 // Compiler doesn't know destination size. Don't call __strlcat_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700295 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700296 return __strlcat_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700297 }
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 Kralevichcb228fb2012-06-26 16:05:19 -0700302 return __strlcat_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700303 }
Nick Kralevicha6cde392013-06-29 08:15:25 -0700304#endif /* !defined(__clang__) */
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700305
306 return __strlcat_chk(dest, src, size, bos);
307}
308
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700309__BIONIC_FORTIFY_INLINE
310size_t strlen(const char *s) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700311 size_t bos = __bos(s);
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700312
Nick Kralevich16d1af12013-06-17 14:49:19 -0700313#if !defined(__clang__)
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700314 // Compiler doesn't know destination size. Don't call __strlen_chk
315 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800316 return __builtin_strlen(s);
317 }
318
319 size_t slen = __builtin_strlen(s);
320 if (__builtin_constant_p(slen)) {
321 return slen;
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700322 }
Nick Kralevich16d1af12013-06-17 14:49:19 -0700323#endif /* !defined(__clang__) */
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700324
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700325 return __strlen_chk(s, bos);
326}
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700327
Nick Kralevich049e5832012-11-30 15:15:58 -0800328__BIONIC_FORTIFY_INLINE
329char* strchr(const char *s, int c) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700330 size_t bos = __bos(s);
Nick Kralevich049e5832012-11-30 15:15:58 -0800331
Nick Kralevich16d1af12013-06-17 14:49:19 -0700332#if !defined(__clang__)
Nick Kralevich049e5832012-11-30 15:15:58 -0800333 // Compiler doesn't know destination size. Don't call __strchr_chk
334 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800335 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 Kralevich049e5832012-11-30 15:15:58 -0800341 }
Nick Kralevich16d1af12013-06-17 14:49:19 -0700342#endif /* !defined(__clang__) */
Nick Kralevich049e5832012-11-30 15:15:58 -0800343
344 return __strchr_chk(s, c, bos);
345}
346
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800347__BIONIC_FORTIFY_INLINE
348char* strrchr(const char *s, int c) {
Nick Kralevich3b2e6bc2013-04-30 14:19:23 -0700349 size_t bos = __bos(s);
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800350
Nick Kralevich16d1af12013-06-17 14:49:19 -0700351#if !defined(__clang__)
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800352 // Compiler doesn't know destination size. Don't call __strrchr_chk
353 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800354 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 Kralevich9a4d3052012-12-03 10:36:13 -0800360 }
Nick Kralevich16d1af12013-06-17 14:49:19 -0700361#endif /* !defined(__clang__) */
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800362
363 return __strrchr_chk(s, c, bos);
364}
365
Nick Kralevich049e5832012-11-30 15:15:58 -0800366
Elliott Hughes890c8ed2013-03-22 10:58:55 -0700367#endif /* defined(__BIONIC_FORTIFY) */
Nick Kralevich0a230152012-06-04 15:20:25 -0700368
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800369__END_DECLS
370
Elliott Hughes09c39d62014-08-19 14:30:30 -0700371#endif /* _STRING_H */