]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Prefer ARRAY_SIZE(...)
authorDimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com>
Wed, 13 Sep 2023 20:31:50 +0000 (22:31 +0200)
committerNeil Horman <nhorman@openssl.org>
Mon, 22 Jul 2024 10:55:35 +0000 (06:55 -0400)
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 <nhorman@openssl.org>
Reviewed-by: David von Oheimb <david.von.oheimb@siemens.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/22097)

crypto/bn/bn_mod.c
crypto/sha/keccak1600.c
crypto/store/store_strings.c
engines/e_afalg.c
test/asn1_encode_test.c
test/bioprinttest.c
test/sslapitest.c

index d7c2f4bd5bfa9e54494d3b068ffa771e38002c09..4cb64b453b81fbdfbfb0cd9399f36b7c86b72a1c 100644 (file)
@@ -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;
index 6682367be19b7ba3fa0691fd22e8a74f34bce3e4..2ae1c9be9b78e7d95e1c1f8f804b7f7f24fb99b1 100644 (file)
@@ -11,6 +11,8 @@
 #include <string.h>
 #include <assert.h>
 
+#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];
index 3d4a8ea730795dbc121f990c01bd7fe1fa1e2260..f9fbd61806001c09ccfe4177eb783df191ce693c 100644 (file)
@@ -9,6 +9,8 @@
 
 #include <openssl/store.h>
 
+#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;
index 3644524e9fc8bf2105c13167b153acec9e16a575..413480d725f0fff2e4fce84889c7c70d292996d8 100644 (file)
@@ -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) {
index 335f24e1133fb70eddc6ee99276281bd8578db6b..834b2cdd6428db99b704f5713a85589da0df1005 100644 (file)
@@ -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
index 04d1613c6cf499a701497c1eea9fb79f94d7381f..95da0bd9bbaa443d8c614a10ee1f20132f02ffca 100644 (file)
 #include <stdio.h>
 #include <string.h>
 #include <openssl/bio.h>
+#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;
 }
 
index 4cd469174d6e614f198dcade8da6a2d2428e1524..fdb5ef49f8aa7a3b779bb1f13929cc0874ca36a7 100644 (file)
@@ -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)
 {