Coverage Report

Created: 2025-08-25 06:30

/src/openssl/crypto/array_alloc.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2025 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
/*
11
 * This file provides implementation of various array allocation routines that
12
 * perform integer overflow checking for size calculation.
13
 */
14
15
#include "internal/mem_alloc_utils.h"
16
#include <openssl/crypto.h>
17
18
void *CRYPTO_malloc_array(size_t num, size_t size, const char *file, int line)
19
2.56k
{
20
2.56k
    size_t bytes;
21
22
2.56k
    if (ossl_unlikely(!ossl_size_mul(num, size, &bytes, file, line)))
23
0
        return NULL;
24
25
2.56k
    return CRYPTO_malloc(bytes, file, line);
26
2.56k
}
27
28
void *CRYPTO_calloc(size_t num, size_t size, const char *file, int line)
29
49.6k
{
30
49.6k
    size_t bytes;
31
32
49.6k
    if (ossl_unlikely(!ossl_size_mul(num, size, &bytes, file, line)))
33
0
        return NULL;
34
35
49.6k
    return CRYPTO_zalloc(bytes, file, line);
36
49.6k
}
37
38
void *CRYPTO_aligned_alloc_array(size_t num, size_t size, size_t align,
39
                                 void **freeptr, const char *file, int line)
40
32
{
41
32
    size_t bytes;
42
43
32
    if (ossl_unlikely(!ossl_size_mul(num, size, &bytes, file, line))) {
44
0
        *freeptr = NULL;
45
46
0
        return NULL;
47
0
    }
48
49
32
    return CRYPTO_aligned_alloc(bytes, align, freeptr, file, line);
50
32
}
51
52
void *CRYPTO_realloc_array(void *addr, size_t num, size_t size,
53
                           const char *file, int line)
54
368
{
55
368
    size_t bytes;
56
57
368
    if (ossl_unlikely(!ossl_size_mul(num, size, &bytes, file, line)))
58
0
        return NULL;
59
60
368
    return CRYPTO_realloc(addr, bytes, file, line);
61
368
}
62
63
void *CRYPTO_clear_realloc_array(void *addr, size_t old_num, size_t num,
64
                                 size_t size, const char *file, int line)
65
0
{
66
0
    size_t old_bytes, bytes = 0;
67
68
0
    if (ossl_unlikely(!ossl_size_mul(old_num, size, &old_bytes, file, line)
69
0
                      || !ossl_size_mul(num, size, &bytes, file, line)))
70
0
        return NULL;
71
72
0
    return CRYPTO_clear_realloc(addr, old_bytes, bytes, file, line);
73
0
}
74
75
void *CRYPTO_secure_malloc_array(size_t num, size_t size,
76
                                 const char *file, int line)
77
0
{
78
0
    size_t bytes;
79
80
0
    if (ossl_unlikely(!ossl_size_mul(num, size, &bytes, file, line)))
81
0
        return NULL;
82
83
0
    return CRYPTO_secure_malloc(bytes, file, line);
84
0
}
85
86
void *CRYPTO_secure_calloc(size_t num, size_t size, const char *file, int line)
87
0
{
88
0
    size_t bytes;
89
90
0
    if (ossl_unlikely(!ossl_size_mul(num, size, &bytes, file, line)))
91
0
        return NULL;
92
93
0
    return CRYPTO_secure_zalloc(bytes, file, line);
94
0
}