Coverage Report

Created: 2024-11-21 07:03

/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
4.42M
#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
634k
static void __asan_poison_memory_region(const void *addr, size_t size) {}
91
634k
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
//
98
// EDK2 targets UEFI but builds as ELF and then translates the binary to
99
// COFF(!). Thus it builds with __ELF__ defined but cannot actually cope with
100
// weak symbols.
101
#if !defined(__EDK2_BORINGSSL__) && defined(__ELF__) && defined(__GNUC__)
102
#define WEAK_SYMBOL_FUNC(rettype, name, args) \
103
  rettype name args __attribute__((weak))
104
#else
105
#define WEAK_SYMBOL_FUNC(rettype, name, args) \
106
  static rettype(*const name) args = NULL
107
#endif
108
109
#if defined(BORINGSSL_DETECT_SDALLOCX)
110
// sdallocx is a sized |free| function. By passing the size (which we happen to
111
// always know in BoringSSL), the malloc implementation can save work. We cannot
112
// depend on |sdallocx| being available, however, so it's a weak symbol.
113
//
114
// This mechanism is kept opt-in because it assumes that, when |sdallocx| is
115
// defined, it is part of the same allocator as |malloc|. This is usually true
116
// but may break if |malloc| does not implement |sdallocx|, but some other
117
// allocator with |sdallocx| is imported which does.
118
WEAK_SYMBOL_FUNC(void, sdallocx, (void *ptr, size_t size, int flags));
119
#else
120
static void (*const sdallocx)(void *ptr, size_t size, int flags) = NULL;
121
#endif
122
123
// The following three functions can be defined to override default heap
124
// allocation and freeing. If defined, it is the responsibility of
125
// |OPENSSL_memory_free| to zero out the memory before returning it to the
126
// system. |OPENSSL_memory_free| will not be passed NULL pointers.
127
//
128
// WARNING: These functions are called on every allocation and free in
129
// BoringSSL across the entire process. They may be called by any code in the
130
// process which calls BoringSSL, including in process initializers and thread
131
// destructors. When called, BoringSSL may hold pthreads locks. Any other code
132
// in the process which, directly or indirectly, calls BoringSSL may be on the
133
// call stack and may itself be using arbitrary synchronization primitives.
134
//
135
// As a result, these functions may not have the usual programming environment
136
// available to most C or C++ code. In particular, they may not call into
137
// BoringSSL, or any library which depends on BoringSSL. Any synchronization
138
// primitives used must tolerate every other synchronization primitive linked
139
// into the process, including pthreads locks. Failing to meet these constraints
140
// may result in deadlocks, crashes, or memory corruption.
141
WEAK_SYMBOL_FUNC(void *, OPENSSL_memory_alloc, (size_t size));
142
WEAK_SYMBOL_FUNC(void, OPENSSL_memory_free, (void *ptr));
143
WEAK_SYMBOL_FUNC(size_t, OPENSSL_memory_get_size, (void *ptr));
144
145
#if defined(BORINGSSL_MALLOC_FAILURE_TESTING)
146
static CRYPTO_MUTEX malloc_failure_lock = CRYPTO_MUTEX_INIT;
147
static uint64_t current_malloc_count = 0;
148
static uint64_t malloc_number_to_fail = 0;
149
static int malloc_failure_enabled = 0, break_on_malloc_fail = 0,
150
           any_malloc_failed = 0, disable_malloc_failures = 0;
151
152
static void malloc_exit_handler(void) {
153
  CRYPTO_MUTEX_lock_read(&malloc_failure_lock);
154
  if (any_malloc_failed) {
155
    // Signal to the test driver that some allocation failed, so it knows to
156
    // increment the counter and continue.
157
    _exit(88);
158
  }
159
  CRYPTO_MUTEX_unlock_read(&malloc_failure_lock);
160
}
161
162
static void init_malloc_failure(void) {
163
  const char *env = getenv("MALLOC_NUMBER_TO_FAIL");
164
  if (env != NULL && env[0] != 0) {
165
    char *endptr;
166
    malloc_number_to_fail = strtoull(env, &endptr, 10);
167
    if (*endptr == 0) {
168
      malloc_failure_enabled = 1;
169
      atexit(malloc_exit_handler);
170
    }
171
  }
172
  break_on_malloc_fail = getenv("MALLOC_BREAK_ON_FAIL") != NULL;
173
}
174
175
// should_fail_allocation returns one if the current allocation should fail and
176
// zero otherwise.
177
static int should_fail_allocation() {
178
  static CRYPTO_once_t once = CRYPTO_ONCE_INIT;
179
  CRYPTO_once(&once, init_malloc_failure);
180
  if (!malloc_failure_enabled || disable_malloc_failures) {
181
    return 0;
182
  }
183
184
  // We lock just so multi-threaded tests are still correct, but we won't test
185
  // every malloc exhaustively.
186
  CRYPTO_MUTEX_lock_write(&malloc_failure_lock);
187
  int should_fail = current_malloc_count == malloc_number_to_fail;
188
  current_malloc_count++;
189
  any_malloc_failed = any_malloc_failed || should_fail;
190
  CRYPTO_MUTEX_unlock_write(&malloc_failure_lock);
191
192
  if (should_fail && break_on_malloc_fail) {
193
    raise(SIGTRAP);
194
  }
195
  if (should_fail) {
196
    errno = ENOMEM;
197
  }
198
  return should_fail;
199
}
200
201
void OPENSSL_reset_malloc_counter_for_testing(void) {
202
  CRYPTO_MUTEX_lock_write(&malloc_failure_lock);
203
  current_malloc_count = 0;
204
  CRYPTO_MUTEX_unlock_write(&malloc_failure_lock);
205
}
206
207
void OPENSSL_disable_malloc_failures_for_testing(void) {
208
  CRYPTO_MUTEX_lock_write(&malloc_failure_lock);
209
  BSSL_CHECK(!disable_malloc_failures);
210
  disable_malloc_failures = 1;
211
  CRYPTO_MUTEX_unlock_write(&malloc_failure_lock);
212
}
213
214
void OPENSSL_enable_malloc_failures_for_testing(void) {
215
  CRYPTO_MUTEX_lock_write(&malloc_failure_lock);
216
  BSSL_CHECK(disable_malloc_failures);
217
  disable_malloc_failures = 0;
218
  CRYPTO_MUTEX_unlock_write(&malloc_failure_lock);
219
}
220
221
#else
222
630k
static int should_fail_allocation(void) { return 0; }
223
#endif
224
225
630k
void *OPENSSL_malloc(size_t size) {
226
630k
  if (should_fail_allocation()) {
227
0
    goto err;
228
0
  }
229
230
630k
  if (OPENSSL_memory_alloc != NULL) {
231
0
    assert(OPENSSL_memory_free != NULL);
232
0
    assert(OPENSSL_memory_get_size != NULL);
233
0
    void *ptr = OPENSSL_memory_alloc(size);
234
0
    if (ptr == NULL && size != 0) {
235
0
      goto err;
236
0
    }
237
0
    return ptr;
238
0
  }
239
240
630k
  if (size + OPENSSL_MALLOC_PREFIX < size) {
241
0
    goto err;
242
0
  }
243
244
630k
  void *ptr = malloc(size + OPENSSL_MALLOC_PREFIX);
245
630k
  if (ptr == NULL) {
246
0
    goto err;
247
0
  }
248
249
630k
  *(size_t *)ptr = size;
250
251
630k
  __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
252
630k
  return ((uint8_t *)ptr) + OPENSSL_MALLOC_PREFIX;
253
254
0
err:
255
  // This only works because ERR does not call OPENSSL_malloc.
256
0
  OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
257
0
  return NULL;
258
630k
}
259
260
232k
void *OPENSSL_zalloc(size_t size) {
261
232k
  void *ret = OPENSSL_malloc(size);
262
232k
  if (ret != NULL) {
263
232k
    OPENSSL_memset(ret, 0, size);
264
232k
  }
265
232k
  return ret;
266
232k
}
267
268
229k
void *OPENSSL_calloc(size_t num, size_t size) {
269
229k
  if (size != 0 && num > SIZE_MAX / size) {
270
0
    OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
271
0
    return NULL;
272
0
  }
273
274
229k
  return OPENSSL_zalloc(num * size);
275
229k
}
276
277
930k
void OPENSSL_free(void *orig_ptr) {
278
930k
  if (orig_ptr == NULL) {
279
300k
    return;
280
300k
  }
281
282
630k
  if (OPENSSL_memory_free != NULL) {
283
0
    OPENSSL_memory_free(orig_ptr);
284
0
    return;
285
0
  }
286
287
630k
  void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX;
288
630k
  __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
289
290
630k
  size_t size = *(size_t *)ptr;
291
630k
  OPENSSL_cleanse(ptr, size + OPENSSL_MALLOC_PREFIX);
292
293
// ASan knows to intercept malloc and free, but not sdallocx.
294
#if defined(OPENSSL_ASAN)
295
  (void)sdallocx;
296
  free(ptr);
297
#else
298
630k
  if (sdallocx) {
299
0
    sdallocx(ptr, size + OPENSSL_MALLOC_PREFIX, 0 /* flags */);
300
630k
  } else {
301
630k
    free(ptr);
302
630k
  }
303
630k
#endif
304
630k
}
305
306
5.60k
void *OPENSSL_realloc(void *orig_ptr, size_t new_size) {
307
5.60k
  if (orig_ptr == NULL) {
308
1.39k
    return OPENSSL_malloc(new_size);
309
1.39k
  }
310
311
4.20k
  size_t old_size;
312
4.20k
  if (OPENSSL_memory_get_size != NULL) {
313
0
    old_size = OPENSSL_memory_get_size(orig_ptr);
314
4.20k
  } else {
315
4.20k
    void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX;
316
4.20k
    __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
317
4.20k
    old_size = *(size_t *)ptr;
318
4.20k
    __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
319
4.20k
  }
320
321
4.20k
  void *ret = OPENSSL_malloc(new_size);
322
4.20k
  if (ret == NULL) {
323
0
    return NULL;
324
0
  }
325
326
4.20k
  size_t to_copy = new_size;
327
4.20k
  if (old_size < to_copy) {
328
4.20k
    to_copy = old_size;
329
4.20k
  }
330
331
4.20k
  memcpy(ret, orig_ptr, to_copy);
332
4.20k
  OPENSSL_free(orig_ptr);
333
334
4.20k
  return ret;
335
4.20k
}
336
337
4.47M
void OPENSSL_cleanse(void *ptr, size_t len) {
338
#if defined(OPENSSL_WINDOWS)
339
  SecureZeroMemory(ptr, len);
340
#else
341
4.47M
  OPENSSL_memset(ptr, 0, len);
342
343
#if !defined(OPENSSL_NO_ASM)
344
  /* As best as we can tell, this is sufficient to break any optimisations that
345
     might try to eliminate "superfluous" memsets. If there's an easy way to
346
     detect memset_s, it would be better to use that. */
347
  __asm__ __volatile__("" : : "r"(ptr) : "memory");
348
#endif
349
4.47M
#endif  // !OPENSSL_NO_ASM
350
4.47M
}
351
352
0
void OPENSSL_clear_free(void *ptr, size_t unused) { OPENSSL_free(ptr); }
353
354
0
int CRYPTO_secure_malloc_init(size_t size, size_t min_size) { return 0; }
355
356
0
int CRYPTO_secure_malloc_initialized(void) { return 0; }
357
358
0
size_t CRYPTO_secure_used(void) { return 0; }
359
360
0
void *OPENSSL_secure_malloc(size_t size) { return OPENSSL_malloc(size); }
361
362
0
void OPENSSL_secure_clear_free(void *ptr, size_t len) {
363
0
  OPENSSL_clear_free(ptr, len);
364
0
}
365
366
742
int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) {
367
742
  const uint8_t *a = in_a;
368
742
  const uint8_t *b = in_b;
369
742
  uint8_t x = 0;
370
371
16.7k
  for (size_t i = 0; i < len; i++) {
372
16.0k
    x |= a[i] ^ b[i];
373
16.0k
  }
374
375
742
  return x;
376
742
}
377
378
0
uint32_t OPENSSL_hash32(const void *ptr, size_t len) {
379
  // These are the FNV-1a parameters for 32 bits.
380
0
  static const uint32_t kPrime = 16777619u;
381
0
  static const uint32_t kOffsetBasis = 2166136261u;
382
383
0
  const uint8_t *in = ptr;
384
0
  uint32_t h = kOffsetBasis;
385
386
0
  for (size_t i = 0; i < len; i++) {
387
0
    h ^= in[i];
388
0
    h *= kPrime;
389
0
  }
390
391
0
  return h;
392
0
}
393
394
0
uint32_t OPENSSL_strhash(const char *s) { return OPENSSL_hash32(s, strlen(s)); }
395
396
size_t OPENSSL_strnlen(const char *s, size_t len) {
397
  for (size_t i = 0; i < len; i++) {
398
    if (s[i] == 0) {
399
      return i;
400
    }
401
  }
402
403
  return len;
404
}
405
406
0
char *OPENSSL_strdup(const char *s) {
407
0
  if (s == NULL) {
408
0
    return NULL;
409
0
  }
410
  // Copy the NUL terminator.
411
0
  return OPENSSL_memdup(s, strlen(s) + 1);
412
0
}
413
414
0
int OPENSSL_isalpha(int c) {
415
0
  return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
416
0
}
417
418
3.67M
int OPENSSL_isdigit(int c) { return c >= '0' && c <= '9'; }
419
420
0
int OPENSSL_isxdigit(int c) {
421
0
  return OPENSSL_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
422
0
}
423
424
0
int OPENSSL_fromxdigit(uint8_t *out, int c) {
425
0
  if (OPENSSL_isdigit(c)) {
426
0
    *out = c - '0';
427
0
    return 1;
428
0
  }
429
0
  if ('a' <= c && c <= 'f') {
430
0
    *out = c - 'a' + 10;
431
0
    return 1;
432
0
  }
433
0
  if ('A' <= c && c <= 'F') {
434
0
    *out = c - 'A' + 10;
435
0
    return 1;
436
0
  }
437
0
  return 0;
438
0
}
439
440
0
int OPENSSL_isalnum(int c) { return OPENSSL_isalpha(c) || OPENSSL_isdigit(c); }
441
442
0
int OPENSSL_tolower(int c) {
443
0
  if (c >= 'A' && c <= 'Z') {
444
0
    return c + ('a' - 'A');
445
0
  }
446
0
  return c;
447
0
}
448
449
0
int OPENSSL_isspace(int c) {
450
0
  return c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' ||
451
0
         c == ' ';
452
0
}
453
454
int OPENSSL_strcasecmp(const char *a, const char *b) {
455
  for (size_t i = 0;; i++) {
456
    const int aa = OPENSSL_tolower(a[i]);
457
    const int bb = OPENSSL_tolower(b[i]);
458
459
    if (aa < bb) {
460
      return -1;
461
    } else if (aa > bb) {
462
      return 1;
463
    } else if (aa == 0) {
464
      return 0;
465
    }
466
  }
467
}
468
469
0
int OPENSSL_strncasecmp(const char *a, const char *b, size_t n) {
470
0
  for (size_t i = 0; i < n; i++) {
471
0
    const int aa = OPENSSL_tolower(a[i]);
472
0
    const int bb = OPENSSL_tolower(b[i]);
473
474
0
    if (aa < bb) {
475
0
      return -1;
476
0
    } else if (aa > bb) {
477
0
      return 1;
478
0
    } else if (aa == 0) {
479
0
      return 0;
480
0
    }
481
0
  }
482
483
0
  return 0;
484
0
}
485
486
78.5k
int BIO_snprintf(char *buf, size_t n, const char *format, ...) {
487
78.5k
  va_list args;
488
78.5k
  va_start(args, format);
489
78.5k
  int ret = BIO_vsnprintf(buf, n, format, args);
490
78.5k
  va_end(args);
491
78.5k
  return ret;
492
78.5k
}
493
494
0
int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) {
495
0
  return vsnprintf(buf, n, format, args);
496
0
}
497
498
int OPENSSL_vasprintf_internal(char **str, const char *format, va_list args,
499
0
                               int system_malloc) {
500
0
  void *(*allocate)(size_t) = system_malloc ? malloc : OPENSSL_malloc;
501
0
  void (*deallocate)(void *) = system_malloc ? free : OPENSSL_free;
502
0
  void *(*reallocate)(void *, size_t) =
503
0
      system_malloc ? realloc : OPENSSL_realloc;
504
0
  char *candidate = NULL;
505
0
  size_t candidate_len = 64;  // TODO(bbe) what's the best initial size?
506
507
0
  if ((candidate = allocate(candidate_len)) == NULL) {
508
0
    goto err;
509
0
  }
510
0
  va_list args_copy;
511
0
  va_copy(args_copy, args);
512
0
  int ret = vsnprintf(candidate, candidate_len, format, args_copy);
513
0
  va_end(args_copy);
514
0
  if (ret < 0) {
515
0
    goto err;
516
0
  }
517
0
  if ((size_t)ret >= candidate_len) {
518
    // Too big to fit in allocation.
519
0
    char *tmp;
520
521
0
    candidate_len = (size_t)ret + 1;
522
0
    if ((tmp = reallocate(candidate, candidate_len)) == NULL) {
523
0
      goto err;
524
0
    }
525
0
    candidate = tmp;
526
0
    ret = vsnprintf(candidate, candidate_len, format, args);
527
0
  }
528
  // At this point this should not happen unless vsnprintf is insane.
529
0
  if (ret < 0 || (size_t)ret >= candidate_len) {
530
0
    goto err;
531
0
  }
532
0
  *str = candidate;
533
0
  return ret;
534
535
0
err:
536
0
  deallocate(candidate);
537
0
  *str = NULL;
538
0
  errno = ENOMEM;
539
0
  return -1;
540
0
}
541
542
0
int OPENSSL_vasprintf(char **str, const char *format, va_list args) {
543
0
  return OPENSSL_vasprintf_internal(str, format, args, /*system_malloc=*/0);
544
0
}
545
546
0
int OPENSSL_asprintf(char **str, const char *format, ...) {
547
0
  va_list args;
548
0
  va_start(args, format);
549
0
  int ret = OPENSSL_vasprintf(str, format, args);
550
0
  va_end(args);
551
0
  return ret;
552
0
}
553
554
0
char *OPENSSL_strndup(const char *str, size_t size) {
555
0
  size = OPENSSL_strnlen(str, size);
556
557
0
  size_t alloc_size = size + 1;
558
0
  if (alloc_size < size) {
559
    // overflow
560
0
    OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
561
0
    return NULL;
562
0
  }
563
0
  char *ret = OPENSSL_malloc(alloc_size);
564
0
  if (ret == NULL) {
565
0
    return NULL;
566
0
  }
567
568
0
  OPENSSL_memcpy(ret, str, size);
569
0
  ret[size] = '\0';
570
0
  return ret;
571
0
}
572
573
2.76k
size_t OPENSSL_strlcpy(char *dst, const char *src, size_t dst_size) {
574
2.76k
  size_t l = 0;
575
576
10.5k
  for (; dst_size > 1 && *src; dst_size--) {
577
7.79k
    *dst++ = *src++;
578
7.79k
    l++;
579
7.79k
  }
580
581
2.76k
  if (dst_size) {
582
2.76k
    *dst = 0;
583
2.76k
  }
584
585
2.76k
  return l + strlen(src);
586
2.76k
}
587
588
0
size_t OPENSSL_strlcat(char *dst, const char *src, size_t dst_size) {
589
0
  size_t l = 0;
590
0
  for (; dst_size > 0 && *dst; dst_size--, dst++) {
591
0
    l++;
592
0
  }
593
0
  return l + OPENSSL_strlcpy(dst, src, dst_size);
594
0
}
595
596
31.4k
void *OPENSSL_memdup(const void *data, size_t size) {
597
31.4k
  if (size == 0) {
598
0
    return NULL;
599
0
  }
600
601
31.4k
  void *ret = OPENSSL_malloc(size);
602
31.4k
  if (ret == NULL) {
603
0
    return NULL;
604
0
  }
605
606
31.4k
  OPENSSL_memcpy(ret, data, size);
607
31.4k
  return ret;
608
31.4k
}
609
610
0
void *CRYPTO_malloc(size_t size, const char *file, int line) {
611
0
  return OPENSSL_malloc(size);
612
0
}
613
614
0
void *CRYPTO_realloc(void *ptr, size_t new_size, const char *file, int line) {
615
0
  return OPENSSL_realloc(ptr, new_size);
616
0
}
617
618
void CRYPTO_free(void *ptr, const char *file, int line) { OPENSSL_free(ptr); }