/proc/self/cwd/external/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 | 12.6M | #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 | 1.84M | static void __asan_poison_memory_region(const void *addr, size_t size) {} |
91 | 1.82M | 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 | | #if defined(BORINGSSL_MALLOC_FAILURE_TESTING) |
137 | | static CRYPTO_MUTEX malloc_failure_lock = CRYPTO_MUTEX_INIT; |
138 | | static uint64_t current_malloc_count = 0; |
139 | | static uint64_t malloc_number_to_fail = 0; |
140 | | static int malloc_failure_enabled = 0, break_on_malloc_fail = 0, |
141 | | any_malloc_failed = 0, disable_malloc_failures = 0; |
142 | | |
143 | | static void malloc_exit_handler(void) { |
144 | | CRYPTO_MUTEX_lock_read(&malloc_failure_lock); |
145 | | if (any_malloc_failed) { |
146 | | // Signal to the test driver that some allocation failed, so it knows to |
147 | | // increment the counter and continue. |
148 | | _exit(88); |
149 | | } |
150 | | CRYPTO_MUTEX_unlock_read(&malloc_failure_lock); |
151 | | } |
152 | | |
153 | | static void init_malloc_failure(void) { |
154 | | const char *env = getenv("MALLOC_NUMBER_TO_FAIL"); |
155 | | if (env != NULL && env[0] != 0) { |
156 | | char *endptr; |
157 | | malloc_number_to_fail = strtoull(env, &endptr, 10); |
158 | | if (*endptr == 0) { |
159 | | malloc_failure_enabled = 1; |
160 | | atexit(malloc_exit_handler); |
161 | | } |
162 | | } |
163 | | break_on_malloc_fail = getenv("MALLOC_BREAK_ON_FAIL") != NULL; |
164 | | } |
165 | | |
166 | | // should_fail_allocation returns one if the current allocation should fail and |
167 | | // zero otherwise. |
168 | | static int should_fail_allocation() { |
169 | | static CRYPTO_once_t once = CRYPTO_ONCE_INIT; |
170 | | CRYPTO_once(&once, init_malloc_failure); |
171 | | if (!malloc_failure_enabled || disable_malloc_failures) { |
172 | | return 0; |
173 | | } |
174 | | |
175 | | // We lock just so multi-threaded tests are still correct, but we won't test |
176 | | // every malloc exhaustively. |
177 | | CRYPTO_MUTEX_lock_write(&malloc_failure_lock); |
178 | | int should_fail = current_malloc_count == malloc_number_to_fail; |
179 | | current_malloc_count++; |
180 | | any_malloc_failed = any_malloc_failed || should_fail; |
181 | | CRYPTO_MUTEX_unlock_write(&malloc_failure_lock); |
182 | | |
183 | | if (should_fail && break_on_malloc_fail) { |
184 | | raise(SIGTRAP); |
185 | | } |
186 | | if (should_fail) { |
187 | | errno = ENOMEM; |
188 | | } |
189 | | return should_fail; |
190 | | } |
191 | | |
192 | | void OPENSSL_reset_malloc_counter_for_testing(void) { |
193 | | CRYPTO_MUTEX_lock_write(&malloc_failure_lock); |
194 | | current_malloc_count = 0; |
195 | | CRYPTO_MUTEX_unlock_write(&malloc_failure_lock); |
196 | | } |
197 | | |
198 | | void OPENSSL_disable_malloc_failures_for_testing(void) { |
199 | | CRYPTO_MUTEX_lock_write(&malloc_failure_lock); |
200 | | BSSL_CHECK(!disable_malloc_failures); |
201 | | disable_malloc_failures = 1; |
202 | | CRYPTO_MUTEX_unlock_write(&malloc_failure_lock); |
203 | | } |
204 | | |
205 | | void OPENSSL_enable_malloc_failures_for_testing(void) { |
206 | | CRYPTO_MUTEX_lock_write(&malloc_failure_lock); |
207 | | BSSL_CHECK(disable_malloc_failures); |
208 | | disable_malloc_failures = 0; |
209 | | CRYPTO_MUTEX_unlock_write(&malloc_failure_lock); |
210 | | } |
211 | | |
212 | | #else |
213 | 1.78M | static int should_fail_allocation(void) { return 0; } |
214 | | #endif |
215 | | |
216 | 1.78M | void *OPENSSL_malloc(size_t size) { |
217 | 1.78M | if (should_fail_allocation()) { |
218 | 0 | goto err; |
219 | 0 | } |
220 | | |
221 | 1.78M | if (OPENSSL_memory_alloc != NULL) { |
222 | 0 | assert(OPENSSL_memory_free != NULL); |
223 | 0 | assert(OPENSSL_memory_get_size != NULL); |
224 | 0 | void *ptr = OPENSSL_memory_alloc(size); |
225 | 0 | if (ptr == NULL && size != 0) { |
226 | 0 | goto err; |
227 | 0 | } |
228 | 0 | return ptr; |
229 | 0 | } |
230 | | |
231 | 1.78M | if (size + OPENSSL_MALLOC_PREFIX < size) { |
232 | 0 | goto err; |
233 | 0 | } |
234 | | |
235 | 1.78M | void *ptr = malloc(size + OPENSSL_MALLOC_PREFIX); |
236 | 1.78M | if (ptr == NULL) { |
237 | 0 | goto err; |
238 | 0 | } |
239 | | |
240 | 1.78M | *(size_t *)ptr = size; |
241 | | |
242 | 1.78M | __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
243 | 1.78M | return ((uint8_t *)ptr) + OPENSSL_MALLOC_PREFIX; |
244 | | |
245 | 0 | err: |
246 | | // This only works because ERR does not call OPENSSL_malloc. |
247 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE); |
248 | 0 | return NULL; |
249 | 1.78M | } |
250 | | |
251 | 846k | void *OPENSSL_zalloc(size_t size) { |
252 | 846k | void *ret = OPENSSL_malloc(size); |
253 | 846k | if (ret != NULL) { |
254 | 846k | OPENSSL_memset(ret, 0, size); |
255 | 846k | } |
256 | 846k | return ret; |
257 | 846k | } |
258 | | |
259 | 413k | void *OPENSSL_calloc(size_t num, size_t size) { |
260 | 413k | if (size != 0 && num > SIZE_MAX / size) { |
261 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW); |
262 | 0 | return NULL; |
263 | 0 | } |
264 | | |
265 | 413k | return OPENSSL_zalloc(num * size); |
266 | 413k | } |
267 | | |
268 | 2.63M | void OPENSSL_free(void *orig_ptr) { |
269 | 2.63M | if (orig_ptr == NULL) { |
270 | 868k | return; |
271 | 868k | } |
272 | | |
273 | 1.77M | if (OPENSSL_memory_free != NULL) { |
274 | 0 | OPENSSL_memory_free(orig_ptr); |
275 | 0 | return; |
276 | 0 | } |
277 | | |
278 | 1.77M | void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX; |
279 | 1.77M | __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
280 | | |
281 | 1.77M | size_t size = *(size_t *)ptr; |
282 | 1.77M | OPENSSL_cleanse(ptr, size + OPENSSL_MALLOC_PREFIX); |
283 | | |
284 | | // ASan knows to intercept malloc and free, but not sdallocx. |
285 | | #if defined(OPENSSL_ASAN) |
286 | | (void)sdallocx; |
287 | | free(ptr); |
288 | | #else |
289 | 1.77M | if (sdallocx) { |
290 | 0 | sdallocx(ptr, size + OPENSSL_MALLOC_PREFIX, 0 /* flags */); |
291 | 1.77M | } else { |
292 | 1.77M | free(ptr); |
293 | 1.77M | } |
294 | 1.77M | #endif |
295 | 1.77M | } |
296 | | |
297 | 112k | void *OPENSSL_realloc(void *orig_ptr, size_t new_size) { |
298 | 112k | if (orig_ptr == NULL) { |
299 | 55.9k | return OPENSSL_malloc(new_size); |
300 | 55.9k | } |
301 | | |
302 | 56.6k | size_t old_size; |
303 | 56.6k | if (OPENSSL_memory_get_size != NULL) { |
304 | 0 | old_size = OPENSSL_memory_get_size(orig_ptr); |
305 | 56.6k | } else { |
306 | 56.6k | void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX; |
307 | 56.6k | __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
308 | 56.6k | old_size = *(size_t *)ptr; |
309 | 56.6k | __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
310 | 56.6k | } |
311 | | |
312 | 56.6k | void *ret = OPENSSL_malloc(new_size); |
313 | 56.6k | if (ret == NULL) { |
314 | 0 | return NULL; |
315 | 0 | } |
316 | | |
317 | 56.6k | size_t to_copy = new_size; |
318 | 56.6k | if (old_size < to_copy) { |
319 | 56.6k | to_copy = old_size; |
320 | 56.6k | } |
321 | | |
322 | 56.6k | memcpy(ret, orig_ptr, to_copy); |
323 | 56.6k | OPENSSL_free(orig_ptr); |
324 | | |
325 | 56.6k | return ret; |
326 | 56.6k | } |
327 | | |
328 | 1.92M | void OPENSSL_cleanse(void *ptr, size_t len) { |
329 | | #if defined(OPENSSL_WINDOWS) |
330 | | SecureZeroMemory(ptr, len); |
331 | | #else |
332 | 1.92M | OPENSSL_memset(ptr, 0, len); |
333 | | |
334 | 1.92M | #if !defined(OPENSSL_NO_ASM) |
335 | | /* As best as we can tell, this is sufficient to break any optimisations that |
336 | | might try to eliminate "superfluous" memsets. If there's an easy way to |
337 | | detect memset_s, it would be better to use that. */ |
338 | 1.92M | __asm__ __volatile__("" : : "r"(ptr) : "memory"); |
339 | 1.92M | #endif |
340 | 1.92M | #endif // !OPENSSL_NO_ASM |
341 | 1.92M | } |
342 | | |
343 | 0 | void OPENSSL_clear_free(void *ptr, size_t unused) { OPENSSL_free(ptr); } |
344 | | |
345 | 0 | int CRYPTO_secure_malloc_init(size_t size, size_t min_size) { return 0; } |
346 | | |
347 | 0 | int CRYPTO_secure_malloc_initialized(void) { return 0; } |
348 | | |
349 | 0 | size_t CRYPTO_secure_used(void) { return 0; } |
350 | | |
351 | 0 | void *OPENSSL_secure_malloc(size_t size) { return OPENSSL_malloc(size); } |
352 | | |
353 | 0 | void OPENSSL_secure_clear_free(void *ptr, size_t len) { |
354 | 0 | OPENSSL_clear_free(ptr, len); |
355 | 0 | } |
356 | | |
357 | 115 | int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) { |
358 | 115 | const uint8_t *a = in_a; |
359 | 115 | const uint8_t *b = in_b; |
360 | 115 | uint8_t x = 0; |
361 | | |
362 | 2.10k | for (size_t i = 0; i < len; i++) { |
363 | 1.99k | x |= a[i] ^ b[i]; |
364 | 1.99k | } |
365 | | |
366 | 115 | return x; |
367 | 115 | } |
368 | | |
369 | 0 | uint32_t OPENSSL_hash32(const void *ptr, size_t len) { |
370 | | // These are the FNV-1a parameters for 32 bits. |
371 | 0 | static const uint32_t kPrime = 16777619u; |
372 | 0 | static const uint32_t kOffsetBasis = 2166136261u; |
373 | |
|
374 | 0 | const uint8_t *in = ptr; |
375 | 0 | uint32_t h = kOffsetBasis; |
376 | |
|
377 | 0 | for (size_t i = 0; i < len; i++) { |
378 | 0 | h ^= in[i]; |
379 | 0 | h *= kPrime; |
380 | 0 | } |
381 | |
|
382 | 0 | return h; |
383 | 0 | } |
384 | | |
385 | 0 | uint32_t OPENSSL_strhash(const char *s) { return OPENSSL_hash32(s, strlen(s)); } |
386 | | |
387 | 0 | size_t OPENSSL_strnlen(const char *s, size_t len) { |
388 | 0 | for (size_t i = 0; i < len; i++) { |
389 | 0 | if (s[i] == 0) { |
390 | 0 | return i; |
391 | 0 | } |
392 | 0 | } |
393 | | |
394 | 0 | return len; |
395 | 0 | } |
396 | | |
397 | 1 | char *OPENSSL_strdup(const char *s) { |
398 | 1 | if (s == NULL) { |
399 | 0 | return NULL; |
400 | 0 | } |
401 | | // Copy the NUL terminator. |
402 | 1 | return OPENSSL_memdup(s, strlen(s) + 1); |
403 | 1 | } |
404 | | |
405 | 14.2k | int OPENSSL_isalpha(int c) { |
406 | 14.2k | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); |
407 | 14.2k | } |
408 | | |
409 | 9.13k | int OPENSSL_isdigit(int c) { return c >= '0' && c <= '9'; } |
410 | | |
411 | 0 | int OPENSSL_isxdigit(int c) { |
412 | 0 | return OPENSSL_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); |
413 | 0 | } |
414 | | |
415 | 0 | int OPENSSL_fromxdigit(uint8_t *out, int c) { |
416 | 0 | if (OPENSSL_isdigit(c)) { |
417 | 0 | *out = c - '0'; |
418 | 0 | return 1; |
419 | 0 | } |
420 | 0 | if ('a' <= c && c <= 'f') { |
421 | 0 | *out = c - 'a' + 10; |
422 | 0 | return 1; |
423 | 0 | } |
424 | 0 | if ('A' <= c && c <= 'F') { |
425 | 0 | *out = c - 'A' + 10; |
426 | 0 | return 1; |
427 | 0 | } |
428 | 0 | return 0; |
429 | 0 | } |
430 | | |
431 | 14.2k | int OPENSSL_isalnum(int c) { return OPENSSL_isalpha(c) || OPENSSL_isdigit(c); } |
432 | | |
433 | 14.4k | int OPENSSL_tolower(int c) { |
434 | 14.4k | if (c >= 'A' && c <= 'Z') { |
435 | 3.98k | return c + ('a' - 'A'); |
436 | 3.98k | } |
437 | 10.4k | return c; |
438 | 14.4k | } |
439 | | |
440 | 19.5k | int OPENSSL_isspace(int c) { |
441 | 19.5k | return c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' || |
442 | 19.5k | c == ' '; |
443 | 19.5k | } |
444 | | |
445 | 0 | int OPENSSL_strcasecmp(const char *a, const char *b) { |
446 | 0 | for (size_t i = 0;; i++) { |
447 | 0 | const int aa = OPENSSL_tolower(a[i]); |
448 | 0 | const int bb = OPENSSL_tolower(b[i]); |
449 | |
|
450 | 0 | if (aa < bb) { |
451 | 0 | return -1; |
452 | 0 | } else if (aa > bb) { |
453 | 0 | return 1; |
454 | 0 | } else if (aa == 0) { |
455 | 0 | return 0; |
456 | 0 | } |
457 | 0 | } |
458 | 0 | } |
459 | | |
460 | 0 | int OPENSSL_strncasecmp(const char *a, const char *b, size_t n) { |
461 | 0 | for (size_t i = 0; i < n; i++) { |
462 | 0 | const int aa = OPENSSL_tolower(a[i]); |
463 | 0 | const int bb = OPENSSL_tolower(b[i]); |
464 | |
|
465 | 0 | if (aa < bb) { |
466 | 0 | return -1; |
467 | 0 | } else if (aa > bb) { |
468 | 0 | return 1; |
469 | 0 | } else if (aa == 0) { |
470 | 0 | return 0; |
471 | 0 | } |
472 | 0 | } |
473 | | |
474 | 0 | return 0; |
475 | 0 | } |
476 | | |
477 | 0 | int BIO_snprintf(char *buf, size_t n, const char *format, ...) { |
478 | 0 | va_list args; |
479 | 0 | va_start(args, format); |
480 | 0 | int ret = BIO_vsnprintf(buf, n, format, args); |
481 | 0 | va_end(args); |
482 | 0 | return ret; |
483 | 0 | } |
484 | | |
485 | 0 | int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) { |
486 | 0 | return vsnprintf(buf, n, format, args); |
487 | 0 | } |
488 | | |
489 | | int OPENSSL_vasprintf_internal(char **str, const char *format, va_list args, |
490 | 0 | int system_malloc) { |
491 | 0 | void *(*allocate)(size_t) = system_malloc ? malloc : OPENSSL_malloc; |
492 | 0 | void (*deallocate)(void *) = system_malloc ? free : OPENSSL_free; |
493 | 0 | void *(*reallocate)(void *, size_t) = |
494 | 0 | system_malloc ? realloc : OPENSSL_realloc; |
495 | 0 | char *candidate = NULL; |
496 | 0 | size_t candidate_len = 64; // TODO(bbe) what's the best initial size? |
497 | |
|
498 | 0 | if ((candidate = allocate(candidate_len)) == NULL) { |
499 | 0 | goto err; |
500 | 0 | } |
501 | 0 | va_list args_copy; |
502 | 0 | va_copy(args_copy, args); |
503 | 0 | int ret = vsnprintf(candidate, candidate_len, format, args_copy); |
504 | 0 | va_end(args_copy); |
505 | 0 | if (ret < 0) { |
506 | 0 | goto err; |
507 | 0 | } |
508 | 0 | if ((size_t)ret >= candidate_len) { |
509 | | // Too big to fit in allocation. |
510 | 0 | char *tmp; |
511 | |
|
512 | 0 | candidate_len = (size_t)ret + 1; |
513 | 0 | if ((tmp = reallocate(candidate, candidate_len)) == NULL) { |
514 | 0 | goto err; |
515 | 0 | } |
516 | 0 | candidate = tmp; |
517 | 0 | ret = vsnprintf(candidate, candidate_len, format, args); |
518 | 0 | } |
519 | | // At this point this should not happen unless vsnprintf is insane. |
520 | 0 | if (ret < 0 || (size_t)ret >= candidate_len) { |
521 | 0 | goto err; |
522 | 0 | } |
523 | 0 | *str = candidate; |
524 | 0 | return ret; |
525 | | |
526 | 0 | err: |
527 | 0 | deallocate(candidate); |
528 | 0 | *str = NULL; |
529 | 0 | errno = ENOMEM; |
530 | 0 | return -1; |
531 | 0 | } |
532 | | |
533 | 0 | int OPENSSL_vasprintf(char **str, const char *format, va_list args) { |
534 | 0 | return OPENSSL_vasprintf_internal(str, format, args, /*system_malloc=*/0); |
535 | 0 | } |
536 | | |
537 | 0 | int OPENSSL_asprintf(char **str, const char *format, ...) { |
538 | 0 | va_list args; |
539 | 0 | va_start(args, format); |
540 | 0 | int ret = OPENSSL_vasprintf(str, format, args); |
541 | 0 | va_end(args); |
542 | 0 | return ret; |
543 | 0 | } |
544 | | |
545 | 0 | char *OPENSSL_strndup(const char *str, size_t size) { |
546 | 0 | size = OPENSSL_strnlen(str, size); |
547 | |
|
548 | 0 | size_t alloc_size = size + 1; |
549 | 0 | if (alloc_size < size) { |
550 | | // overflow |
551 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE); |
552 | 0 | return NULL; |
553 | 0 | } |
554 | 0 | char *ret = OPENSSL_malloc(alloc_size); |
555 | 0 | if (ret == NULL) { |
556 | 0 | return NULL; |
557 | 0 | } |
558 | | |
559 | 0 | OPENSSL_memcpy(ret, str, size); |
560 | 0 | ret[size] = '\0'; |
561 | 0 | return ret; |
562 | 0 | } |
563 | | |
564 | 2 | size_t OPENSSL_strlcpy(char *dst, const char *src, size_t dst_size) { |
565 | 2 | size_t l = 0; |
566 | | |
567 | 32 | for (; dst_size > 1 && *src; dst_size--) { |
568 | 30 | *dst++ = *src++; |
569 | 30 | l++; |
570 | 30 | } |
571 | | |
572 | 2 | if (dst_size) { |
573 | 2 | *dst = 0; |
574 | 2 | } |
575 | | |
576 | 2 | return l + strlen(src); |
577 | 2 | } |
578 | | |
579 | 2 | size_t OPENSSL_strlcat(char *dst, const char *src, size_t dst_size) { |
580 | 2 | size_t l = 0; |
581 | 13 | for (; dst_size > 0 && *dst; dst_size--, dst++) { |
582 | 11 | l++; |
583 | 11 | } |
584 | 2 | return l + OPENSSL_strlcpy(dst, src, dst_size); |
585 | 2 | } |
586 | | |
587 | 3.33k | void *OPENSSL_memdup(const void *data, size_t size) { |
588 | 3.33k | if (size == 0) { |
589 | 0 | return NULL; |
590 | 0 | } |
591 | | |
592 | 3.33k | void *ret = OPENSSL_malloc(size); |
593 | 3.33k | if (ret == NULL) { |
594 | 0 | return NULL; |
595 | 0 | } |
596 | | |
597 | 3.33k | OPENSSL_memcpy(ret, data, size); |
598 | 3.33k | return ret; |
599 | 3.33k | } |
600 | | |
601 | 0 | void *CRYPTO_malloc(size_t size, const char *file, int line) { |
602 | 0 | return OPENSSL_malloc(size); |
603 | 0 | } |
604 | | |
605 | 0 | void *CRYPTO_realloc(void *ptr, size_t new_size, const char *file, int line) { |
606 | 0 | return OPENSSL_realloc(ptr, new_size); |
607 | 0 | } |
608 | | |
609 | 0 | void CRYPTO_free(void *ptr, const char *file, int line) { OPENSSL_free(ptr); } |