/src/boringssl/crypto/mem.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
2 | | * All rights reserved. |
3 | | * |
4 | | * This package is an SSL implementation written |
5 | | * by Eric Young (eay@cryptsoft.com). |
6 | | * The implementation was written so as to conform with Netscapes SSL. |
7 | | * |
8 | | * This library is free for commercial and non-commercial use as long as |
9 | | * the following conditions are aheared to. The following conditions |
10 | | * apply to all code found in this distribution, be it the RC4, RSA, |
11 | | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
12 | | * included with this distribution is covered by the same copyright terms |
13 | | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
14 | | * |
15 | | * Copyright remains Eric Young's, and as such any Copyright notices in |
16 | | * the code are not to be removed. |
17 | | * If this package is used in a product, Eric Young should be given attribution |
18 | | * as the author of the parts of the library used. |
19 | | * This can be in the form of a textual message at program startup or |
20 | | * in documentation (online or textual) provided with the package. |
21 | | * |
22 | | * Redistribution and use in source and binary forms, with or without |
23 | | * modification, are permitted provided that the following conditions |
24 | | * are met: |
25 | | * 1. Redistributions of source code must retain the copyright |
26 | | * notice, this list of conditions and the following disclaimer. |
27 | | * 2. Redistributions in binary form must reproduce the above copyright |
28 | | * notice, this list of conditions and the following disclaimer in the |
29 | | * documentation and/or other materials provided with the distribution. |
30 | | * 3. All advertising materials mentioning features or use of this software |
31 | | * must display the following acknowledgement: |
32 | | * "This product includes cryptographic software written by |
33 | | * Eric Young (eay@cryptsoft.com)" |
34 | | * The word 'cryptographic' can be left out if the rouines from the library |
35 | | * being used are not cryptographic related :-). |
36 | | * 4. If you include any Windows specific code (or a derivative thereof) from |
37 | | * the apps directory (application code) you must include an acknowledgement: |
38 | | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
39 | | * |
40 | | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
41 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
42 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
43 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
44 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
45 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
46 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
47 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
48 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
49 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
50 | | * SUCH DAMAGE. |
51 | | * |
52 | | * The licence and distribution terms for any publically available version or |
53 | | * derivative of this code cannot be changed. i.e. this code cannot simply be |
54 | | * copied and put under another distribution licence |
55 | | * [including the GNU Public Licence.] */ |
56 | | |
57 | | #include <openssl/mem.h> |
58 | | |
59 | | #include <assert.h> |
60 | | #include <errno.h> |
61 | | #include <limits.h> |
62 | | #include <stdarg.h> |
63 | | #include <stdio.h> |
64 | | #include <stdlib.h> |
65 | | |
66 | | #include <openssl/err.h> |
67 | | |
68 | | #if defined(OPENSSL_WINDOWS) |
69 | | OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
70 | | #include <windows.h> |
71 | | OPENSSL_MSVC_PRAGMA(warning(pop)) |
72 | | #endif |
73 | | |
74 | | #if defined(BORINGSSL_MALLOC_FAILURE_TESTING) |
75 | | #include <errno.h> |
76 | | #include <signal.h> |
77 | | #include <unistd.h> |
78 | | #endif |
79 | | |
80 | | #include "internal.h" |
81 | | |
82 | | |
83 | 77.0M | #define OPENSSL_MALLOC_PREFIX 8 |
84 | | static_assert(OPENSSL_MALLOC_PREFIX >= sizeof(size_t), "size_t too large"); |
85 | | |
86 | | #if defined(OPENSSL_ASAN) |
87 | | void __asan_poison_memory_region(const volatile void *addr, size_t size); |
88 | | void __asan_unpoison_memory_region(const volatile void *addr, size_t size); |
89 | | #else |
90 | 11.1M | static void __asan_poison_memory_region(const void *addr, size_t size) {} |
91 | 11.1M | static void __asan_unpoison_memory_region(const void *addr, size_t size) {} |
92 | | #endif |
93 | | |
94 | | // Windows doesn't really support weak symbols as of May 2019, and Clang on |
95 | | // Windows will emit strong symbols instead. See |
96 | | // https://bugs.llvm.org/show_bug.cgi?id=37598 |
97 | | #if defined(__ELF__) && defined(__GNUC__) |
98 | | #define WEAK_SYMBOL_FUNC(rettype, name, args) \ |
99 | | rettype name args __attribute__((weak)); |
100 | | #else |
101 | | #define WEAK_SYMBOL_FUNC(rettype, name, args) static rettype(*name) args = NULL; |
102 | | #endif |
103 | | |
104 | | // sdallocx is a sized |free| function. By passing the size (which we happen to |
105 | | // always know in BoringSSL), the malloc implementation can save work. We cannot |
106 | | // depend on |sdallocx| being available, however, so it's a weak symbol. |
107 | | // |
108 | | // This will always be safe, but will only be overridden if the malloc |
109 | | // implementation is statically linked with BoringSSL. So, if |sdallocx| is |
110 | | // provided in, say, libc.so, we still won't use it because that's dynamically |
111 | | // linked. This isn't an ideal result, but its helps in some cases. |
112 | | WEAK_SYMBOL_FUNC(void, sdallocx, (void *ptr, size_t size, int flags)); |
113 | | |
114 | | // The following three functions can be defined to override default heap |
115 | | // allocation and freeing. If defined, it is the responsibility of |
116 | | // |OPENSSL_memory_free| to zero out the memory before returning it to the |
117 | | // system. |OPENSSL_memory_free| will not be passed NULL pointers. |
118 | | // |
119 | | // WARNING: These functions are called on every allocation and free in |
120 | | // BoringSSL across the entire process. They may be called by any code in the |
121 | | // process which calls BoringSSL, including in process initializers and thread |
122 | | // destructors. When called, BoringSSL may hold pthreads locks. Any other code |
123 | | // in the process which, directly or indirectly, calls BoringSSL may be on the |
124 | | // call stack and may itself be using arbitrary synchronization primitives. |
125 | | // |
126 | | // As a result, these functions may not have the usual programming environment |
127 | | // available to most C or C++ code. In particular, they may not call into |
128 | | // BoringSSL, or any library which depends on BoringSSL. Any synchronization |
129 | | // primitives used must tolerate every other synchronization primitive linked |
130 | | // into the process, including pthreads locks. Failing to meet these constraints |
131 | | // may result in deadlocks, crashes, or memory corruption. |
132 | | WEAK_SYMBOL_FUNC(void *, OPENSSL_memory_alloc, (size_t size)); |
133 | | WEAK_SYMBOL_FUNC(void, OPENSSL_memory_free, (void *ptr)); |
134 | | WEAK_SYMBOL_FUNC(size_t, OPENSSL_memory_get_size, (void *ptr)); |
135 | | |
136 | | // kBoringSSLBinaryTag is a distinctive byte sequence to identify binaries that |
137 | | // are linking in BoringSSL and, roughly, what version they are using. |
138 | | static const uint8_t kBoringSSLBinaryTag[18] = { |
139 | | // 16 bytes of magic tag. |
140 | | 0x8c, |
141 | | 0x62, |
142 | | 0x20, |
143 | | 0x0b, |
144 | | 0xd2, |
145 | | 0xa0, |
146 | | 0x72, |
147 | | 0x58, |
148 | | 0x44, |
149 | | 0xa8, |
150 | | 0x96, |
151 | | 0x69, |
152 | | 0xad, |
153 | | 0x55, |
154 | | 0x7e, |
155 | | 0xec, |
156 | | // Current source iteration. Incremented ~monthly. |
157 | | 3, |
158 | | 0, |
159 | | }; |
160 | | |
161 | | #if defined(BORINGSSL_MALLOC_FAILURE_TESTING) |
162 | | static CRYPTO_MUTEX malloc_failure_lock = CRYPTO_MUTEX_INIT; |
163 | | static uint64_t current_malloc_count = 0; |
164 | | static uint64_t malloc_number_to_fail = 0; |
165 | | static int malloc_failure_enabled = 0, break_on_malloc_fail = 0, |
166 | | any_malloc_failed = 0; |
167 | | |
168 | | static void malloc_exit_handler(void) { |
169 | | CRYPTO_MUTEX_lock_read(&malloc_failure_lock); |
170 | | if (any_malloc_failed) { |
171 | | // Signal to the test driver that some allocation failed, so it knows to |
172 | | // increment the counter and continue. |
173 | | _exit(88); |
174 | | } |
175 | | CRYPTO_MUTEX_unlock_read(&malloc_failure_lock); |
176 | | } |
177 | | |
178 | | static void init_malloc_failure(void) { |
179 | | const char *env = getenv("MALLOC_NUMBER_TO_FAIL"); |
180 | | if (env != NULL && env[0] != 0) { |
181 | | char *endptr; |
182 | | malloc_number_to_fail = strtoull(env, &endptr, 10); |
183 | | if (*endptr == 0) { |
184 | | malloc_failure_enabled = 1; |
185 | | atexit(malloc_exit_handler); |
186 | | } |
187 | | } |
188 | | break_on_malloc_fail = getenv("MALLOC_BREAK_ON_FAIL") != NULL; |
189 | | } |
190 | | |
191 | | // should_fail_allocation returns one if the current allocation should fail and |
192 | | // zero otherwise. |
193 | | static int should_fail_allocation() { |
194 | | static CRYPTO_once_t once = CRYPTO_ONCE_INIT; |
195 | | CRYPTO_once(&once, init_malloc_failure); |
196 | | if (!malloc_failure_enabled) { |
197 | | return 0; |
198 | | } |
199 | | |
200 | | // We lock just so multi-threaded tests are still correct, but we won't test |
201 | | // every malloc exhaustively. |
202 | | CRYPTO_MUTEX_lock_write(&malloc_failure_lock); |
203 | | int should_fail = current_malloc_count == malloc_number_to_fail; |
204 | | current_malloc_count++; |
205 | | any_malloc_failed = any_malloc_failed || should_fail; |
206 | | CRYPTO_MUTEX_unlock_write(&malloc_failure_lock); |
207 | | |
208 | | if (should_fail && break_on_malloc_fail) { |
209 | | raise(SIGTRAP); |
210 | | } |
211 | | if (should_fail) { |
212 | | errno = ENOMEM; |
213 | | } |
214 | | return should_fail; |
215 | | } |
216 | | |
217 | | void OPENSSL_reset_malloc_counter_for_testing(void) { |
218 | | CRYPTO_MUTEX_lock_write(&malloc_failure_lock); |
219 | | current_malloc_count = 0; |
220 | | CRYPTO_MUTEX_unlock_write(&malloc_failure_lock); |
221 | | } |
222 | | |
223 | | #else |
224 | 10.9M | static int should_fail_allocation(void) { return 0; } |
225 | | #endif |
226 | | |
227 | 10.9M | void *OPENSSL_malloc(size_t size) { |
228 | 10.9M | if (should_fail_allocation()) { |
229 | 0 | goto err; |
230 | 0 | } |
231 | | |
232 | 10.9M | if (OPENSSL_memory_alloc != NULL) { |
233 | 0 | assert(OPENSSL_memory_free != NULL); |
234 | 0 | assert(OPENSSL_memory_get_size != NULL); |
235 | 0 | void *ptr = OPENSSL_memory_alloc(size); |
236 | 0 | if (ptr == NULL && size != 0) { |
237 | 0 | goto err; |
238 | 0 | } |
239 | 0 | return ptr; |
240 | 0 | } |
241 | | |
242 | 10.9M | if (size + OPENSSL_MALLOC_PREFIX < size) { |
243 | | // |OPENSSL_malloc| is a central function in BoringSSL thus a reference to |
244 | | // |kBoringSSLBinaryTag| is created here so that the tag isn't discarded by |
245 | | // the linker. The following is sufficient to stop GCC, Clang, and MSVC |
246 | | // optimising away the reference at the time of writing. Since this |
247 | | // probably results in an actual memory reference, it is put in this very |
248 | | // rare code path. |
249 | 0 | uint8_t unused = *(volatile uint8_t *)kBoringSSLBinaryTag; |
250 | 0 | (void) unused; |
251 | 0 | goto err; |
252 | 0 | } |
253 | | |
254 | 10.9M | void *ptr = malloc(size + OPENSSL_MALLOC_PREFIX); |
255 | 10.9M | if (ptr == NULL) { |
256 | 0 | goto err; |
257 | 0 | } |
258 | | |
259 | 10.9M | *(size_t *)ptr = size; |
260 | | |
261 | 10.9M | __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
262 | 10.9M | return ((uint8_t *)ptr) + OPENSSL_MALLOC_PREFIX; |
263 | | |
264 | 0 | err: |
265 | | // This only works because ERR does not call OPENSSL_malloc. |
266 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE); |
267 | 0 | return NULL; |
268 | 10.9M | } |
269 | | |
270 | 14.8M | void OPENSSL_free(void *orig_ptr) { |
271 | 14.8M | if (orig_ptr == NULL) { |
272 | 3.94M | return; |
273 | 3.94M | } |
274 | | |
275 | 10.9M | if (OPENSSL_memory_free != NULL) { |
276 | 0 | OPENSSL_memory_free(orig_ptr); |
277 | 0 | return; |
278 | 0 | } |
279 | | |
280 | 10.9M | void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX; |
281 | 10.9M | __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
282 | | |
283 | 10.9M | size_t size = *(size_t *)ptr; |
284 | 10.9M | OPENSSL_cleanse(ptr, size + OPENSSL_MALLOC_PREFIX); |
285 | | |
286 | | // ASan knows to intercept malloc and free, but not sdallocx. |
287 | | #if defined(OPENSSL_ASAN) |
288 | | (void)sdallocx; |
289 | | free(ptr); |
290 | | #else |
291 | 10.9M | if (sdallocx) { |
292 | 0 | sdallocx(ptr, size + OPENSSL_MALLOC_PREFIX, 0 /* flags */); |
293 | 10.9M | } else { |
294 | 10.9M | free(ptr); |
295 | 10.9M | } |
296 | 10.9M | #endif |
297 | 10.9M | } |
298 | | |
299 | 310k | void *OPENSSL_realloc(void *orig_ptr, size_t new_size) { |
300 | 310k | if (orig_ptr == NULL) { |
301 | 155k | return OPENSSL_malloc(new_size); |
302 | 155k | } |
303 | | |
304 | 154k | size_t old_size; |
305 | 154k | if (OPENSSL_memory_get_size != NULL) { |
306 | 0 | old_size = OPENSSL_memory_get_size(orig_ptr); |
307 | 154k | } else { |
308 | 154k | void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX; |
309 | 154k | __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
310 | 154k | old_size = *(size_t *)ptr; |
311 | 154k | __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
312 | 154k | } |
313 | | |
314 | 154k | void *ret = OPENSSL_malloc(new_size); |
315 | 154k | if (ret == NULL) { |
316 | 0 | return NULL; |
317 | 0 | } |
318 | | |
319 | 154k | size_t to_copy = new_size; |
320 | 154k | if (old_size < to_copy) { |
321 | 154k | to_copy = old_size; |
322 | 154k | } |
323 | | |
324 | 154k | memcpy(ret, orig_ptr, to_copy); |
325 | 154k | OPENSSL_free(orig_ptr); |
326 | | |
327 | 154k | return ret; |
328 | 154k | } |
329 | | |
330 | 10.9M | void OPENSSL_cleanse(void *ptr, size_t len) { |
331 | | #if defined(OPENSSL_WINDOWS) |
332 | | SecureZeroMemory(ptr, len); |
333 | | #else |
334 | 10.9M | OPENSSL_memset(ptr, 0, len); |
335 | | |
336 | 10.9M | #if !defined(OPENSSL_NO_ASM) |
337 | | /* As best as we can tell, this is sufficient to break any optimisations that |
338 | | might try to eliminate "superfluous" memsets. If there's an easy way to |
339 | | detect memset_s, it would be better to use that. */ |
340 | 10.9M | __asm__ __volatile__("" : : "r"(ptr) : "memory"); |
341 | 10.9M | #endif |
342 | 10.9M | #endif // !OPENSSL_NO_ASM |
343 | 10.9M | } |
344 | | |
345 | 0 | void OPENSSL_clear_free(void *ptr, size_t unused) { OPENSSL_free(ptr); } |
346 | | |
347 | 0 | int CRYPTO_secure_malloc_init(size_t size, size_t min_size) { return 0; } |
348 | | |
349 | 0 | int CRYPTO_secure_malloc_initialized(void) { return 0; } |
350 | | |
351 | 0 | size_t CRYPTO_secure_used(void) { return 0; } |
352 | | |
353 | 0 | void *OPENSSL_secure_malloc(size_t size) { return OPENSSL_malloc(size); } |
354 | | |
355 | 0 | void OPENSSL_secure_clear_free(void *ptr, size_t len) { |
356 | 0 | OPENSSL_clear_free(ptr, len); |
357 | 0 | } |
358 | | |
359 | 0 | int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) { |
360 | 0 | const uint8_t *a = in_a; |
361 | 0 | const uint8_t *b = in_b; |
362 | 0 | uint8_t x = 0; |
363 | |
|
364 | 0 | for (size_t i = 0; i < len; i++) { |
365 | 0 | x |= a[i] ^ b[i]; |
366 | 0 | } |
367 | |
|
368 | 0 | return x; |
369 | 0 | } |
370 | | |
371 | 299k | uint32_t OPENSSL_hash32(const void *ptr, size_t len) { |
372 | | // These are the FNV-1a parameters for 32 bits. |
373 | 299k | static const uint32_t kPrime = 16777619u; |
374 | 299k | static const uint32_t kOffsetBasis = 2166136261u; |
375 | | |
376 | 299k | const uint8_t *in = ptr; |
377 | 299k | uint32_t h = kOffsetBasis; |
378 | | |
379 | 1.40M | for (size_t i = 0; i < len; i++) { |
380 | 1.11M | h ^= in[i]; |
381 | 1.11M | h *= kPrime; |
382 | 1.11M | } |
383 | | |
384 | 299k | return h; |
385 | 299k | } |
386 | | |
387 | 299k | uint32_t OPENSSL_strhash(const char *s) { return OPENSSL_hash32(s, strlen(s)); } |
388 | | |
389 | 289k | size_t OPENSSL_strnlen(const char *s, size_t len) { |
390 | 4.72M | for (size_t i = 0; i < len; i++) { |
391 | 4.43M | if (s[i] == 0) { |
392 | 0 | return i; |
393 | 0 | } |
394 | 4.43M | } |
395 | | |
396 | 289k | return len; |
397 | 289k | } |
398 | | |
399 | 515k | char *OPENSSL_strdup(const char *s) { |
400 | 515k | if (s == NULL) { |
401 | 3 | return NULL; |
402 | 3 | } |
403 | 515k | const size_t len = strlen(s) + 1; |
404 | 515k | char *ret = OPENSSL_malloc(len); |
405 | 515k | if (ret == NULL) { |
406 | 0 | return NULL; |
407 | 0 | } |
408 | 515k | OPENSSL_memcpy(ret, s, len); |
409 | 515k | return ret; |
410 | 515k | } |
411 | | |
412 | 6.79k | int OPENSSL_isalpha(int c) { |
413 | 6.79k | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); |
414 | 6.79k | } |
415 | | |
416 | 1.69M | int OPENSSL_isdigit(int c) { return c >= '0' && c <= '9'; } |
417 | | |
418 | 184k | int OPENSSL_isxdigit(int c) { |
419 | 184k | return OPENSSL_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); |
420 | 184k | } |
421 | | |
422 | 193k | int OPENSSL_fromxdigit(uint8_t *out, int c) { |
423 | 193k | if (OPENSSL_isdigit(c)) { |
424 | 157k | *out = c - '0'; |
425 | 157k | return 1; |
426 | 157k | } |
427 | 35.6k | if ('a' <= c && c <= 'f') { |
428 | 27.3k | *out = c - 'a' + 10; |
429 | 27.3k | return 1; |
430 | 27.3k | } |
431 | 8.32k | if ('A' <= c && c <= 'F') { |
432 | 8.16k | *out = c - 'A' + 10; |
433 | 8.16k | return 1; |
434 | 8.16k | } |
435 | 156 | return 0; |
436 | 8.32k | } |
437 | | |
438 | 6.79k | int OPENSSL_isalnum(int c) { return OPENSSL_isalpha(c) || OPENSSL_isdigit(c); } |
439 | | |
440 | 183M | int OPENSSL_tolower(int c) { |
441 | 183M | if (c >= 'A' && c <= 'Z') { |
442 | 11.4M | return c + ('a' - 'A'); |
443 | 11.4M | } |
444 | 171M | return c; |
445 | 183M | } |
446 | | |
447 | 195M | int OPENSSL_isspace(int c) { |
448 | 195M | return c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' || |
449 | 195M | c == ' '; |
450 | 195M | } |
451 | | |
452 | 0 | int OPENSSL_strcasecmp(const char *a, const char *b) { |
453 | 0 | for (size_t i = 0;; i++) { |
454 | 0 | const int aa = OPENSSL_tolower(a[i]); |
455 | 0 | const int bb = OPENSSL_tolower(b[i]); |
456 | |
|
457 | 0 | if (aa < bb) { |
458 | 0 | return -1; |
459 | 0 | } else if (aa > bb) { |
460 | 0 | return 1; |
461 | 0 | } else if (aa == 0) { |
462 | 0 | return 0; |
463 | 0 | } |
464 | 0 | } |
465 | 0 | } |
466 | | |
467 | 0 | int OPENSSL_strncasecmp(const char *a, const char *b, size_t n) { |
468 | 0 | for (size_t i = 0; i < n; i++) { |
469 | 0 | const int aa = OPENSSL_tolower(a[i]); |
470 | 0 | const int bb = OPENSSL_tolower(b[i]); |
471 | |
|
472 | 0 | if (aa < bb) { |
473 | 0 | return -1; |
474 | 0 | } else if (aa > bb) { |
475 | 0 | return 1; |
476 | 0 | } else if (aa == 0) { |
477 | 0 | return 0; |
478 | 0 | } |
479 | 0 | } |
480 | | |
481 | 0 | return 0; |
482 | 0 | } |
483 | | |
484 | 313 | int BIO_snprintf(char *buf, size_t n, const char *format, ...) { |
485 | 313 | va_list args; |
486 | 313 | va_start(args, format); |
487 | 313 | int ret = BIO_vsnprintf(buf, n, format, args); |
488 | 313 | va_end(args); |
489 | 313 | return ret; |
490 | 313 | } |
491 | | |
492 | 313 | int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) { |
493 | 313 | return vsnprintf(buf, n, format, args); |
494 | 313 | } |
495 | | |
496 | | int OPENSSL_vasprintf_internal(char **str, const char *format, va_list args, |
497 | 15 | int system_malloc) { |
498 | 15 | void *(*allocate)(size_t) = system_malloc ? malloc : OPENSSL_malloc; |
499 | 15 | void (*deallocate)(void *) = system_malloc ? free : OPENSSL_free; |
500 | 15 | void *(*reallocate)(void *, size_t) = |
501 | 15 | system_malloc ? realloc : OPENSSL_realloc; |
502 | 15 | char *candidate = NULL; |
503 | 15 | size_t candidate_len = 64; // TODO(bbe) what's the best initial size? |
504 | | |
505 | 15 | if ((candidate = allocate(candidate_len)) == NULL) { |
506 | 0 | goto err; |
507 | 0 | } |
508 | 15 | va_list args_copy; |
509 | 15 | va_copy(args_copy, args); |
510 | 15 | int ret = vsnprintf(candidate, candidate_len, format, args_copy); |
511 | 15 | va_end(args_copy); |
512 | 15 | if (ret < 0) { |
513 | 0 | goto err; |
514 | 0 | } |
515 | 15 | if ((size_t)ret >= candidate_len) { |
516 | | // Too big to fit in allocation. |
517 | 0 | char *tmp; |
518 | |
|
519 | 0 | candidate_len = (size_t)ret + 1; |
520 | 0 | if ((tmp = reallocate(candidate, candidate_len)) == NULL) { |
521 | 0 | goto err; |
522 | 0 | } |
523 | 0 | candidate = tmp; |
524 | 0 | ret = vsnprintf(candidate, candidate_len, format, args); |
525 | 0 | } |
526 | | // At this point this should not happen unless vsnprintf is insane. |
527 | 15 | if (ret < 0 || (size_t)ret >= candidate_len) { |
528 | 0 | goto err; |
529 | 0 | } |
530 | 15 | *str = candidate; |
531 | 15 | return ret; |
532 | | |
533 | 0 | err: |
534 | 0 | deallocate(candidate); |
535 | 0 | *str = NULL; |
536 | 0 | errno = ENOMEM; |
537 | 0 | return -1; |
538 | 15 | } |
539 | | |
540 | 0 | int OPENSSL_vasprintf(char **str, const char *format, va_list args) { |
541 | 0 | return OPENSSL_vasprintf_internal(str, format, args, /*system_malloc=*/0); |
542 | 0 | } |
543 | | |
544 | 0 | int OPENSSL_asprintf(char **str, const char *format, ...) { |
545 | 0 | va_list args; |
546 | 0 | va_start(args, format); |
547 | 0 | int ret = OPENSSL_vasprintf(str, format, args); |
548 | 0 | va_end(args); |
549 | 0 | return ret; |
550 | 0 | } |
551 | | |
552 | 289k | char *OPENSSL_strndup(const char *str, size_t size) { |
553 | 289k | size = OPENSSL_strnlen(str, size); |
554 | | |
555 | 289k | size_t alloc_size = size + 1; |
556 | 289k | if (alloc_size < size) { |
557 | | // overflow |
558 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE); |
559 | 0 | return NULL; |
560 | 0 | } |
561 | 289k | char *ret = OPENSSL_malloc(alloc_size); |
562 | 289k | if (ret == NULL) { |
563 | 0 | return NULL; |
564 | 0 | } |
565 | | |
566 | 289k | OPENSSL_memcpy(ret, str, size); |
567 | 289k | ret[size] = '\0'; |
568 | 289k | return ret; |
569 | 289k | } |
570 | | |
571 | 66.4k | size_t OPENSSL_strlcpy(char *dst, const char *src, size_t dst_size) { |
572 | 66.4k | size_t l = 0; |
573 | | |
574 | 1.72M | for (; dst_size > 1 && *src; dst_size--) { |
575 | 1.66M | *dst++ = *src++; |
576 | 1.66M | l++; |
577 | 1.66M | } |
578 | | |
579 | 66.4k | if (dst_size) { |
580 | 66.4k | *dst = 0; |
581 | 66.4k | } |
582 | | |
583 | 66.4k | return l + strlen(src); |
584 | 66.4k | } |
585 | | |
586 | 66.4k | size_t OPENSSL_strlcat(char *dst, const char *src, size_t dst_size) { |
587 | 66.4k | size_t l = 0; |
588 | 703k | for (; dst_size > 0 && *dst; dst_size--, dst++) { |
589 | 637k | l++; |
590 | 637k | } |
591 | 66.4k | return l + OPENSSL_strlcpy(dst, src, dst_size); |
592 | 66.4k | } |
593 | | |
594 | 8.70k | void *OPENSSL_memdup(const void *data, size_t size) { |
595 | 8.70k | if (size == 0) { |
596 | 0 | return NULL; |
597 | 0 | } |
598 | | |
599 | 8.70k | void *ret = OPENSSL_malloc(size); |
600 | 8.70k | if (ret == NULL) { |
601 | 0 | return NULL; |
602 | 0 | } |
603 | | |
604 | 8.70k | OPENSSL_memcpy(ret, data, size); |
605 | 8.70k | return ret; |
606 | 8.70k | } |
607 | | |
608 | 0 | void *CRYPTO_malloc(size_t size, const char *file, int line) { |
609 | 0 | return OPENSSL_malloc(size); |
610 | 0 | } |
611 | | |
612 | 0 | void *CRYPTO_realloc(void *ptr, size_t new_size, const char *file, int line) { |
613 | 0 | return OPENSSL_realloc(ptr, new_size); |
614 | 0 | } |
615 | | |
616 | 0 | void CRYPTO_free(void *ptr, const char *file, int line) { OPENSSL_free(ptr); } |