blob: 3e52f481202ccdfe10200579d8aa4d8dc4114b24 [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 Hughes5470c182016-07-22 11:36:17 -070040#if defined(__clang__)
41#pragma clang diagnostic push
42#pragma clang diagnostic ignored "-Wnullability-completeness"
43#endif
44
Elliott Hughes76f89162015-01-26 13:34:58 -080045#if defined(__USE_BSD)
46#include <strings.h>
47#endif
48
Elliott Hughes5470c182016-07-22 11:36:17 -070049void* memccpy(void* _Nonnull __restrict, const void* _Nonnull __restrict, int, size_t);
Elliott Hughes95fa0612016-09-28 12:29:52 -070050void* memchr(const void* _Nonnull, int, size_t) __attribute_pure__;
51void* memrchr(const void* _Nonnull, int, size_t) __attribute_pure__;
52int memcmp(const void* _Nonnull, const void* _Nonnull, size_t) __attribute_pure__;
Elliott Hughes5470c182016-07-22 11:36:17 -070053void* memcpy(void* _Nonnull __restrict, const void* _Nonnull __restrict, size_t);
Elliott Hughes3cfb52a2015-02-18 21:29:13 -080054#if defined(__USE_GNU)
Elliott Hughes5470c182016-07-22 11:36:17 -070055void* mempcpy(void* _Nonnull __restrict, const void* _Nonnull __restrict, size_t) __INTRODUCED_IN(23);
Elliott Hughes3cfb52a2015-02-18 21:29:13 -080056#endif
Elliott Hughes5470c182016-07-22 11:36:17 -070057void* memmove(void* _Nonnull, const void* _Nonnull, size_t);
58void* memset(void* _Nonnull, int, size_t);
Elliott Hughes95fa0612016-09-28 12:29:52 -070059void* memmem(const void* _Nonnull, size_t, const void* _Nonnull, size_t) __attribute_pure__;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080060
Elliott Hughes95fa0612016-09-28 12:29:52 -070061char* strchr(const char* _Nonnull, int) __attribute_pure__;
Elliott Hughes5470c182016-07-22 11:36:17 -070062char* __strchr_chk(const char* _Nonnull, int, size_t) __INTRODUCED_IN(18);
Elliott Hughes7ac3c122015-08-26 09:59:29 -070063#if defined(__USE_GNU)
64#if defined(__cplusplus)
Elliott Hughes95fa0612016-09-28 12:29:52 -070065extern "C++" char* strchrnul(char* _Nonnull, int) __RENAME(strchrnul) __attribute_pure__;
66extern "C++" const char* strchrnul(const char* _Nonnull, int) __RENAME(strchrnul) __attribute_pure__;
Elliott Hughes7ac3c122015-08-26 09:59:29 -070067#else
Elliott Hughes95fa0612016-09-28 12:29:52 -070068char* strchrnul(const char* _Nonnull, int) __attribute_pure__ __INTRODUCED_IN(24);
Elliott Hughes7ac3c122015-08-26 09:59:29 -070069#endif
70#endif
Pavel Chupin3c4b50f2013-07-26 16:50:11 +040071
Elliott Hughes95fa0612016-09-28 12:29:52 -070072char* strrchr(const char* _Nonnull, int) __attribute_pure__;
Elliott Hughes5470c182016-07-22 11:36:17 -070073char* __strrchr_chk(const char* _Nonnull, int, size_t) __INTRODUCED_IN(18);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074
Elliott Hughes95fa0612016-09-28 12:29:52 -070075size_t strlen(const char* _Nonnull) __attribute_pure__;
Elliott Hughes5470c182016-07-22 11:36:17 -070076size_t __strlen_chk(const char* _Nonnull, size_t) __INTRODUCED_IN(17);
Elliott Hughes95fa0612016-09-28 12:29:52 -070077int strcmp(const char* _Nonnull, const char* _Nonnull) __attribute_pure__;
Elliott Hughes5470c182016-07-22 11:36:17 -070078char* stpcpy(char* _Nonnull __restrict, const char* _Nonnull__restrict) __INTRODUCED_IN(21);
79char* strcpy(char* _Nonnull __restrict, const char* _Nonnull __restrict);
80char* strcat(char* _Nonnull __restrict, const char* _Nonnull __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080081
Elliott Hughes5470c182016-07-22 11:36:17 -070082char* strdup(const char* _Nonnull);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083
Elliott Hughes95fa0612016-09-28 12:29:52 -070084char* strstr(const char* _Nonnull, const char* _Nonnull) __attribute_pure__;
85char* strcasestr(const char* _Nonnull, const char* _Nonnull) __attribute_pure__;
Elliott Hughes5470c182016-07-22 11:36:17 -070086char* strtok(char* __restrict, const char* _Nonnull __restrict);
87char* strtok_r(char* __restrict, const char* _Nonnull __restrict, char** _Nonnull __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080088
Elliott Hughes5470c182016-07-22 11:36:17 -070089char* strerror(int);
90char* strerror_l(int, locale_t) __INTRODUCED_IN(23);
Elliott Hughes416d7dd2014-08-18 17:28:32 -070091#if defined(__USE_GNU)
Elliott Hughes5470c182016-07-22 11:36:17 -070092char* strerror_r(int, char*, size_t) __RENAME(__gnu_strerror_r) __INTRODUCED_IN(23);
Elliott Hughes416d7dd2014-08-18 17:28:32 -070093#else /* POSIX */
Elliott Hughes5470c182016-07-22 11:36:17 -070094int strerror_r(int, char*, size_t);
Elliott Hughes416d7dd2014-08-18 17:28:32 -070095#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080096
Elliott Hughes95fa0612016-09-28 12:29:52 -070097size_t strnlen(const char* _Nonnull, size_t) __attribute_pure__;
Elliott Hughes5470c182016-07-22 11:36:17 -070098char* strncat(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t);
99char* strndup(const char* _Nonnull, size_t);
Elliott Hughes95fa0612016-09-28 12:29:52 -0700100int strncmp(const char* _Nonnull, const char* _Nonnull, size_t) __attribute_pure__;
Elliott Hughes5470c182016-07-22 11:36:17 -0700101char* stpncpy(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t) __INTRODUCED_IN(21);
102char* strncpy(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103
Elliott Hughes5470c182016-07-22 11:36:17 -0700104size_t strlcat(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t);
105size_t strlcpy(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800106
Elliott Hughes95fa0612016-09-28 12:29:52 -0700107size_t strcspn(const char* _Nonnull, const char* _Nonnull) __attribute_pure__;
108char* strpbrk(const char* _Nonnull, const char* _Nonnull) __attribute_pure__;
Elliott Hughes5470c182016-07-22 11:36:17 -0700109char* strsep(char** _Nonnull __restrict, const char* _Nonnull __restrict);
110size_t strspn(const char* _Nonnull, const char* _Nonnull);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800111
Elliott Hughes5470c182016-07-22 11:36:17 -0700112char* strsignal(int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800113
Elliott Hughes95fa0612016-09-28 12:29:52 -0700114int strcoll(const char* _Nonnull, const char* _Nonnull) __attribute_pure__;
Elliott Hughes5470c182016-07-22 11:36:17 -0700115size_t strxfrm(char* __restrict, const char* _Nonnull __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800116
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800117#if __ANDROID_API__ >= __ANDROID_API_L__
Elliott Hughes95fa0612016-09-28 12:29:52 -0700118int strcoll_l(const char* _Nonnull, const char* _Nonnull, locale_t) __attribute_pure__ __INTRODUCED_IN(21);
Elliott Hughes5470c182016-07-22 11:36:17 -0700119size_t strxfrm_l(char* __restrict, const char* _Nonnull __restrict, size_t, locale_t) __INTRODUCED_IN(21);
Josh Gao6cd9fb02016-09-23 14:06:05 -0700120#else
121// Implemented as static inlines before 21.
122#endif
Dan Albertdfb5ce42014-07-09 22:51:34 +0000123
Josh Gaoeb9b9252015-11-03 18:46:02 -0800124#if defined(__USE_GNU) && !defined(basename)
Elliott Hughes09c39d62014-08-19 14:30:30 -0700125/*
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 Albertbaa2a972015-08-13 16:58:50 -0700130extern "C++" char* basename(char* _Nonnull) __RENAME(__gnu_basename);
131extern "C++" const char* basename(const char* _Nonnull) __RENAME(__gnu_basename);
Elliott Hughes09c39d62014-08-19 14:30:30 -0700132#else
Elliott Hughes5470c182016-07-22 11:36:17 -0700133char* basename(const char* _Nonnull) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
Elliott Hughes09c39d62014-08-19 14:30:30 -0700134#endif
Elliott Hughes09c39d62014-08-19 14:30:30 -0700135#endif
136
Elliott Hughes5470c182016-07-22 11:36:17 -0700137void* __memchr_chk(const void* _Nonnull, int, size_t, size_t) __INTRODUCED_IN(23);
Daniel Micay4ae77362015-04-17 18:16:57 -0400138__errordecl(__memchr_buf_size_error, "memchr called with size bigger than buffer");
139
Elliott Hughes5470c182016-07-22 11:36:17 -0700140void* __memrchr_chk(const void* _Nonnull, int, size_t, size_t) __INTRODUCED_IN(23);
Daniel Micay4ae77362015-04-17 18:16:57 -0400141__errordecl(__memrchr_buf_size_error, "memrchr called with size bigger than buffer");
Elliott Hughes5470c182016-07-22 11:36:17 -0700142void* __memrchr_real(const void* _Nonnull, int, size_t) __RENAME(memrchr);
Daniel Micay4ae77362015-04-17 18:16:57 -0400143
Elliott Hughes5470c182016-07-22 11:36:17 -0700144char* __stpncpy_chk2(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t, size_t, size_t)
Josh Gao14adff12016-04-29 12:00:55 -0700145 __INTRODUCED_IN(21);
Elliott Hughes5470c182016-07-22 11:36:17 -0700146char* __strncpy_chk2(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t, size_t, size_t)
Josh Gao14adff12016-04-29 12:00:55 -0700147 __INTRODUCED_IN(21);
Elliott Hughes5470c182016-07-22 11:36:17 -0700148size_t __strlcpy_real(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t) __RENAME(strlcpy);
149size_t __strlcpy_chk(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t, size_t) __INTRODUCED_IN(17);
150size_t __strlcat_real(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t) __RENAME(strlcat);
151size_t __strlcat_chk(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t, size_t) __INTRODUCED_IN(17);
Dan Albert658727e2014-10-07 11:10:36 -0700152
Elliott Hughes890c8ed2013-03-22 10:58:55 -0700153#if defined(__BIONIC_FORTIFY)
Nick Kralevich0a230152012-06-04 15:20:25 -0700154
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800155#if __ANDROID_API__ >= __ANDROID_API_M__
Nick Kralevich0a230152012-06-04 15:20:25 -0700156__BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700157void* memchr(const void* s, int c, size_t n) {
Daniel Micay4ae77362015-04-17 18:16:57 -0400158 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 Hughes5470c182016-07-22 11:36:17 -0700178void* memrchr(const void* s, int c, size_t n) {
Daniel Micay4ae77362015-04-17 18:16:57 -0400179 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 Hughes5bc78c82016-11-16 11:35:43 -0800197#endif /* __ANDROID_API__ >= __ANDROID_API_M__ */
Daniel Micay4ae77362015-04-17 18:16:57 -0400198
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800199#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
Daniel Micay4ae77362015-04-17 18:16:57 -0400200__BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700201void* 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 Kralevich0a230152012-06-04 15:20:25 -0700203}
204
205__BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700206void* memmove(void* _Nonnull dst, const void* _Nonnull src, size_t len) {
207 return __builtin___memmove_chk(dst, src, len, __bos0(dst));
Nick Kralevich0a230152012-06-04 15:20:25 -0700208}
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800209#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
Nick Kralevich0a230152012-06-04 15:20:25 -0700210
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800211#if __ANDROID_API__ >= __ANDROID_API_L__
Nick Kralevich0a230152012-06-04 15:20:25 -0700212__BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700213char* stpcpy(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src) {
214 return __builtin___stpcpy_chk(dst, src, __bos(dst));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700215}
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800216#endif /* __ANDROID_API__ >= __ANDROID_API_L__ */
Christopher Ferris950a58e2014-04-04 14:38:18 -0700217
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800218#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
Christopher Ferris950a58e2014-04-04 14:38:18 -0700219__BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700220char* strcpy(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src) {
221 return __builtin___strcpy_chk(dst, src, __bos(dst));
Nick Kralevich0a230152012-06-04 15:20:25 -0700222}
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800223#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
Nick Kralevich0a230152012-06-04 15:20:25 -0700224
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800225#if __ANDROID_API__ >= __ANDROID_API_L__
Christopher Ferris950a58e2014-04-04 14:38:18 -0700226__BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700227char* stpncpy(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src, size_t n) {
228 size_t bos_dst = __bos(dst);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700229 size_t bos_src = __bos(src);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700230
231 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Elliott Hughes5470c182016-07-22 11:36:17 -0700232 return __builtin___stpncpy_chk(dst, src, n, bos_dst);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700233 }
234
235 if (__builtin_constant_p(n) && (n <= bos_src)) {
Elliott Hughes5470c182016-07-22 11:36:17 -0700236 return __builtin___stpncpy_chk(dst, src, n, bos_dst);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700237 }
238
239 size_t slen = __builtin_strlen(src);
240 if (__builtin_constant_p(slen)) {
Elliott Hughes5470c182016-07-22 11:36:17 -0700241 return __builtin___stpncpy_chk(dst, src, n, bos_dst);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700242 }
243
Elliott Hughes5470c182016-07-22 11:36:17 -0700244 return __stpncpy_chk2(dst, src, n, bos_dst, bos_src);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700245}
246
Nick Kralevich0a230152012-06-04 15:20:25 -0700247__BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700248char* strncpy(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src, size_t n) {
249 size_t bos_dst = __bos(dst);
Nick Kralevich93501d32013-08-28 10:47:43 -0700250 size_t bos_src = __bos(src);
Nick Kralevich93501d32013-08-28 10:47:43 -0700251
252 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Elliott Hughes5470c182016-07-22 11:36:17 -0700253 return __builtin___strncpy_chk(dst, src, n, bos_dst);
Nick Kralevich93501d32013-08-28 10:47:43 -0700254 }
255
Nick Kralevichd13c2b12013-09-27 13:21:24 -0700256 if (__builtin_constant_p(n) && (n <= bos_src)) {
Elliott Hughes5470c182016-07-22 11:36:17 -0700257 return __builtin___strncpy_chk(dst, src, n, bos_dst);
Nick Kralevichd13c2b12013-09-27 13:21:24 -0700258 }
259
Nick Kralevich93501d32013-08-28 10:47:43 -0700260 size_t slen = __builtin_strlen(src);
261 if (__builtin_constant_p(slen)) {
Elliott Hughes5470c182016-07-22 11:36:17 -0700262 return __builtin___strncpy_chk(dst, src, n, bos_dst);
Nick Kralevich93501d32013-08-28 10:47:43 -0700263 }
264
Elliott Hughes5470c182016-07-22 11:36:17 -0700265 return __strncpy_chk2(dst, src, n, bos_dst, bos_src);
Nick Kralevich0a230152012-06-04 15:20:25 -0700266}
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800267#endif /* __ANDROID_API__ >= __ANDROID_API_L__ */
Nick Kralevich0a230152012-06-04 15:20:25 -0700268
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800269#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
Nick Kralevich0a230152012-06-04 15:20:25 -0700270__BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700271char* strcat(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src) {
272 return __builtin___strcat_chk(dst, src, __bos(dst));
Nick Kralevich0a230152012-06-04 15:20:25 -0700273}
274
275__BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700276char *strncat(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src, size_t n) {
277 return __builtin___strncat_chk(dst, src, n, __bos(dst));
Nick Kralevich0a230152012-06-04 15:20:25 -0700278}
279
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700280__BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700281void* memset(void* _Nonnull s, int c, size_t n) {
Nick Kralevichbd8e6742013-08-28 13:22:52 -0700282 return __builtin___memset_chk(s, c, n, __bos0(s));
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700283}
284
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700285__BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700286size_t strlcpy(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src, size_t size) {
287 size_t bos = __bos(dst);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700288
Nick Kralevich8bafa742013-06-20 12:17:44 -0700289#if !defined(__clang__)
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700290 // Compiler doesn't know destination size. Don't call __strlcpy_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700291 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Elliott Hughes5470c182016-07-22 11:36:17 -0700292 return __strlcpy_real(dst, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700293 }
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 Hughes5470c182016-07-22 11:36:17 -0700298 return __strlcpy_real(dst, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700299 }
Nick Kralevich8bafa742013-06-20 12:17:44 -0700300#endif /* !defined(__clang__) */
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700301
Elliott Hughes5470c182016-07-22 11:36:17 -0700302 return __strlcpy_chk(dst, src, size, bos);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700303}
304
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700305
306__BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700307size_t strlcat(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src, size_t size) {
308 size_t bos = __bos(dst);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700309
Nick Kralevicha6cde392013-06-29 08:15:25 -0700310#if !defined(__clang__)
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700311 // Compiler doesn't know destination size. Don't call __strlcat_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700312 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Elliott Hughes5470c182016-07-22 11:36:17 -0700313 return __strlcat_real(dst, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700314 }
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 Hughes5470c182016-07-22 11:36:17 -0700319 return __strlcat_real(dst, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700320 }
Nick Kralevicha6cde392013-06-29 08:15:25 -0700321#endif /* !defined(__clang__) */
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700322
Elliott Hughes5470c182016-07-22 11:36:17 -0700323 return __strlcat_chk(dst, src, size, bos);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700324}
325
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700326__BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700327size_t strlen(const char* _Nonnull s) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700328 size_t bos = __bos(s);
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700329
Nick Kralevich16d1af12013-06-17 14:49:19 -0700330#if !defined(__clang__)
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700331 // Compiler doesn't know destination size. Don't call __strlen_chk
332 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800333 return __builtin_strlen(s);
334 }
335
336 size_t slen = __builtin_strlen(s);
337 if (__builtin_constant_p(slen)) {
338 return slen;
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700339 }
Nick Kralevich16d1af12013-06-17 14:49:19 -0700340#endif /* !defined(__clang__) */
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700341
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700342 return __strlen_chk(s, bos);
343}
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800344#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700345
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800346#if __ANDROID_API__ >= __ANDROID_API_J_MR2__
Nick Kralevich049e5832012-11-30 15:15:58 -0800347__BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700348char* strchr(const char* _Nonnull s, int c) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700349 size_t bos = __bos(s);
Nick Kralevich049e5832012-11-30 15:15:58 -0800350
Nick Kralevich16d1af12013-06-17 14:49:19 -0700351#if !defined(__clang__)
Nick Kralevich049e5832012-11-30 15:15:58 -0800352 // Compiler doesn't know destination size. Don't call __strchr_chk
353 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800354 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 Kralevich049e5832012-11-30 15:15:58 -0800360 }
Nick Kralevich16d1af12013-06-17 14:49:19 -0700361#endif /* !defined(__clang__) */
Nick Kralevich049e5832012-11-30 15:15:58 -0800362
363 return __strchr_chk(s, c, bos);
364}
365
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800366__BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700367char* strrchr(const char* _Nonnull s, int c) {
Nick Kralevich3b2e6bc2013-04-30 14:19:23 -0700368 size_t bos = __bos(s);
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800369
Nick Kralevich16d1af12013-06-17 14:49:19 -0700370#if !defined(__clang__)
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800371 // Compiler doesn't know destination size. Don't call __strrchr_chk
372 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800373 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 Kralevich9a4d3052012-12-03 10:36:13 -0800379 }
Nick Kralevich16d1af12013-06-17 14:49:19 -0700380#endif /* !defined(__clang__) */
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800381
382 return __strrchr_chk(s, c, bos);
383}
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800384#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR2__ */
Nick Kralevich049e5832012-11-30 15:15:58 -0800385
Elliott Hughes890c8ed2013-03-22 10:58:55 -0700386#endif /* defined(__BIONIC_FORTIFY) */
Nick Kralevich0a230152012-06-04 15:20:25 -0700387
Elliott Hughes5470c182016-07-22 11:36:17 -0700388#if defined(__clang__)
389#pragma clang diagnostic pop
390#endif
391
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800392__END_DECLS
393
Elliott Hughes09c39d62014-08-19 14:30:30 -0700394#endif /* _STRING_H */