blob: 202723108bfd1d7a6ebfb9f07ada2455203e1bbf [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);
George Burgess IV7cc779f2017-02-09 00:00:31 -080050void* memchr(const void* _Nonnull, int, size_t) __attribute_pure__ __overloadable
51 __RENAME_CLANG(memchr);
52void* memrchr(const void* _Nonnull, int, size_t) __attribute_pure__ __overloadable
53 __RENAME_CLANG(memrchr);
Elliott Hughes95fa0612016-09-28 12:29:52 -070054int memcmp(const void* _Nonnull, const void* _Nonnull, size_t) __attribute_pure__;
George Burgess IV7cc779f2017-02-09 00:00:31 -080055void* memcpy(void* _Nonnull __restrict, const void* _Nonnull __restrict, size_t)
56 __overloadable __RENAME_CLANG(memcpy);
Elliott Hughes3cfb52a2015-02-18 21:29:13 -080057#if defined(__USE_GNU)
Elliott Hughes5470c182016-07-22 11:36:17 -070058void* mempcpy(void* _Nonnull __restrict, const void* _Nonnull __restrict, size_t) __INTRODUCED_IN(23);
Elliott Hughes3cfb52a2015-02-18 21:29:13 -080059#endif
George Burgess IV7cc779f2017-02-09 00:00:31 -080060void* memmove(void* _Nonnull, const void* _Nonnull, size_t) __overloadable
61 __RENAME_CLANG(memmove);
62void* memset(void* _Nonnull, int, size_t) __overloadable __RENAME_CLANG(memset);
Elliott Hughes95fa0612016-09-28 12:29:52 -070063void* memmem(const void* _Nonnull, size_t, const void* _Nonnull, size_t) __attribute_pure__;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064
George Burgess IV7cc779f2017-02-09 00:00:31 -080065char* strchr(const char* _Nonnull, int) __attribute_pure__ __overloadable
66 __RENAME_CLANG(strchr);
Elliott Hughes5470c182016-07-22 11:36:17 -070067char* __strchr_chk(const char* _Nonnull, int, size_t) __INTRODUCED_IN(18);
Elliott Hughes7ac3c122015-08-26 09:59:29 -070068#if defined(__USE_GNU)
69#if defined(__cplusplus)
Elliott Hughes95fa0612016-09-28 12:29:52 -070070extern "C++" char* strchrnul(char* _Nonnull, int) __RENAME(strchrnul) __attribute_pure__;
71extern "C++" const char* strchrnul(const char* _Nonnull, int) __RENAME(strchrnul) __attribute_pure__;
Elliott Hughes7ac3c122015-08-26 09:59:29 -070072#else
Elliott Hughes95fa0612016-09-28 12:29:52 -070073char* strchrnul(const char* _Nonnull, int) __attribute_pure__ __INTRODUCED_IN(24);
Elliott Hughes7ac3c122015-08-26 09:59:29 -070074#endif
75#endif
Pavel Chupin3c4b50f2013-07-26 16:50:11 +040076
George Burgess IV7cc779f2017-02-09 00:00:31 -080077char* strrchr(const char* _Nonnull, int) __attribute_pure__ __overloadable
78 __RENAME_CLANG(strrchr);
Elliott Hughes5470c182016-07-22 11:36:17 -070079char* __strrchr_chk(const char* _Nonnull, int, size_t) __INTRODUCED_IN(18);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080080
George Burgess IV7cc779f2017-02-09 00:00:31 -080081size_t strlen(const char* _Nonnull) __attribute_pure__ __overloadable
82 __RENAME_CLANG(strlen);
Elliott Hughes5470c182016-07-22 11:36:17 -070083size_t __strlen_chk(const char* _Nonnull, size_t) __INTRODUCED_IN(17);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080084
George Burgess IV7cc779f2017-02-09 00:00:31 -080085int strcmp(const char* _Nonnull, const char* _Nonnull) __attribute_pure__;
86char* stpcpy(char* _Nonnull __restrict, const char* _Nonnull __restrict)
87 __overloadable __RENAME_CLANG(stpcpy) __INTRODUCED_IN(21);
88char* strcpy(char* _Nonnull __restrict, const char* _Nonnull __restrict)
89 __overloadable __RENAME_CLANG(strcpy);
90char* strcat(char* _Nonnull __restrict, const char* _Nonnull __restrict)
91 __overloadable __RENAME_CLANG(strcat);
Elliott Hughes5470c182016-07-22 11:36:17 -070092char* strdup(const char* _Nonnull);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093
Elliott Hughes95fa0612016-09-28 12:29:52 -070094char* strstr(const char* _Nonnull, const char* _Nonnull) __attribute_pure__;
95char* strcasestr(const char* _Nonnull, const char* _Nonnull) __attribute_pure__;
Elliott Hughes5470c182016-07-22 11:36:17 -070096char* strtok(char* __restrict, const char* _Nonnull __restrict);
97char* strtok_r(char* __restrict, const char* _Nonnull __restrict, char** _Nonnull __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080098
Elliott Hughes5470c182016-07-22 11:36:17 -070099char* strerror(int);
100char* strerror_l(int, locale_t) __INTRODUCED_IN(23);
Dan Albert8b154b12017-02-14 16:33:06 -0800101#if defined(__USE_GNU) && __ANDROID_API__ >= 23
Elliott Hughes5470c182016-07-22 11:36:17 -0700102char* strerror_r(int, char*, size_t) __RENAME(__gnu_strerror_r) __INTRODUCED_IN(23);
Elliott Hughes416d7dd2014-08-18 17:28:32 -0700103#else /* POSIX */
Elliott Hughes5470c182016-07-22 11:36:17 -0700104int strerror_r(int, char*, size_t);
Elliott Hughes416d7dd2014-08-18 17:28:32 -0700105#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800106
Elliott Hughes95fa0612016-09-28 12:29:52 -0700107size_t strnlen(const char* _Nonnull, size_t) __attribute_pure__;
George Burgess IV7cc779f2017-02-09 00:00:31 -0800108char* strncat(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t)
109 __overloadable __RENAME_CLANG(strncat);
Elliott Hughes5470c182016-07-22 11:36:17 -0700110char* strndup(const char* _Nonnull, size_t);
Elliott Hughes95fa0612016-09-28 12:29:52 -0700111int strncmp(const char* _Nonnull, const char* _Nonnull, size_t) __attribute_pure__;
George Burgess IV7cc779f2017-02-09 00:00:31 -0800112char* stpncpy(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t)
113 __overloadable __RENAME_CLANG(stpncpy) __INTRODUCED_IN(21);
114char* strncpy(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t)
115 __overloadable __RENAME_CLANG(strncpy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800116
George Burgess IV7cc779f2017-02-09 00:00:31 -0800117size_t strlcat(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t)
118 __overloadable __RENAME_CLANG(strlcat);
119size_t strlcpy(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t)
120 __overloadable __RENAME_CLANG(strlcpy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800121
Elliott Hughes95fa0612016-09-28 12:29:52 -0700122size_t strcspn(const char* _Nonnull, const char* _Nonnull) __attribute_pure__;
123char* strpbrk(const char* _Nonnull, const char* _Nonnull) __attribute_pure__;
Elliott Hughes5470c182016-07-22 11:36:17 -0700124char* strsep(char** _Nonnull __restrict, const char* _Nonnull __restrict);
125size_t strspn(const char* _Nonnull, const char* _Nonnull);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800126
Elliott Hughes5470c182016-07-22 11:36:17 -0700127char* strsignal(int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800128
Elliott Hughes95fa0612016-09-28 12:29:52 -0700129int strcoll(const char* _Nonnull, const char* _Nonnull) __attribute_pure__;
Elliott Hughes5470c182016-07-22 11:36:17 -0700130size_t strxfrm(char* __restrict, const char* _Nonnull __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800132#if __ANDROID_API__ >= __ANDROID_API_L__
Elliott Hughes95fa0612016-09-28 12:29:52 -0700133int strcoll_l(const char* _Nonnull, const char* _Nonnull, locale_t) __attribute_pure__ __INTRODUCED_IN(21);
Elliott Hughes5470c182016-07-22 11:36:17 -0700134size_t strxfrm_l(char* __restrict, const char* _Nonnull __restrict, size_t, locale_t) __INTRODUCED_IN(21);
Josh Gao6cd9fb02016-09-23 14:06:05 -0700135#else
136// Implemented as static inlines before 21.
137#endif
Dan Albertdfb5ce42014-07-09 22:51:34 +0000138
Josh Gaoeb9b9252015-11-03 18:46:02 -0800139#if defined(__USE_GNU) && !defined(basename)
Elliott Hughes09c39d62014-08-19 14:30:30 -0700140/*
141 * glibc has a basename in <string.h> that's different to the POSIX one in <libgen.h>.
142 * It doesn't modify its argument, and in C++ it's const-correct.
143 */
144#if defined(__cplusplus)
Dan Albertbaa2a972015-08-13 16:58:50 -0700145extern "C++" char* basename(char* _Nonnull) __RENAME(__gnu_basename);
146extern "C++" const char* basename(const char* _Nonnull) __RENAME(__gnu_basename);
Elliott Hughes09c39d62014-08-19 14:30:30 -0700147#else
Elliott Hughes5470c182016-07-22 11:36:17 -0700148char* basename(const char* _Nonnull) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
Elliott Hughes09c39d62014-08-19 14:30:30 -0700149#endif
Elliott Hughes09c39d62014-08-19 14:30:30 -0700150#endif
151
Elliott Hughes5470c182016-07-22 11:36:17 -0700152void* __memchr_chk(const void* _Nonnull, int, size_t, size_t) __INTRODUCED_IN(23);
Elliott Hughes5470c182016-07-22 11:36:17 -0700153void* __memrchr_chk(const void* _Nonnull, int, size_t, size_t) __INTRODUCED_IN(23);
Elliott Hughes5470c182016-07-22 11:36:17 -0700154char* __stpncpy_chk2(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t, size_t, size_t)
Josh Gao14adff12016-04-29 12:00:55 -0700155 __INTRODUCED_IN(21);
Elliott Hughes5470c182016-07-22 11:36:17 -0700156char* __strncpy_chk2(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t, size_t, size_t)
Josh Gao14adff12016-04-29 12:00:55 -0700157 __INTRODUCED_IN(21);
Elliott Hughes5470c182016-07-22 11:36:17 -0700158size_t __strlcpy_chk(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t, size_t) __INTRODUCED_IN(17);
Elliott Hughes5470c182016-07-22 11:36:17 -0700159size_t __strlcat_chk(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t, size_t) __INTRODUCED_IN(17);
Dan Albert658727e2014-10-07 11:10:36 -0700160
George Burgess IV7cc779f2017-02-09 00:00:31 -0800161/* Only used with FORTIFY, but some headers that need it undef FORTIFY, so we
162 * have the definition out here.
163 */
164struct __bionic_zero_size_is_okay_t {};
165
Elliott Hughes890c8ed2013-03-22 10:58:55 -0700166#if defined(__BIONIC_FORTIFY)
George Burgess IV7cc779f2017-02-09 00:00:31 -0800167// These can share their implementation between gcc and clang with minimal
168// trickery...
169#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
170__BIONIC_FORTIFY_INLINE
George Burgess IV156d5a82017-02-10 13:56:22 -0800171void* memcpy(void* _Nonnull __restrict const dst __pass_object_size0,
George Burgess IV7cc779f2017-02-09 00:00:31 -0800172 const void* _Nonnull __restrict src, size_t copy_amount) __overloadable {
173 return __builtin___memcpy_chk(dst, src, copy_amount, __bos0(dst));
174}
175
176__BIONIC_FORTIFY_INLINE
George Burgess IV156d5a82017-02-10 13:56:22 -0800177void* memmove(void* const _Nonnull dst __pass_object_size0,
George Burgess IV7cc779f2017-02-09 00:00:31 -0800178 const void* _Nonnull src, size_t len) __overloadable {
179 return __builtin___memmove_chk(dst, src, len, __bos0(dst));
180}
181#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
182
183#if __ANDROID_API__ >= __ANDROID_API_L__
184__BIONIC_FORTIFY_INLINE
185char* stpcpy(char* _Nonnull __restrict const dst __pass_object_size,
186 const char* _Nonnull __restrict src) __overloadable {
187 return __builtin___stpcpy_chk(dst, src, __bos(dst));
188}
189#endif /* __ANDROID_API__ >= __ANDROID_API_L__ */
190
191#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
192__BIONIC_FORTIFY_INLINE
193char* strcpy(char* _Nonnull __restrict const dst __pass_object_size,
194 const char* _Nonnull __restrict src) __overloadable {
195 return __builtin___strcpy_chk(dst, src, __bos(dst));
196}
George Burgess IV7cc779f2017-02-09 00:00:31 -0800197
198__BIONIC_FORTIFY_INLINE
199char* strcat(char* _Nonnull __restrict const dst __pass_object_size,
200 const char* _Nonnull __restrict src) __overloadable {
201 return __builtin___strcat_chk(dst, src, __bos(dst));
202}
203
204__BIONIC_FORTIFY_INLINE
205char* strncat(char* const _Nonnull __restrict dst __pass_object_size,
206 const char* _Nonnull __restrict src, size_t n) __overloadable {
207 return __builtin___strncat_chk(dst, src, n, __bos(dst));
208}
209
210__BIONIC_FORTIFY_INLINE
George Burgess IV156d5a82017-02-10 13:56:22 -0800211void* memset(void* const _Nonnull s __pass_object_size0, int c, size_t n)
George Burgess IV7cc779f2017-02-09 00:00:31 -0800212 __overloadable {
213 return __builtin___memset_chk(s, c, n, __bos0(s));
214}
Dan Albert9c2094f2017-02-14 19:28:18 -0800215#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
George Burgess IV7cc779f2017-02-09 00:00:31 -0800216
217
218#if defined(__clang__)
219
220#define __error_if_overflows_dst(name, dst, n, what) \
221 __enable_if(__bos0(dst) != __BIONIC_FORTIFY_UNKNOWN_SIZE && \
222 __bos0(dst) < (n), "selected when the buffer is too small") \
223 __errorattr(#name " called with " what " bigger than buffer")
224
225/*
226 * N.B. _Nonnull isn't necessary on params, since these functions just emit
227 * errors.
228 */
229__BIONIC_ERROR_FUNCTION_VISIBILITY
230void* memcpy(void* dst, const void* src, size_t copy_amount) __overloadable
231 __error_if_overflows_dst(memcpy, dst, copy_amount, "size");
232
233__BIONIC_ERROR_FUNCTION_VISIBILITY
234void* memmove(void *dst, const void* src, size_t len) __overloadable
235 __error_if_overflows_dst(memmove, dst, len, "size");
236
237__BIONIC_ERROR_FUNCTION_VISIBILITY
238void* memset(void* s, int c, size_t n) __overloadable
239 __error_if_overflows_dst(memset, s, n, "size");
240
241__BIONIC_ERROR_FUNCTION_VISIBILITY
242char* stpcpy(char* dst, const char* src) __overloadable
243 __error_if_overflows_dst(stpcpy, dst, __builtin_strlen(src), "string");
244
245__BIONIC_ERROR_FUNCTION_VISIBILITY
246char* strcpy(char* dst, const char* src) __overloadable
247 __error_if_overflows_dst(strcpy, dst, __builtin_strlen(src), "string");
Nick Kralevich0a230152012-06-04 15:20:25 -0700248
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800249#if __ANDROID_API__ >= __ANDROID_API_M__
Nick Kralevich0a230152012-06-04 15:20:25 -0700250__BIONIC_FORTIFY_INLINE
George Burgess IV7cc779f2017-02-09 00:00:31 -0800251void* memchr(const void* const _Nonnull s __pass_object_size, int c, size_t n)
252 __overloadable {
Daniel Micay4ae77362015-04-17 18:16:57 -0400253 size_t bos = __bos(s);
254
George Burgess IV7cc779f2017-02-09 00:00:31 -0800255 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
256 return __builtin_memchr(s, c, n);
257 }
258
259 return __memchr_chk(s, c, n, bos);
260}
261
262__BIONIC_FORTIFY_INLINE
263void* memrchr(const void* const _Nonnull s __pass_object_size, int c, size_t n)
264 __overloadable {
265 size_t bos = __bos(s);
266
267 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
268 return __call_bypassing_fortify(memrchr)(s, c, n);
269 }
270
271 return __memrchr_chk(s, c, n, bos);
272}
273#endif /* __ANDROID_API__ >= __ANDROID_API_M__ */
274
275#if __ANDROID_API__ >= __ANDROID_API_L__
276__BIONIC_FORTIFY_INLINE
277char* stpncpy(char* __restrict const _Nonnull dst __pass_object_size,
278 const char* __restrict const _Nonnull src __pass_object_size,
279 size_t n) __overloadable {
280 size_t bos_dst = __bos(dst);
281 size_t bos_src = __bos(src);
282
283 /* Ignore dst size checks; they're handled in strncpy_chk */
284 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
285 return __builtin___stpncpy_chk(dst, src, n, bos_dst);
286 }
287
288 return __stpncpy_chk2(dst, src, n, bos_dst, bos_src);
289}
George Burgess IV7cc779f2017-02-09 00:00:31 -0800290
George Burgess IV7cc779f2017-02-09 00:00:31 -0800291__BIONIC_FORTIFY_INLINE
292char* strncpy(char* __restrict const _Nonnull dst __pass_object_size,
293 const char* __restrict const _Nonnull src __pass_object_size,
294 size_t n) __overloadable {
295 size_t bos_dst = __bos(dst);
296 size_t bos_src = __bos(src);
297
298 /* Ignore dst size checks; they're handled in strncpy_chk */
299 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
300 return __builtin___strncpy_chk(dst, src, n, bos_dst);
301 }
302
303 return __strncpy_chk2(dst, src, n, bos_dst, bos_src);
304}
Dan Albert9c2094f2017-02-14 19:28:18 -0800305#endif /* __ANDROID_API__ >= __ANDROID_API_L__ */
George Burgess IV7cc779f2017-02-09 00:00:31 -0800306
Dan Albert9c2094f2017-02-14 19:28:18 -0800307#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
George Burgess IV7cc779f2017-02-09 00:00:31 -0800308__BIONIC_FORTIFY_INLINE
309size_t strlcpy(char* const _Nonnull __restrict dst __pass_object_size,
310 const char *_Nonnull __restrict src, size_t size) __overloadable {
311 size_t bos = __bos(dst);
312
313 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
314 return __call_bypassing_fortify(strlcpy)(dst, src, size);
315 }
316
317 return __strlcpy_chk(dst, src, size, bos);
318}
319
320__BIONIC_FORTIFY_INLINE
321size_t strlcat(char* const _Nonnull __restrict dst __pass_object_size,
322 const char* _Nonnull __restrict src, size_t size) __overloadable {
323 size_t bos = __bos(dst);
324
325 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
326 return __call_bypassing_fortify(strlcat)(dst, src, size);
327 }
328
329 return __strlcat_chk(dst, src, size, bos);
330}
331
332/*
333 * If we can evaluate the size of s at compile-time, just call __builtin_strlen
334 * on it directly. This makes it way easier for compilers to fold things like
335 * strlen("Foo") into a constant, as users would expect. -1ULL is chosen simply
336 * because it's large.
337 */
338__BIONIC_FORTIFY_INLINE
339size_t strlen(const char* const _Nonnull s __pass_object_size)
340 __overloadable __enable_if(__builtin_strlen(s) != -1ULL,
341 "enabled if s is a known good string.") {
342 return __builtin_strlen(s);
343}
344
345__BIONIC_FORTIFY_INLINE
George Burgess IV156d5a82017-02-10 13:56:22 -0800346size_t strlen(const char* const _Nonnull s __pass_object_size0)
George Burgess IV7cc779f2017-02-09 00:00:31 -0800347 __overloadable {
348 size_t bos = __bos0(s);
349
350 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
351 return __builtin_strlen(s);
352 }
353
354 // return __builtin_strlen(s);
355 return __strlen_chk(s, bos);
356}
357#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
358
359#if __ANDROID_API__ >= __ANDROID_API_J_MR2__
360__BIONIC_FORTIFY_INLINE
George Burgess IVbce9a7d2017-04-03 18:03:36 -0700361char* strchr(const char* const _Nonnull s __pass_object_size, int c)
George Burgess IV7cc779f2017-02-09 00:00:31 -0800362 __overloadable {
George Burgess IVbce9a7d2017-04-03 18:03:36 -0700363 size_t bos = __bos(s);
George Burgess IV7cc779f2017-02-09 00:00:31 -0800364
365 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
366 return __builtin_strchr(s, c);
367 }
368
George Burgess IV7cc779f2017-02-09 00:00:31 -0800369 return __strchr_chk(s, c, bos);
370}
371
372__BIONIC_FORTIFY_INLINE
373char* strrchr(const char* const _Nonnull s __pass_object_size, int c)
374 __overloadable {
375 size_t bos = __bos(s);
376
377 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
378 return __builtin_strrchr(s, c);
379 }
380
381 return __strrchr_chk(s, c, bos);
382}
383#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR2__ */
384
Dan Albert9c2094f2017-02-14 19:28:18 -0800385#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
George Burgess IV7cc779f2017-02-09 00:00:31 -0800386/* In *many* cases, memset(foo, sizeof(foo), 0) is a mistake where the user has
387 * flipped the size + value arguments. However, there may be cases (e.g. with
388 * macros) where it's okay for the size to fold to zero. We should warn on this,
389 * but we should also provide a FORTIFY'ed escape hatch.
390 */
391__BIONIC_ERROR_FUNCTION_VISIBILITY
392void* memset(void* _Nonnull s, int c, size_t n,
393 struct __bionic_zero_size_is_okay_t ok)
394 __overloadable
395 __error_if_overflows_dst(memset, s, n, "size");
396
397__BIONIC_FORTIFY_INLINE
George Burgess IV156d5a82017-02-10 13:56:22 -0800398void* memset(void* const _Nonnull s __pass_object_size0, int c, size_t n,
George Burgess IV7cc779f2017-02-09 00:00:31 -0800399 struct __bionic_zero_size_is_okay_t ok __attribute__((unused)))
400 __overloadable {
401 return __builtin___memset_chk(s, c, n, __bos0(s));
402}
403
404extern struct __bionic_zero_size_is_okay_t __bionic_zero_size_is_okay;
405/* We verify that `c` is non-zero, because as pointless as memset(foo, 0, 0) is,
406 * flipping size + count will do nothing.
407 */
408__BIONIC_ERROR_FUNCTION_VISIBILITY
409void* memset(void* _Nonnull s, int c, size_t n) __overloadable
410 __enable_if(c && !n, "selected when we'll set zero bytes")
411 __RENAME_CLANG(memset)
412 __warnattr_real("will set 0 bytes; maybe the arguments got flipped? "
413 "(Add __bionic_zero_size_is_okay as a fourth argument "
414 "to silence this.)");
Dan Albert9c2094f2017-02-14 19:28:18 -0800415#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
George Burgess IV7cc779f2017-02-09 00:00:31 -0800416
417#undef __error_zero_size
418#undef __error_if_overflows_dst
419#else // defined(__clang__)
420extern char* __strncpy_real(char* __restrict, const char*, size_t) __RENAME(strcpy);
421extern void* __memrchr_real(const void*, int, size_t) __RENAME(memrchr);
422extern size_t __strlcpy_real(char* __restrict, const char* __restrict, size_t)
423 __RENAME(strlcpy);
424extern size_t __strlcat_real(char* __restrict, const char* __restrict, size_t)
425 __RENAME(strlcat);
426
427__errordecl(__memchr_buf_size_error, "memchr called with size bigger than buffer");
428__errordecl(__memrchr_buf_size_error, "memrchr called with size bigger than buffer");
429
430#if __ANDROID_API__ >= __ANDROID_API_M__
431__BIONIC_FORTIFY_INLINE
432void* memchr(const void *_Nonnull s __pass_object_size, int c, size_t n) {
433 size_t bos = __bos(s);
434
Daniel Micay4ae77362015-04-17 18:16:57 -0400435 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
436 return __builtin_memchr(s, c, n);
437 }
438
439 if (__builtin_constant_p(n) && (n > bos)) {
440 __memchr_buf_size_error();
441 }
442
443 if (__builtin_constant_p(n) && (n <= bos)) {
444 return __builtin_memchr(s, c, n);
445 }
Daniel Micay4ae77362015-04-17 18:16:57 -0400446
447 return __memchr_chk(s, c, n, bos);
448}
449
450__BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700451void* memrchr(const void* s, int c, size_t n) {
Daniel Micay4ae77362015-04-17 18:16:57 -0400452 size_t bos = __bos(s);
453
Daniel Micay4ae77362015-04-17 18:16:57 -0400454 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
455 return __memrchr_real(s, c, n);
456 }
457
458 if (__builtin_constant_p(n) && (n > bos)) {
459 __memrchr_buf_size_error();
460 }
461
462 if (__builtin_constant_p(n) && (n <= bos)) {
463 return __memrchr_real(s, c, n);
464 }
Daniel Micay4ae77362015-04-17 18:16:57 -0400465
466 return __memrchr_chk(s, c, n, bos);
467}
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800468#endif /* __ANDROID_API__ >= __ANDROID_API_M__ */
Daniel Micay4ae77362015-04-17 18:16:57 -0400469
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800470#if __ANDROID_API__ >= __ANDROID_API_L__
Christopher Ferris950a58e2014-04-04 14:38:18 -0700471__BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700472char* stpncpy(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src, size_t n) {
473 size_t bos_dst = __bos(dst);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700474 size_t bos_src = __bos(src);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700475
476 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Elliott Hughes5470c182016-07-22 11:36:17 -0700477 return __builtin___stpncpy_chk(dst, src, n, bos_dst);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700478 }
479
480 if (__builtin_constant_p(n) && (n <= bos_src)) {
Elliott Hughes5470c182016-07-22 11:36:17 -0700481 return __builtin___stpncpy_chk(dst, src, n, bos_dst);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700482 }
483
484 size_t slen = __builtin_strlen(src);
485 if (__builtin_constant_p(slen)) {
Elliott Hughes5470c182016-07-22 11:36:17 -0700486 return __builtin___stpncpy_chk(dst, src, n, bos_dst);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700487 }
488
Elliott Hughes5470c182016-07-22 11:36:17 -0700489 return __stpncpy_chk2(dst, src, n, bos_dst, bos_src);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700490}
491
Nick Kralevich0a230152012-06-04 15:20:25 -0700492__BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700493char* strncpy(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src, size_t n) {
494 size_t bos_dst = __bos(dst);
Nick Kralevich93501d32013-08-28 10:47:43 -0700495 size_t bos_src = __bos(src);
Nick Kralevich93501d32013-08-28 10:47:43 -0700496
497 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
George Burgess IV7cc779f2017-02-09 00:00:31 -0800498 return __strncpy_real(dst, src, n);
Nick Kralevich93501d32013-08-28 10:47:43 -0700499 }
500
Nick Kralevichd13c2b12013-09-27 13:21:24 -0700501 if (__builtin_constant_p(n) && (n <= bos_src)) {
Elliott Hughes5470c182016-07-22 11:36:17 -0700502 return __builtin___strncpy_chk(dst, src, n, bos_dst);
Nick Kralevichd13c2b12013-09-27 13:21:24 -0700503 }
504
Nick Kralevich93501d32013-08-28 10:47:43 -0700505 size_t slen = __builtin_strlen(src);
506 if (__builtin_constant_p(slen)) {
Elliott Hughes5470c182016-07-22 11:36:17 -0700507 return __builtin___strncpy_chk(dst, src, n, bos_dst);
Nick Kralevich93501d32013-08-28 10:47:43 -0700508 }
509
Elliott Hughes5470c182016-07-22 11:36:17 -0700510 return __strncpy_chk2(dst, src, n, bos_dst, bos_src);
Nick Kralevich0a230152012-06-04 15:20:25 -0700511}
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800512#endif /* __ANDROID_API__ >= __ANDROID_API_L__ */
Nick Kralevich0a230152012-06-04 15:20:25 -0700513
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800514#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
Nick Kralevich0a230152012-06-04 15:20:25 -0700515__BIONIC_FORTIFY_INLINE
George Burgess IV7cc779f2017-02-09 00:00:31 -0800516size_t strlcpy(char* _Nonnull __restrict dst __pass_object_size,
517 const char* _Nonnull __restrict src, size_t size) {
Elliott Hughes5470c182016-07-22 11:36:17 -0700518 size_t bos = __bos(dst);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700519
520 // Compiler doesn't know destination size. Don't call __strlcpy_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700521 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Elliott Hughes5470c182016-07-22 11:36:17 -0700522 return __strlcpy_real(dst, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700523 }
524
525 // Compiler can prove, at compile time, that the passed in size
526 // is always <= the actual object size. Don't call __strlcpy_chk
527 if (__builtin_constant_p(size) && (size <= bos)) {
Elliott Hughes5470c182016-07-22 11:36:17 -0700528 return __strlcpy_real(dst, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700529 }
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700530
Elliott Hughes5470c182016-07-22 11:36:17 -0700531 return __strlcpy_chk(dst, src, size, bos);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700532}
533
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700534__BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700535size_t strlcat(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src, size_t size) {
536 size_t bos = __bos(dst);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700537
538 // Compiler doesn't know destination size. Don't call __strlcat_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700539 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Elliott Hughes5470c182016-07-22 11:36:17 -0700540 return __strlcat_real(dst, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700541 }
542
543 // Compiler can prove, at compile time, that the passed in size
544 // is always <= the actual object size. Don't call __strlcat_chk
545 if (__builtin_constant_p(size) && (size <= bos)) {
Elliott Hughes5470c182016-07-22 11:36:17 -0700546 return __strlcat_real(dst, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700547 }
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700548
Elliott Hughes5470c182016-07-22 11:36:17 -0700549 return __strlcat_chk(dst, src, size, bos);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700550}
551
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700552__BIONIC_FORTIFY_INLINE
George Burgess IV7cc779f2017-02-09 00:00:31 -0800553size_t strlen(const char* _Nonnull s) __overloadable {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700554 size_t bos = __bos(s);
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700555
556 // Compiler doesn't know destination size. Don't call __strlen_chk
557 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800558 return __builtin_strlen(s);
559 }
560
561 size_t slen = __builtin_strlen(s);
562 if (__builtin_constant_p(slen)) {
563 return slen;
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700564 }
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700565
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700566 return __strlen_chk(s, bos);
567}
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800568#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700569
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800570#if __ANDROID_API__ >= __ANDROID_API_J_MR2__
Nick Kralevich049e5832012-11-30 15:15:58 -0800571__BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700572char* strchr(const char* _Nonnull s, int c) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700573 size_t bos = __bos(s);
Nick Kralevich049e5832012-11-30 15:15:58 -0800574
575 // Compiler doesn't know destination size. Don't call __strchr_chk
576 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800577 return __builtin_strchr(s, c);
578 }
579
580 size_t slen = __builtin_strlen(s);
581 if (__builtin_constant_p(slen) && (slen < bos)) {
582 return __builtin_strchr(s, c);
Nick Kralevich049e5832012-11-30 15:15:58 -0800583 }
584
585 return __strchr_chk(s, c, bos);
586}
587
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800588__BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700589char* strrchr(const char* _Nonnull s, int c) {
Nick Kralevich3b2e6bc2013-04-30 14:19:23 -0700590 size_t bos = __bos(s);
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800591
592 // Compiler doesn't know destination size. Don't call __strrchr_chk
593 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800594 return __builtin_strrchr(s, c);
595 }
596
597 size_t slen = __builtin_strlen(s);
598 if (__builtin_constant_p(slen) && (slen < bos)) {
599 return __builtin_strrchr(s, c);
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800600 }
601
602 return __strrchr_chk(s, c, bos);
603}
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800604#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR2__ */
George Burgess IV7cc779f2017-02-09 00:00:31 -0800605#endif /* defined(__clang__) */
Elliott Hughes890c8ed2013-03-22 10:58:55 -0700606#endif /* defined(__BIONIC_FORTIFY) */
Nick Kralevich0a230152012-06-04 15:20:25 -0700607
George Burgess IVbd3d2082017-04-04 17:34:02 -0700608/* Const-correct overloads. Placed after FORTIFY so we call those functions, if possible. */
609#if defined(__cplusplus) && defined(__clang__)
610/*
611 * Use two enable_ifs so these overloads don't conflict with + are preferred over libcxx's. This can
612 * be reduced to 1 after libcxx recognizes that we have const-correct overloads.
613 */
614#define __prefer_this_overload __enable_if(true, "preferred overload") __enable_if(true, "")
615extern "C++" {
616inline __always_inline
617void* __bionic_memchr(const void* const _Nonnull s __pass_object_size, int c, size_t n) {
618 return memchr(s, c, n);
619}
620
621inline __always_inline
622const void* memchr(const void* const _Nonnull s __pass_object_size, int c, size_t n)
623 __prefer_this_overload {
624 return __bionic_memchr(s, c, n);
625}
626
627inline __always_inline
628void* memchr(void* const _Nonnull s __pass_object_size, int c, size_t n) __prefer_this_overload {
629 return __bionic_memchr(s, c, n);
630}
631
632inline __always_inline
633char* __bionic_strchr(const char* const _Nonnull s __pass_object_size, int c) {
634 return strchr(s, c);
635}
636
637inline __always_inline
638const char* strchr(const char* const _Nonnull s __pass_object_size, int c)
639 __prefer_this_overload {
640 return __bionic_strchr(s, c);
641}
642
643inline __always_inline
644char* strchr(char* const _Nonnull s __pass_object_size, int c)
645 __prefer_this_overload {
646 return __bionic_strchr(s, c);
647}
648
649inline __always_inline
650char* __bionic_strrchr(const char* const _Nonnull s __pass_object_size, int c) {
651 return strrchr(s, c);
652}
653
654inline __always_inline
655const char* strrchr(const char* const _Nonnull s __pass_object_size, int c) __prefer_this_overload {
656 return __bionic_strrchr(s, c);
657}
658
659inline __always_inline
660char* strrchr(char* const _Nonnull s __pass_object_size, int c) __prefer_this_overload {
661 return __bionic_strrchr(s, c);
662}
663
664/* Functions with no FORTIFY counterpart. */
665inline __always_inline
666char* __bionic_strstr(const char* _Nonnull h, const char* _Nonnull n) { return strstr(h, n); }
667
668inline __always_inline
669const char* strstr(const char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload {
670 return __bionic_strstr(h, n);
671}
672
673inline __always_inline
674char* strstr(char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload {
675 return __bionic_strstr(h, n);
676}
677
678inline __always_inline
679char* __bionic_strpbrk(const char* _Nonnull h, const char* _Nonnull n) { return strpbrk(h, n); }
680
681inline __always_inline
682char* strpbrk(char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload {
683 return __bionic_strpbrk(h, n);
684}
685
686inline __always_inline
687const char* strpbrk(const char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload {
688 return __bionic_strpbrk(h, n);
689}
690}
691#undef __prefer_this_overload
692#endif
693
Elliott Hughes5470c182016-07-22 11:36:17 -0700694#if defined(__clang__)
695#pragma clang diagnostic pop
696#endif
697
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800698__END_DECLS
699
Elliott Hughes09c39d62014-08-19 14:30:30 -0700700#endif /* _STRING_H */