From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Wed, 13 Sep 2023 20:31:50 +0000 (+0200) Subject: Prefer ARRAY_SIZE(...) X-Git-Tag: openssl-3.4.0-alpha1~298 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=001b92d68d61250c88b355773142af31675ca0ab;p=thirdparty%2Fopenssl.git Prefer ARRAY_SIZE(...) In OpenSSL, it's actually OSSL_NELEM() in "internal/nelem.h". Found by running the checkpatch.pl Linux script to enforce coding style. Reviewed-by: Neil Horman Reviewed-by: David von Oheimb Reviewed-by: Tomas Mraz (Merged from https://github.com/openssl/openssl/pull/22097) --- diff --git a/crypto/bn/bn_mod.c b/crypto/bn/bn_mod.c index d7c2f4bd5bf..4cb64b453b8 100644 --- a/crypto/bn/bn_mod.c +++ b/crypto/bn/bn_mod.c @@ -8,6 +8,7 @@ */ #include "internal/cryptlib.h" +#include "internal/nelem.h" #include "bn_local.h" int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx) @@ -61,7 +62,7 @@ int bn_mod_add_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, if (bn_wexpand(r, mtop) == NULL) return 0; - if (mtop > sizeof(storage) / sizeof(storage[0])) { + if (mtop > OSSL_NELEM(storage)) { tp = OPENSSL_malloc(mtop * sizeof(BN_ULONG)); if (tp == NULL) return 0; diff --git a/crypto/sha/keccak1600.c b/crypto/sha/keccak1600.c index 6682367be19..2ae1c9be9b7 100644 --- a/crypto/sha/keccak1600.c +++ b/crypto/sha/keccak1600.c @@ -11,6 +11,8 @@ #include #include +#include "internal/nelem.h" + size_t SHA3_absorb(uint64_t A[5][5], const unsigned char *inp, size_t len, size_t r); void SHA3_squeeze(uint64_t A[5][5], unsigned char *out, size_t len, size_t r, int next); @@ -231,7 +233,7 @@ static void Chi(uint64_t A[5][5]) static void Iota(uint64_t A[5][5], size_t i) { - assert(i < (sizeof(iotas) / sizeof(iotas[0]))); + assert(i < OSSL_NELEM(iotas)); A[0][0] ^= iotas[i]; } @@ -264,7 +266,7 @@ static void Round(uint64_t A[5][5], size_t i) uint64_t C[5], E[2]; /* registers */ uint64_t D[5], T[2][5]; /* memory */ - assert(i < (sizeof(iotas) / sizeof(iotas[0]))); + assert(i < OSSL_NELEM(iotas)); C[0] = A[0][0] ^ A[1][0] ^ A[2][0] ^ A[3][0] ^ A[4][0]; C[1] = A[0][1] ^ A[1][1] ^ A[2][1] ^ A[3][1] ^ A[4][1]; @@ -391,7 +393,7 @@ static void Round(uint64_t A[5][5], size_t i) { uint64_t C[5], D[5]; - assert(i < (sizeof(iotas) / sizeof(iotas[0]))); + assert(i < OSSL_NELEM(iotas)); C[0] = A[0][0] ^ A[1][0] ^ A[2][0] ^ A[3][0] ^ A[4][0]; C[1] = A[0][1] ^ A[1][1] ^ A[2][1] ^ A[3][1] ^ A[4][1]; @@ -536,7 +538,7 @@ static void Round(uint64_t R[5][5], uint64_t A[5][5], size_t i) { uint64_t C[5], D[5]; - assert(i < (sizeof(iotas) / sizeof(iotas[0]))); + assert(i < OSSL_NELEM(iotas)); C[0] = A[0][0] ^ A[1][0] ^ A[2][0] ^ A[3][0] ^ A[4][0]; C[1] = A[0][1] ^ A[1][1] ^ A[2][1] ^ A[3][1] ^ A[4][1]; @@ -694,7 +696,7 @@ static void FourRounds(uint64_t A[5][5], size_t i) { uint64_t B[5], C[5], D[5]; - assert(i <= (sizeof(iotas) / sizeof(iotas[0]) - 4)); + assert(i <= OSSL_NELEM(iotas) - 4); /* Round 4*n */ C[0] = A[0][0] ^ A[1][0] ^ A[2][0] ^ A[3][0] ^ A[4][0]; diff --git a/crypto/store/store_strings.c b/crypto/store/store_strings.c index 3d4a8ea7307..f9fbd618060 100644 --- a/crypto/store/store_strings.c +++ b/crypto/store/store_strings.c @@ -9,6 +9,8 @@ #include +#include "internal/nelem.h" + static char *type_strings[] = { "Name", /* OSSL_STORE_INFO_NAME */ "Parameters", /* OSSL_STORE_INFO_PARAMS */ @@ -20,7 +22,7 @@ static char *type_strings[] = { const char *OSSL_STORE_INFO_type_string(int type) { - int types = sizeof(type_strings) / sizeof(type_strings[0]); + int types = OSSL_NELEM(type_strings); if (type < 1 || type > types) return NULL; diff --git a/engines/e_afalg.c b/engines/e_afalg.c index 3644524e9fc..413480d725f 100644 --- a/engines/e_afalg.c +++ b/engines/e_afalg.c @@ -773,7 +773,7 @@ static int afalg_ciphers(ENGINE *e, const EVP_CIPHER **cipher, if (cipher == NULL) { *nids = afalg_cipher_nids; - return (sizeof(afalg_cipher_nids) / sizeof(afalg_cipher_nids[0])); + return OSSL_NELEM(afalg_cipher_nids); } switch (nid) { diff --git a/test/asn1_encode_test.c b/test/asn1_encode_test.c index 335f24e1133..834b2cdd642 100644 --- a/test/asn1_encode_test.c +++ b/test/asn1_encode_test.c @@ -738,8 +738,7 @@ static int test_intern(const TEST_PACKAGE *package) /* Do decode_custom checks */ nelems = package->encode_expectations_size / package->encode_expectations_elem_size; - OPENSSL_assert(nelems == - sizeof(test_custom_data) / sizeof(test_custom_data[0])); + OPENSSL_assert(nelems == OSSL_NELEM(test_custom_data)); for (i = 0; i < nelems; i++) { size_t pos = i * package->encode_expectations_elem_size; EXPECTED *expected diff --git a/test/bioprinttest.c b/test/bioprinttest.c index 04d1613c6cf..95da0bd9bba 100644 --- a/test/bioprinttest.c +++ b/test/bioprinttest.c @@ -12,12 +12,11 @@ #include #include #include +#include "internal/nelem.h" #include "internal/numbers.h" #include "testutil.h" #include "testutil/output.h" -#define nelem(x) (int)(sizeof(x) / sizeof((x)[0])) - static int justprint = 0; static char *fpexpected[][10][5] = { @@ -192,7 +191,7 @@ static int dofptest(int test, int sub, double val, const char *width, int prec) char format[80], result[80]; int ret = 1, i; - for (i = 0; i < nelem(fspecs); i++) { + for (i = 0; i < (int)OSSL_NELEM(fspecs); i++) { const char *fspec = fspecs[i]; if (prec >= 0) @@ -287,9 +286,9 @@ int setup_tests(void) } ADD_TEST(test_big); - ADD_ALL_TESTS(test_fp, nelem(pw_params)); - ADD_ALL_TESTS(test_zu, nelem(zu_data)); - ADD_ALL_TESTS(test_j, nelem(jf_data)); + ADD_ALL_TESTS(test_fp, OSSL_NELEM(pw_params)); + ADD_ALL_TESTS(test_zu, OSSL_NELEM(zu_data)); + ADD_ALL_TESTS(test_j, OSSL_NELEM(jf_data)); return 1; } diff --git a/test/sslapitest.c b/test/sslapitest.c index 4cd469174d6..fdb5ef49f8a 100644 --- a/test/sslapitest.c +++ b/test/sslapitest.c @@ -1478,8 +1478,7 @@ static struct ktls_test_cipher { # endif }; -#define NUM_KTLS_TEST_CIPHERS \ - (sizeof(ktls_test_ciphers) / sizeof(ktls_test_ciphers[0])) +#define NUM_KTLS_TEST_CIPHERS OSSL_NELEM(ktls_test_ciphers) static int test_ktls(int test) {