Skip to content

Commit

Permalink
Merge pull request #7030 from daverodgman/sizeof-brackets-2.28
Browse files Browse the repository at this point in the history
Backport 2.28 - Sizeof brackets
  • Loading branch information
mpg authored Feb 3, 2023
2 parents 45379cb + ecb4420 commit fc9cb13
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 49 deletions.
2 changes: 1 addition & 1 deletion library/ccm.c
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ int mbedtls_ccm_self_test(int verbose)
mbedtls_ccm_init(&ctx);

if (mbedtls_ccm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key_test_data,
8 * sizeof key_test_data) != 0) {
8 * sizeof(key_test_data)) != 0) {
if (verbose != 0) {
mbedtls_printf(" CCM: setup failed");
}
Expand Down
8 changes: 4 additions & 4 deletions library/ecp_curves.c
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ static const mbedtls_mpi_uint brainpoolP512r1_n[] = {
#if defined(ECP_LOAD_GROUP)
/*
* Create an MPI from embedded constants
* (assumes len is an exact multiple of sizeof mbedtls_mpi_uint)
* (assumes len is an exact multiple of sizeof(mbedtls_mpi_uint))
*/
static inline void ecp_mpi_load(mbedtls_mpi *X, const mbedtls_mpi_uint *p, size_t len)
{
Expand Down Expand Up @@ -1252,7 +1252,7 @@ static int ecp_mod_p255(mbedtls_mpi *N)
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}
M.p = Mp;
memset(Mp, 0, sizeof Mp);
memset(Mp, 0, sizeof(Mp));
memcpy(Mp, N->p + P255_WIDTH - 1, M.n * sizeof(mbedtls_mpi_uint));
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&M, 255 % (8 * sizeof(mbedtls_mpi_uint))));
M.n++; /* Make room for multiplication by 19 */
Expand Down Expand Up @@ -1386,7 +1386,7 @@ static inline int ecp_mod_koblitz(mbedtls_mpi *N, mbedtls_mpi_uint *Rp, size_t p
if (M.n > p_limbs + adjust) {
M.n = p_limbs + adjust;
}
memset(Mp, 0, sizeof Mp);
memset(Mp, 0, sizeof(Mp));
memcpy(Mp, N->p + p_limbs - adjust, M.n * sizeof(mbedtls_mpi_uint));
if (shift != 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&M, shift));
Expand All @@ -1412,7 +1412,7 @@ static inline int ecp_mod_koblitz(mbedtls_mpi *N, mbedtls_mpi_uint *Rp, size_t p
if (M.n > p_limbs + adjust) {
M.n = p_limbs + adjust;
}
memset(Mp, 0, sizeof Mp);
memset(Mp, 0, sizeof(Mp));
memcpy(Mp, N->p + p_limbs - adjust, M.n * sizeof(mbedtls_mpi_uint));
if (shift != 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&M, shift));
Expand Down
2 changes: 1 addition & 1 deletion library/entropy.c
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ int mbedtls_entropy_self_test(int verbose)
goto cleanup;
}

if ((ret = mbedtls_entropy_update_manual(&ctx, buf, sizeof buf)) != 0) {
if ((ret = mbedtls_entropy_update_manual(&ctx, buf, sizeof(buf))) != 0) {
goto cleanup;
}

Expand Down
2 changes: 1 addition & 1 deletion library/ripemd160.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ int mbedtls_ripemd160_self_test(int verbose)
int i, ret = 0;
unsigned char output[20];

memset(output, 0, sizeof output);
memset(output, 0, sizeof(output));

for (i = 0; i < TESTS; i++) {
if (verbose != 0) {
Expand Down
4 changes: 2 additions & 2 deletions library/x509_crt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1645,10 +1645,10 @@ int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
memset(&sb, 0, sizeof(sb));

while ((entry = readdir(dir)) != NULL) {
snp_ret = mbedtls_snprintf(entry_name, sizeof entry_name,
snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
"%s/%s", path, entry->d_name);

if (snp_ret < 0 || (size_t) snp_ret >= sizeof entry_name) {
if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
goto cleanup;
} else if (stat(entry_name, &sb) == -1) {
Expand Down
2 changes: 1 addition & 1 deletion programs/pkey/ecdh_curve25519.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ int main(int argc, char *argv[])
if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
&entropy,
(const unsigned char *) pers,
sizeof pers)) != 0) {
sizeof(pers))) != 0) {
mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
ret);
goto exit;
Expand Down
2 changes: 1 addition & 1 deletion programs/pkey/ecdsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static void dump_pubkey(const char *title, mbedtls_ecdsa_context *key)
size_t len;

if (mbedtls_ecp_point_write_binary(&key->grp, &key->Q,
MBEDTLS_ECP_PF_UNCOMPRESSED, &len, buf, sizeof buf) != 0) {
MBEDTLS_ECP_PF_UNCOMPRESSED, &len, buf, sizeof(buf)) != 0) {
mbedtls_printf("internal error\n");
return;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/suites/test_suite_ctr_drbg.function
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ void ctr_drbg_entropy_usage(int entropy_nonce_len)
}
TEST_EQUAL(test_offset_idx, expected_idx);

/* Call update with too much data (sizeof entropy > MAX(_SEED)_INPUT).
/* Call update with too much data (sizeof(entropy) > MAX(_SEED)_INPUT).
* Make sure it's detected as an error and doesn't cause memory
* corruption. */
TEST_ASSERT(mbedtls_ctr_drbg_update_ret(
Expand Down
24 changes: 12 additions & 12 deletions tests/suites/test_suite_mdx.function
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ void md2_text(char *text_src_string, data_t *hash)
unsigned char src_str[100];
unsigned char output[16];

memset(src_str, 0x00, sizeof src_str);
memset(output, 0x00, sizeof output);
memset(src_str, 0x00, sizeof(src_str));
memset(output, 0x00, sizeof(output));

strncpy((char *) src_str, text_src_string, sizeof(src_str) - 1);

ret = mbedtls_md2_ret(src_str, strlen((char *) src_str), output);
TEST_ASSERT(ret == 0);

TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
sizeof output, hash->len) == 0);
sizeof(output), hash->len) == 0);
}
/* END_CASE */

Expand All @@ -32,16 +32,16 @@ void md4_text(char *text_src_string, data_t *hash)
unsigned char src_str[100];
unsigned char output[16];

memset(src_str, 0x00, sizeof src_str);
memset(output, 0x00, sizeof output);
memset(src_str, 0x00, sizeof(src_str));
memset(output, 0x00, sizeof(output));

strncpy((char *) src_str, text_src_string, sizeof(src_str) - 1);

ret = mbedtls_md4_ret(src_str, strlen((char *) src_str), output);
TEST_ASSERT(ret == 0);

TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
sizeof output, hash->len) == 0);
sizeof(output), hash->len) == 0);
}
/* END_CASE */

Expand All @@ -52,16 +52,16 @@ void md5_text(char *text_src_string, data_t *hash)
unsigned char src_str[100];
unsigned char output[16];

memset(src_str, 0x00, sizeof src_str);
memset(output, 0x00, sizeof output);
memset(src_str, 0x00, sizeof(src_str));
memset(output, 0x00, sizeof(output));

strncpy((char *) src_str, text_src_string, sizeof(src_str) - 1);

ret = mbedtls_md5_ret(src_str, strlen((char *) src_str), output);
TEST_ASSERT(ret == 0);

TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
sizeof output, hash->len) == 0);
sizeof(output), hash->len) == 0);
}
/* END_CASE */

Expand All @@ -72,16 +72,16 @@ void ripemd160_text(char *text_src_string, data_t *hash)
unsigned char src_str[100];
unsigned char output[20];

memset(src_str, 0x00, sizeof src_str);
memset(output, 0x00, sizeof output);
memset(src_str, 0x00, sizeof(src_str));
memset(output, 0x00, sizeof(output));

strncpy((char *) src_str, text_src_string, sizeof(src_str) - 1);

ret = mbedtls_ripemd160_ret(src_str, strlen((char *) src_str), output);
TEST_ASSERT(ret == 0);

TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
sizeof output, hash->len) == 0);
sizeof(output), hash->len) == 0);
}
/* END_CASE */

Expand Down
42 changes: 21 additions & 21 deletions tests/suites/test_suite_pk.function
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ void pk_sign_verify(int type, int parameter, int sign_ret, int verify_ret)
USE_PSA_INIT();

memset(hash, 0x2a, hash_len);
memset(sig, 0, sizeof sig);
memset(sig, 0, sizeof(sig));

TEST_ASSERT(mbedtls_pk_setup(&pk, mbedtls_pk_info_from_type(type)) == 0);
TEST_ASSERT(pk_genkey(&pk, parameter) == 0);
Expand Down Expand Up @@ -1095,8 +1095,8 @@ void pk_rsa_overflow()
return;
}

memset(hash, 0x2a, sizeof hash);
memset(sig, 0, sizeof sig);
memset(hash, 0x2a, sizeof(hash));
memset(sig, 0, sizeof(sig));

mbedtls_pk_init(&pk);

Expand Down Expand Up @@ -1140,11 +1140,11 @@ void pk_rsa_alt()
mbedtls_rsa_init(&raw, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE);
mbedtls_pk_init(&rsa); mbedtls_pk_init(&alt);

memset(hash, 0x2a, sizeof hash);
memset(sig, 0, sizeof sig);
memset(msg, 0x2a, sizeof msg);
memset(ciph, 0, sizeof ciph);
memset(test, 0, sizeof test);
memset(hash, 0x2a, sizeof(hash));
memset(sig, 0, sizeof(sig));
memset(msg, 0x2a, sizeof(msg));
memset(ciph, 0, sizeof(ciph));
memset(test, 0, sizeof(test));

/* Initialize PK RSA context with random key */
TEST_ASSERT(mbedtls_pk_setup(&rsa,
Expand Down Expand Up @@ -1172,29 +1172,29 @@ void pk_rsa_alt()
&sig_len, mbedtls_test_rnd_std_rand, NULL)
== MBEDTLS_ERR_PK_BAD_INPUT_DATA);
#endif /* SIZE_MAX > UINT_MAX */
TEST_ASSERT(mbedtls_pk_sign(&alt, MBEDTLS_MD_NONE, hash, sizeof hash, sig,
TEST_ASSERT(mbedtls_pk_sign(&alt, MBEDTLS_MD_NONE, hash, sizeof(hash), sig,
&sig_len, mbedtls_test_rnd_std_rand, NULL)
== 0);
TEST_ASSERT(sig_len == RSA_KEY_LEN);
TEST_ASSERT(mbedtls_pk_verify(&rsa, MBEDTLS_MD_NONE,
hash, sizeof hash, sig, sig_len) == 0);
hash, sizeof(hash), sig, sig_len) == 0);

/* Test decrypt */
TEST_ASSERT(mbedtls_pk_encrypt(&rsa, msg, sizeof msg,
ciph, &ciph_len, sizeof ciph,
TEST_ASSERT(mbedtls_pk_encrypt(&rsa, msg, sizeof(msg),
ciph, &ciph_len, sizeof(ciph),
mbedtls_test_rnd_std_rand, NULL) == 0);
TEST_ASSERT(mbedtls_pk_decrypt(&alt, ciph, ciph_len,
test, &test_len, sizeof test,
test, &test_len, sizeof(test),
mbedtls_test_rnd_std_rand, NULL) == 0);
TEST_ASSERT(test_len == sizeof msg);
TEST_ASSERT(test_len == sizeof(msg));
TEST_ASSERT(memcmp(test, msg, test_len) == 0);

/* Test forbidden operations */
TEST_ASSERT(mbedtls_pk_encrypt(&alt, msg, sizeof msg,
ciph, &ciph_len, sizeof ciph,
TEST_ASSERT(mbedtls_pk_encrypt(&alt, msg, sizeof(msg),
ciph, &ciph_len, sizeof(ciph),
mbedtls_test_rnd_std_rand, NULL) == ret);
TEST_ASSERT(mbedtls_pk_verify(&alt, MBEDTLS_MD_NONE,
hash, sizeof hash, sig, sig_len) == ret);
hash, sizeof(hash), sig, sig_len) == ret);
TEST_ASSERT(mbedtls_pk_debug(&alt, dbg_items) == ret);

exit:
Expand Down Expand Up @@ -1257,11 +1257,11 @@ void pk_psa_sign(int grpid_arg,
TEST_EQUAL(psa_get_key_lifetime(&attributes),
PSA_KEY_LIFETIME_VOLATILE);

memset(hash, 0x2a, sizeof hash);
memset(sig, 0, sizeof sig);
memset(hash, 0x2a, sizeof(hash));
memset(sig, 0, sizeof(sig));

TEST_ASSERT(mbedtls_pk_sign(&pk, MBEDTLS_MD_SHA256,
hash, sizeof hash, sig, &sig_len,
hash, sizeof(hash), sig, &sig_len,
NULL, NULL) == 0);

/* Export underlying public key for re-importing in a psa context. */
Expand All @@ -1282,7 +1282,7 @@ void pk_psa_sign(int grpid_arg,
TEST_ASSERT(mbedtls_pk_parse_public_key(&pk, pkey_legacy_start,
klen_legacy) == 0);
TEST_ASSERT(mbedtls_pk_verify(&pk, MBEDTLS_MD_SHA256,
hash, sizeof hash, sig, sig_len) == 0);
hash, sizeof(hash), sig, sig_len) == 0);

exit:
/*
Expand Down
8 changes: 4 additions & 4 deletions tests/suites/test_suite_x509parse.function
Original file line number Diff line number Diff line change
Expand Up @@ -1117,9 +1117,9 @@ void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,

/* Load a chain with nb_int intermediates (from 01 to nb_int),
* plus one "end-entity" cert (nb_int + 1) */
ret = mbedtls_snprintf(file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir,
ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
nb_int + 1);
TEST_ASSERT(ret > 0 && (size_t) ret < sizeof file_buf);
TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, file_buf) == 0);

/* Try to verify that chain */
Expand Down Expand Up @@ -1212,13 +1212,13 @@ void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
mbedtls_x509_buf oid;
char num_buf[100];

memset(num_buf, 0x2a, sizeof num_buf);
memset(num_buf, 0x2a, sizeof(num_buf));

oid.tag = MBEDTLS_ASN1_OID;
oid.p = oid_buf->x;
oid.len = oid_buf->len;

TEST_ASSERT((size_t) blen <= sizeof num_buf);
TEST_ASSERT((size_t) blen <= sizeof(num_buf));

TEST_ASSERT(mbedtls_oid_get_numeric_string(num_buf, blen, &oid) == ret);

Expand Down

0 comments on commit fc9cb13

Please sign in to comment.