Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/array_alloc.c
Line
Count
Source
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
6.81M
{
20
6.81M
    size_t bytes;
21
22
6.81M
    if (ossl_unlikely(!ossl_size_mul(num, size, &bytes, file, line)))
23
0
        return NULL;
24
25
6.81M
    return CRYPTO_malloc(bytes, file, line);
26
6.81M
}
27
28
void *CRYPTO_calloc(size_t num, size_t size, const char *file, int line)
29
35.2M
{
30
35.2M
    size_t bytes;
31
32
35.2M
    if (ossl_unlikely(!ossl_size_mul(num, size, &bytes, file, line)))
33
0
        return NULL;
34
35
35.2M
    return CRYPTO_zalloc(bytes, file, line);
36
35.2M
}
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
63.2k
{
41
63.2k
    size_t bytes;
42
43
63.2k
    if (ossl_unlikely(!ossl_size_mul(num, size, &bytes, file, line))) {
44
0
        *freeptr = NULL;
45
46
0
        return NULL;
47
0
    }
48
49
63.2k
    return CRYPTO_aligned_alloc(bytes, align, freeptr, file, line);
50
63.2k
}
51
52
void *CRYPTO_realloc_array(void *addr, size_t num, size_t size,
53
    const char *file, int line)
54
2.39M
{
55
2.39M
    size_t bytes;
56
57
2.39M
    if (ossl_unlikely(!ossl_size_mul(num, size, &bytes, file, line)))
58
0
        return NULL;
59
60
2.39M
    return CRYPTO_realloc(addr, bytes, file, line);
61
2.39M
}
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
1.11k
{
78
1.11k
    size_t bytes;
79
80
1.11k
    if (ossl_unlikely(!ossl_size_mul(num, size, &bytes, file, line)))
81
0
        return NULL;
82
83
1.11k
    return CRYPTO_secure_malloc(bytes, file, line);
84
1.11k
}
85
86
void *CRYPTO_secure_calloc(size_t num, size_t size, const char *file, int line)
87
328k
{
88
328k
    size_t bytes;
89
90
328k
    if (ossl_unlikely(!ossl_size_mul(num, size, &bytes, file, line)))
91
0
        return NULL;
92
93
328k
    return CRYPTO_secure_zalloc(bytes, file, line);
94
328k
}