blob: 9be2b1a4523b847ea8b700ea264ee9ebe2e41d48 [file] [log] [blame]
George Burgess IVb97049c2017-07-24 15:05:05 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#ifndef _STRING_H
30#error "Never include this file directly; instead, include <string.h>"
31#endif
32
Elliott Hughes3f66e742017-08-01 13:24:40 -070033void* __memchr_chk(const void*, int, size_t, size_t) __INTRODUCED_IN(23);
34void* __memrchr_chk(const void*, int, size_t, size_t) __INTRODUCED_IN(23);
35char* __stpncpy_chk2(char*, const char*, size_t, size_t, size_t) __INTRODUCED_IN(21);
36char* __strncpy_chk2(char*, const char*, size_t, size_t, size_t) __INTRODUCED_IN(21);
37size_t __strlcpy_chk(char*, const char*, size_t, size_t) __INTRODUCED_IN(17);
38size_t __strlcat_chk(char*, const char*, size_t, size_t) __INTRODUCED_IN(17);
George Burgess IVb97049c2017-07-24 15:05:05 -070039
George Burgess IVb97049c2017-07-24 15:05:05 -070040#if defined(__BIONIC_FORTIFY)
Elliott Hughesdf9a4892017-08-23 14:34:03 -070041extern void* __memrchr_real(const void*, int, size_t) __RENAME(memrchr);
42
George Burgess IVb97049c2017-07-24 15:05:05 -070043#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
George Burgess IV36926f42019-09-15 16:57:00 -070044/* No diag -- clang diagnoses misuses of this on its own. */
George Burgess IVb97049c2017-07-24 15:05:05 -070045__BIONIC_FORTIFY_INLINE
George Burgess IVb6300462017-07-31 21:29:42 -070046void* memcpy(void* const dst __pass_object_size0, const void* src, size_t copy_amount)
George Burgess IV36926f42019-09-15 16:57:00 -070047 __overloadable {
George Burgess IV5da5dd52019-05-09 14:32:43 -070048 size_t bos_dst = __bos0(dst);
49 if (__bos_trivially_not_lt(bos_dst, copy_amount)) {
50 return __builtin_memcpy(dst, src, copy_amount);
51 }
52 return __builtin___memcpy_chk(dst, src, copy_amount, bos_dst);
George Burgess IVb97049c2017-07-24 15:05:05 -070053}
54
George Burgess IV36926f42019-09-15 16:57:00 -070055/* No diag -- clang diagnoses misuses of this on its own. */
George Burgess IVb97049c2017-07-24 15:05:05 -070056__BIONIC_FORTIFY_INLINE
George Burgess IV36926f42019-09-15 16:57:00 -070057void* memmove(void* const dst __pass_object_size0, const void* src, size_t len) __overloadable {
George Burgess IV5da5dd52019-05-09 14:32:43 -070058 size_t bos_dst = __bos0(dst);
59 if (__bos_trivially_not_lt(bos_dst, len)) {
60 return __builtin_memmove(dst, src, len);
61 }
62 return __builtin___memmove_chk(dst, src, len, bos_dst);
George Burgess IVb97049c2017-07-24 15:05:05 -070063}
64#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
65
George Burgess IV849c0b92019-06-10 16:22:09 -070066#if defined(__USE_GNU)
67#if __ANDROID_API__ >= __ANDROID_API_R__
68__BIONIC_FORTIFY_INLINE
69void* mempcpy(void* const dst __pass_object_size0, const void* src, size_t copy_amount)
70 __overloadable
71 __clang_error_if(__bos_unevaluated_lt(__bos0(dst), copy_amount),
72 "'mempcpy' called with size bigger than buffer") {
73 size_t bos_dst = __bos0(dst);
74 if (__bos_trivially_not_lt(bos_dst, copy_amount)) {
75 return __builtin_mempcpy(dst, src, copy_amount);
76 }
77 return __builtin___mempcpy_chk(dst, src, copy_amount, bos_dst);
78}
79#endif /* __ANDROID_API__ >= __ANDROID_API_R__ */
80#endif
81
George Burgess IVb97049c2017-07-24 15:05:05 -070082#if __ANDROID_API__ >= __ANDROID_API_L__
83__BIONIC_FORTIFY_INLINE
George Burgess IVb6300462017-07-31 21:29:42 -070084char* stpcpy(char* const dst __pass_object_size, const char* src)
85 __overloadable
George Burgess IVc03d5962019-05-23 15:22:01 -070086 __clang_error_if(__bos_unevaluated_le(__bos(dst), __builtin_strlen(src)),
George Burgess IVb6300462017-07-31 21:29:42 -070087 "'stpcpy' called with string bigger than buffer") {
George Burgess IV5da5dd52019-05-09 14:32:43 -070088 size_t bos_dst = __bos(dst);
George Burgess IVc03d5962019-05-23 15:22:01 -070089 if (__bos_trivially_not_le(bos_dst, __builtin_strlen(src))) {
George Burgess IV5da5dd52019-05-09 14:32:43 -070090 return __builtin_stpcpy(dst, src);
91 }
92 return __builtin___stpcpy_chk(dst, src, bos_dst);
George Burgess IVb97049c2017-07-24 15:05:05 -070093}
94#endif /* __ANDROID_API__ >= __ANDROID_API_L__ */
95
96#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
97__BIONIC_FORTIFY_INLINE
George Burgess IVb6300462017-07-31 21:29:42 -070098char* strcpy(char* const dst __pass_object_size, const char* src)
99 __overloadable
George Burgess IVc03d5962019-05-23 15:22:01 -0700100 __clang_error_if(__bos_unevaluated_le(__bos(dst), __builtin_strlen(src)),
George Burgess IVb6300462017-07-31 21:29:42 -0700101 "'strcpy' called with string bigger than buffer") {
George Burgess IV5da5dd52019-05-09 14:32:43 -0700102 size_t bos_dst = __bos(dst);
George Burgess IVc03d5962019-05-23 15:22:01 -0700103 if (__bos_trivially_not_le(bos_dst, __builtin_strlen(src))) {
George Burgess IV5da5dd52019-05-09 14:32:43 -0700104 return __builtin_strcpy(dst, src);
105 }
106 return __builtin___strcpy_chk(dst, src, bos_dst);
George Burgess IVb97049c2017-07-24 15:05:05 -0700107}
108
109__BIONIC_FORTIFY_INLINE
George Burgess IV77f99aa2019-06-06 14:14:52 -0700110char* strcat(char* const dst __pass_object_size, const char* src)
111 __overloadable
112 __clang_error_if(__bos_unevaluated_le(__bos(dst), __builtin_strlen(src)),
113 "'strcat' called with string bigger than buffer") {
George Burgess IVb97049c2017-07-24 15:05:05 -0700114 return __builtin___strcat_chk(dst, src, __bos(dst));
115}
116
George Burgess IV36926f42019-09-15 16:57:00 -0700117/* No diag -- clang diagnoses misuses of this on its own. */
George Burgess IVb97049c2017-07-24 15:05:05 -0700118__BIONIC_FORTIFY_INLINE
George Burgess IV36926f42019-09-15 16:57:00 -0700119char* strncat(char* const dst __pass_object_size, const char* src, size_t n) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -0700120 return __builtin___strncat_chk(dst, src, n, __bos(dst));
121}
122
George Burgess IV36926f42019-09-15 16:57:00 -0700123/* No diag -- clang diagnoses misuses of this on its own. */
George Burgess IVb97049c2017-07-24 15:05:05 -0700124__BIONIC_FORTIFY_INLINE
George Burgess IV36926f42019-09-15 16:57:00 -0700125void* memset(void* const s __pass_object_size0, int c, size_t n) __overloadable
George Burgess IVb6300462017-07-31 21:29:42 -0700126 /* If you're a user who wants this warning to go away: use `(&memset)(foo, bar, baz)`. */
127 __clang_warning_if(c && !n, "'memset' will set 0 bytes; maybe the arguments got flipped?") {
George Burgess IV5da5dd52019-05-09 14:32:43 -0700128 size_t bos = __bos0(s);
129 if (__bos_trivially_not_lt(bos, n)) {
130 return __builtin_memset(s, c, n);
131 }
132 return __builtin___memset_chk(s, c, n, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700133}
134#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
135
George Burgess IVb97049c2017-07-24 15:05:05 -0700136#if __ANDROID_API__ >= __ANDROID_API_M__
137__BIONIC_FORTIFY_INLINE
George Burgess IVb6300462017-07-31 21:29:42 -0700138void* memchr(const void* const s __pass_object_size, int c, size_t n) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -0700139 size_t bos = __bos(s);
140
George Burgess IVc03d5962019-05-23 15:22:01 -0700141 if (__bos_trivially_ge(bos, n)) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700142 return __builtin_memchr(s, c, n);
143 }
144
145 return __memchr_chk(s, c, n, bos);
146}
147
148__BIONIC_FORTIFY_INLINE
Elliott Hughesdf9a4892017-08-23 14:34:03 -0700149void* __memrchr_fortify(const void* const __pass_object_size s, int c, size_t n) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -0700150 size_t bos = __bos(s);
151
George Burgess IVc03d5962019-05-23 15:22:01 -0700152 if (__bos_trivially_ge(bos, n)) {
Elliott Hughesdf9a4892017-08-23 14:34:03 -0700153 return __memrchr_real(s, c, n);
George Burgess IVb97049c2017-07-24 15:05:05 -0700154 }
155
156 return __memrchr_chk(s, c, n, bos);
157}
158#endif /* __ANDROID_API__ >= __ANDROID_API_M__ */
159
160#if __ANDROID_API__ >= __ANDROID_API_L__
George Burgess IV36926f42019-09-15 16:57:00 -0700161/* No diag -- clang diagnoses misuses of this on its own. */
George Burgess IVb97049c2017-07-24 15:05:05 -0700162__BIONIC_FORTIFY_INLINE
Elliott Hughes3f66e742017-08-01 13:24:40 -0700163char* stpncpy(char* const dst __pass_object_size, const char* const src __pass_object_size, size_t n)
George Burgess IV36926f42019-09-15 16:57:00 -0700164 __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -0700165 size_t bos_dst = __bos(dst);
166 size_t bos_src = __bos(src);
167
168 /* Ignore dst size checks; they're handled in strncpy_chk */
169 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
170 return __builtin___stpncpy_chk(dst, src, n, bos_dst);
171 }
172
173 return __stpncpy_chk2(dst, src, n, bos_dst, bos_src);
174}
175
George Burgess IV36926f42019-09-15 16:57:00 -0700176/* No diag -- clang diagnoses misuses of this on its own. */
George Burgess IVb97049c2017-07-24 15:05:05 -0700177__BIONIC_FORTIFY_INLINE
Elliott Hughes3f66e742017-08-01 13:24:40 -0700178char* strncpy(char* const dst __pass_object_size, const char* const src __pass_object_size, size_t n)
George Burgess IV36926f42019-09-15 16:57:00 -0700179 __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -0700180 size_t bos_dst = __bos(dst);
181 size_t bos_src = __bos(src);
182
183 /* Ignore dst size checks; they're handled in strncpy_chk */
184 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
185 return __builtin___strncpy_chk(dst, src, n, bos_dst);
186 }
187
188 return __strncpy_chk2(dst, src, n, bos_dst, bos_src);
189}
190#endif /* __ANDROID_API__ >= __ANDROID_API_L__ */
191
192#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
193__BIONIC_FORTIFY_INLINE
George Burgess IV77f99aa2019-06-06 14:14:52 -0700194size_t strlcpy(char* const dst __pass_object_size, const char* src, size_t size)
195 __overloadable
196 __clang_error_if(__bos_unevaluated_lt(__bos(dst), size),
197 "'strlcpy' called with size bigger than buffer") {
George Burgess IVb97049c2017-07-24 15:05:05 -0700198 size_t bos = __bos(dst);
199
200 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
201 return __call_bypassing_fortify(strlcpy)(dst, src, size);
202 }
203
204 return __strlcpy_chk(dst, src, size, bos);
205}
206
207__BIONIC_FORTIFY_INLINE
George Burgess IV77f99aa2019-06-06 14:14:52 -0700208size_t strlcat(char* const dst __pass_object_size, const char* src, size_t size)
209 __overloadable
210 __clang_error_if(__bos_unevaluated_lt(__bos(dst), size),
211 "'strlcat' called with size bigger than buffer") {
George Burgess IVb97049c2017-07-24 15:05:05 -0700212 size_t bos = __bos(dst);
213
214 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
215 return __call_bypassing_fortify(strlcat)(dst, src, size);
216 }
217
218 return __strlcat_chk(dst, src, size, bos);
219}
220
George Burgess IVb97049c2017-07-24 15:05:05 -0700221__BIONIC_FORTIFY_INLINE
George Burgess IVb6300462017-07-31 21:29:42 -0700222size_t strlen(const char* const s __pass_object_size0) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -0700223 size_t bos = __bos0(s);
224
George Burgess IVa1a09b22019-05-13 17:16:20 -0700225 if (__bos_trivially_gt(bos, __builtin_strlen(s))) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700226 return __builtin_strlen(s);
227 }
228
George Burgess IVb97049c2017-07-24 15:05:05 -0700229 return __strlen_chk(s, bos);
230}
231#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
232
233#if __ANDROID_API__ >= __ANDROID_API_J_MR2__
234__BIONIC_FORTIFY_INLINE
Elliott Hughes3f66e742017-08-01 13:24:40 -0700235char* strchr(const char* const s __pass_object_size, int c) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -0700236 size_t bos = __bos(s);
237
238 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
239 return __builtin_strchr(s, c);
240 }
241
242 return __strchr_chk(s, c, bos);
243}
244
245__BIONIC_FORTIFY_INLINE
Elliott Hughes3f66e742017-08-01 13:24:40 -0700246char* strrchr(const char* const s __pass_object_size, int c) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -0700247 size_t bos = __bos(s);
248
249 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
250 return __builtin_strrchr(s, c);
251 }
252
253 return __strrchr_chk(s, c, bos);
254}
255#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR2__ */
256
Elliott Hughesdf9a4892017-08-23 14:34:03 -0700257#if __ANDROID_API__ >= __ANDROID_API_M__
258#if defined(__cplusplus)
259extern "C++" {
260__BIONIC_FORTIFY_INLINE
261void* memrchr(void* const __pass_object_size s, int c, size_t n) {
262 return __memrchr_fortify(s, c, n);
263}
264
265__BIONIC_FORTIFY_INLINE
266const void* memrchr(const void* const __pass_object_size s, int c, size_t n) {
267 return __memrchr_fortify(s, c, n);
268}
269}
270#else
271__BIONIC_FORTIFY_INLINE
272void* memrchr(const void* const __pass_object_size s, int c, size_t n) __overloadable {
273 return __memrchr_fortify(s, c, n);
274}
275#endif
276#endif /* __ANDROID_API__ >= __ANDROID_API_M__ */
277
George Burgess IVb97049c2017-07-24 15:05:05 -0700278#endif /* defined(__BIONIC_FORTIFY) */