test: use array memory (re)allocation routines

Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org>

Reviewed-by: Saša Nedvědický <sashan@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <ppzgs1@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/28059)
This commit is contained in:
Eugene Syromiatnikov 2025-07-17 15:19:56 +02:00 committed by Neil Horman
parent 351caebeac
commit 5fab189ddd
15 changed files with 28 additions and 27 deletions

View File

@ -96,7 +96,7 @@ static int setup_bio_chain(const char *progname)
BIO *next = NULL;
size_t n = amount;
chain = OPENSSL_zalloc(sizeof(*chain) * n);
chain = OPENSSL_calloc(n, sizeof(*chain));
if (chain != NULL) {
size_t i;

View File

@ -2355,7 +2355,7 @@ static int test_rand_range_single(size_t n)
unsigned int i, v;
int res = 0;
if (!TEST_ptr(counts = OPENSSL_zalloc(sizeof(*counts) * range))
if (!TEST_ptr(counts = OPENSSL_calloc(range, sizeof(*counts)))
|| !TEST_ptr(rng = BN_new())
|| !TEST_ptr(val = BN_new())
|| !TEST_true(BN_set_word(rng, range)))

View File

@ -549,7 +549,7 @@ static int named_group_creation_test(void)
int setup_tests(void)
{
crv_len = EC_get_builtin_curves(NULL, 0);
if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
if (!TEST_ptr(curves = OPENSSL_malloc_array(crv_len, sizeof(*curves)))
|| !TEST_true(EC_get_builtin_curves(curves, crv_len)))
return 0;

View File

@ -399,7 +399,7 @@ int setup_tests(void)
/* get a list of all internal curves */
crv_len = EC_get_builtin_curves(NULL, 0);
if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
if (!TEST_ptr(curves = OPENSSL_malloc_array(crv_len, sizeof(*curves)))
|| !TEST_true(EC_get_builtin_curves(curves, crv_len))) {
fake_rand_finish(fake_rand);
return 0;

View File

@ -3125,7 +3125,7 @@ static int ec_d2i_publickey_test(void)
int setup_tests(void)
{
crv_len = EC_get_builtin_curves(NULL, 0);
if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
if (!TEST_ptr(curves = OPENSSL_malloc_array(crv_len, sizeof(*curves)))
|| !TEST_true(EC_get_builtin_curves(curves, crv_len)))
return 0;

View File

@ -38,7 +38,7 @@ static EVP_KDF_CTX *get_kdfbyname(const char *name)
static OSSL_PARAM *construct_tls1_prf_params(const char *digest, const char *secret,
const char *seed)
{
OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 4);
OSSL_PARAM *params = OPENSSL_malloc_array(4, sizeof(OSSL_PARAM));
OSSL_PARAM *p = params;
if (params == NULL)
@ -194,7 +194,7 @@ static int test_kdf_tls1_prf_1byte_seed(void)
static OSSL_PARAM *construct_hkdf_params(char *digest, char *key,
size_t keylen, char *salt, char *info)
{
OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 5);
OSSL_PARAM *params = OPENSSL_malloc_array(5, sizeof(OSSL_PARAM));
OSSL_PARAM *p = params;
if (params == NULL)
@ -661,7 +661,7 @@ static int test_kdf_hkdf_empty_salt(void)
static OSSL_PARAM *construct_pbkdf1_params(char *pass, char *digest, char *salt,
unsigned int *iter)
{
OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 5);
OSSL_PARAM *params = OPENSSL_malloc_array(5, sizeof(OSSL_PARAM));
OSSL_PARAM *p = params;
if (params == NULL)
@ -781,7 +781,7 @@ err:
static OSSL_PARAM *construct_pbkdf2_params(char *pass, char *digest, char *salt,
unsigned int *iter, int *mode)
{
OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 6);
OSSL_PARAM *params = OPENSSL_malloc_array(6, sizeof(OSSL_PARAM));
OSSL_PARAM *p = params;
if (params == NULL)
@ -1310,7 +1310,7 @@ static int test_kdf_kbkdf_6803_256(void)
static OSSL_PARAM *construct_kbkdf_params(char *digest, char *mac, unsigned char *key,
size_t keylen, char *salt, char *info, int *r)
{
OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 8);
OSSL_PARAM *params = OPENSSL_malloc_array(8, sizeof(OSSL_PARAM));
OSSL_PARAM *p = params;
if (params == NULL)

View File

@ -2143,7 +2143,7 @@ static int test_check_dsa(void)
static OSSL_PARAM *do_construct_hkdf_params(char *digest, char *key,
size_t keylen, char *salt)
{
OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 5);
OSSL_PARAM *params = OPENSSL_malloc_array(5, sizeof(OSSL_PARAM));
OSSL_PARAM *p = params;
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, digest, 0);

View File

@ -4533,7 +4533,7 @@ static int keygen_test_run(EVP_TEST *t)
}
if (sk_OPENSSL_STRING_num(keygen->in_controls) > 0) {
if ((params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 4)) == NULL)
if ((params = OPENSSL_malloc_array(4, sizeof(OSSL_PARAM))) == NULL)
goto err;
if (!ctrl2params(t, keygen->in_controls,
EVP_PKEY_CTX_settable_params(genctx),

View File

@ -567,21 +567,21 @@ static size_t build_request_set(SSL *ssl)
/*
* Expand our poll_list, outbiolist, and outnames arrays
*/
poll_list = OPENSSL_realloc(poll_list,
sizeof(SSL_POLL_ITEM) * poll_count);
poll_list = OPENSSL_realloc_array(poll_list,
poll_count, sizeof(SSL_POLL_ITEM));
if (poll_list == NULL) {
fprintf(stderr, "Unable to realloc poll_list\n");
goto err;
}
outbiolist = OPENSSL_realloc(outbiolist,
sizeof(BIO *) * poll_count);
outbiolist = OPENSSL_realloc_array(outbiolist,
poll_count, sizeof(BIO *));
if (outbiolist == NULL) {
fprintf(stderr, "Unable to realloc outbiolist\n");
goto err;
}
outnames = OPENSSL_realloc(outnames, sizeof(char *) * poll_count);
outnames = OPENSSL_realloc_array(outnames, poll_count, sizeof(char *));
if (outnames == NULL) {
fprintf(stderr, "Unable to realloc outnames\n");
goto err;
@ -922,7 +922,8 @@ int main(int argc, char *argv[])
while (req != NULL) {
total_requests++;
req_array = OPENSSL_realloc(req_array, sizeof(char *) * total_requests);
req_array = OPENSSL_realloc_array(req_array,
total_requests, sizeof(char *));
if (req_array == NULL)
goto end;
req_array[total_requests - 1] = req;

View File

@ -112,7 +112,7 @@ static int helper_init(struct helper *h, size_t num_pkts)
/* Allocate our array of packet information. */
h->num_pkts = num_pkts;
if (num_pkts > 0) {
h->pkts = OPENSSL_zalloc(sizeof(struct pkt_info) * num_pkts);
h->pkts = OPENSSL_calloc(num_pkts, sizeof(struct pkt_info));
if (!TEST_ptr(h->pkts))
goto err;
} else {
@ -936,11 +936,11 @@ static int test_rx_ack_actual(int tidx, int space)
num_tx += s->num_pn;
/* Allocate packet information structures. */
txs = OPENSSL_zalloc(sizeof(*txs) * num_tx);
txs = OPENSSL_calloc(num_tx, sizeof(*txs));
if (!TEST_ptr(txs))
goto err;
pkts = OPENSSL_zalloc(sizeof(*pkts) * num_tx);
pkts = OPENSSL_calloc(num_tx, sizeof(*pkts));
if (!TEST_ptr(pkts))
goto err;

View File

@ -1886,7 +1886,7 @@ static int run_script_worker(struct helper *h, const struct script_op *script,
goto out;
}
h->threads = OPENSSL_zalloc(op->arg1 * sizeof(struct child_thread_args));
h->threads = OPENSSL_calloc(op->arg1, sizeof(struct child_thread_args));
if (!TEST_ptr(h->threads))
goto out;

View File

@ -480,7 +480,7 @@ static int slh_dsa_deterministic_usage_test(void)
if (!TEST_int_eq(EVP_PKEY_sign(sctx, NULL, &sig_len, msg, msg_len), 1))
goto err;
len = sig_len;
if (!TEST_ptr(sig = OPENSSL_zalloc(sig_len * 2))
if (!TEST_ptr(sig = OPENSSL_calloc(2, sig_len))
|| !TEST_int_eq(EVP_PKEY_sign(sctx, sig, &len, msg, msg_len), 1)
|| !TEST_size_t_eq(sig_len, len)
|| !TEST_int_eq(EVP_PKEY_sign(dupctx, sig + sig_len, &len,

View File

@ -7969,7 +7969,7 @@ static int ssl_srp_cb(SSL *s, int *ad, void *arg)
static int create_new_vfile(char *userid, char *password, const char *filename)
{
char *gNid = NULL;
OPENSSL_STRING *row = OPENSSL_zalloc(sizeof(row) * (DB_NUMBER + 1));
OPENSSL_STRING *row = OPENSSL_calloc(DB_NUMBER + 1, sizeof(row));
TXT_DB *db = NULL;
int ret = 0;
BIO *out = NULL, *dummy = BIO_new_mem_buf("", 0);

View File

@ -154,7 +154,7 @@ static int setup_cipher_list(void)
* so that some of the allocated space will be wasted, but the loss
* is deemed acceptable...
*/
cipher_list = OPENSSL_malloc(sk_SSL_CIPHER_num(sk_ciphers) *
cipher_list = OPENSSL_malloc_array(sk_SSL_CIPHER_num(sk_ciphers),
sizeof(cipher_list[0]));
if (!TEST_ptr(cipher_list))
goto err;

View File

@ -299,7 +299,7 @@ static void test_fail_bignum_common(const char *prefix, const char *file,
len = ((l1 > l2 ? l1 : l2) + bytes - 1) / bytes * bytes;
if (len > MEM_BUFFER_SIZE && (bufp = OPENSSL_malloc(len * 2)) == NULL) {
if (len > MEM_BUFFER_SIZE && (bufp = OPENSSL_malloc_array(2, len)) == NULL) {
bufp = buffer;
len = MEM_BUFFER_SIZE;
test_printf_stderr("WARNING: these BIGNUMs have been truncated\n");