Adding ARRAY_SIZE macro for getting the size of constant arrays.
Change-Id: Ie60744761f5aa434a71a998f5ca98a8f8b1c25d5
Reviewed-on: https://boringssl-review.googlesource.com/10447
Reviewed-by: David Benjamin <davidben@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
diff --git a/crypto/base64/base64_test.cc b/crypto/base64/base64_test.cc
index a608773..32b44f6 100644
--- a/crypto/base64/base64_test.cc
+++ b/crypto/base64/base64_test.cc
@@ -22,6 +22,8 @@
#include <openssl/crypto.h>
#include <openssl/err.h>
+#include "../internal.h"
+
enum encoding_relation {
// canonical indicates that the encoding is the expected encoding of the
@@ -98,7 +100,7 @@
"=======\n"},
};
-static const size_t kNumTests = sizeof(kTestVectors) / sizeof(kTestVectors[0]);
+static const size_t kNumTests = OPENSSL_ARRAY_SIZE(kTestVectors);
// RemoveNewlines returns a copy of |in| with all '\n' characters removed.
static std::string RemoveNewlines(const char *in) {
diff --git a/crypto/bio/bio_test.cc b/crypto/bio/bio_test.cc
index f2eb20b..d7be884 100644
--- a/crypto/bio/bio_test.cc
+++ b/crypto/bio/bio_test.cc
@@ -40,6 +40,7 @@
#include <algorithm>
+#include "../internal.h"
#include "../test/scoped_types.h"
@@ -206,9 +207,8 @@
// Transfer bytes from bio1_application_send_buffer to
// bio2_application_recv_buffer in various ways.
- for (size_t i = 0; i < sizeof(kLengths) / sizeof(kLengths[0]); i++) {
- for (size_t j = 0; j < sizeof(kPartialLengths) / sizeof(kPartialLengths[0]);
- j++) {
+ for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kLengths); i++) {
+ for (size_t j = 0; j < OPENSSL_ARRAY_SIZE(kPartialLengths); j++) {
size_t total_write = 0;
size_t total_read = 0;
@@ -293,7 +293,7 @@
return false;
}
- for (size_t i = 0; i < sizeof(kLengths) / sizeof(kLengths[0]); i++) {
+ for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kLengths); i++) {
char string[1024];
if (kLengths[i] >= sizeof(string)) {
fprintf(stderr, "Bad test string length\n");
diff --git a/crypto/bn/bn_test.cc b/crypto/bn/bn_test.cc
index f55dee0..b38aa7f 100644
--- a/crypto/bn/bn_test.cc
+++ b/crypto/bn/bn_test.cc
@@ -85,9 +85,10 @@
#include <openssl/err.h>
#include <openssl/mem.h>
-#include "../crypto/test/file_test.h"
-#include "../crypto/test/scoped_types.h"
-#include "../crypto/test/test_util.h"
+#include "../internal.h"
+#include "../test/file_test.h"
+#include "../test/scoped_types.h"
+#include "../test/test_util.h"
static int HexToBIGNUM(ScopedBIGNUM *out, const char *in) {
@@ -867,7 +868,7 @@
static bool TestMPI() {
uint8_t scratch[8];
- for (size_t i = 0; i < sizeof(kMPITests) / sizeof(kMPITests[0]); i++) {
+ for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kMPITests); i++) {
const MPITest &test = kMPITests[i];
ScopedBIGNUM bn(ASCIIToBIGNUM(test.base10));
const size_t mpi_len = BN_bn2mpi(bn.get(), NULL);
diff --git a/crypto/bytestring/bytestring_test.cc b/crypto/bytestring/bytestring_test.cc
index 0ec7d54..f567fd0 100644
--- a/crypto/bytestring/bytestring_test.cc
+++ b/crypto/bytestring/bytestring_test.cc
@@ -26,6 +26,7 @@
#include <openssl/crypto.h>
#include "internal.h"
+#include "../internal.h"
#include "../test/scoped_types.h"
namespace bssl {
@@ -730,8 +731,7 @@
};
static bool TestASN1Uint64() {
- for (size_t i = 0; i < sizeof(kASN1Uint64Tests) / sizeof(kASN1Uint64Tests[0]);
- i++) {
+ for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kASN1Uint64Tests); i++) {
const ASN1Uint64Test *test = &kASN1Uint64Tests[i];
CBS cbs;
uint64_t value;
@@ -760,9 +760,7 @@
}
}
- for (size_t i = 0;
- i < sizeof(kASN1InvalidUint64Tests) / sizeof(kASN1InvalidUint64Tests[0]);
- i++) {
+ for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kASN1InvalidUint64Tests); i++) {
const ASN1InvalidUint64Test *test = &kASN1InvalidUint64Tests[i];
CBS cbs;
uint64_t value;
diff --git a/crypto/cipher/e_tls.c b/crypto/cipher/e_tls.c
index b562a53..d44234d 100644
--- a/crypto/cipher/e_tls.c
+++ b/crypto/cipher/e_tls.c
@@ -25,7 +25,7 @@
#include <openssl/sha.h>
#include <openssl/type_check.h>
-#include "../crypto/internal.h"
+#include "../internal.h"
#include "internal.h"
diff --git a/crypto/digest/digest_test.cc b/crypto/digest/digest_test.cc
index c94096b..585b62a 100644
--- a/crypto/digest/digest_test.cc
+++ b/crypto/digest/digest_test.cc
@@ -25,6 +25,9 @@
#include <openssl/md5.h>
#include <openssl/sha.h>
+#include "../internal.h"
+
+
namespace bssl {
struct MD {
@@ -247,7 +250,7 @@
static int Main() {
CRYPTO_library_init();
- for (size_t i = 0; i < sizeof(kTestVectors) / sizeof(kTestVectors[0]); i++) {
+ for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kTestVectors); i++) {
if (!TestDigest(&kTestVectors[i])) {
fprintf(stderr, "Test %d failed\n", (int)i);
return 1;
diff --git a/crypto/evp/evp_asn1.c b/crypto/evp/evp_asn1.c
index 3681d4f..2b24858 100644
--- a/crypto/evp/evp_asn1.c
+++ b/crypto/evp/evp_asn1.c
@@ -65,6 +65,7 @@
#include <openssl/rsa.h>
#include "internal.h"
+#include "../internal.h"
static const EVP_PKEY_ASN1_METHOD *const kASN1Methods[] = {
@@ -80,7 +81,7 @@
}
unsigned i;
- for (i = 0; i < sizeof(kASN1Methods)/sizeof(kASN1Methods[0]); i++) {
+ for (i = 0; i < OPENSSL_ARRAY_SIZE(kASN1Methods); i++) {
const EVP_PKEY_ASN1_METHOD *method = kASN1Methods[i];
if (CBS_len(&oid) == method->oid_len &&
memcmp(CBS_data(&oid), method->oid, method->oid_len) == 0) {
diff --git a/crypto/evp/print.c b/crypto/evp/print.c
index 56521ec..53527b4 100644
--- a/crypto/evp/print.c
+++ b/crypto/evp/print.c
@@ -60,6 +60,7 @@
#include <openssl/mem.h>
#include <openssl/rsa.h>
+#include "../internal.h"
#include "../rsa/internal.h"
@@ -479,8 +480,7 @@
},
};
-static size_t kPrintMethodsLen =
- sizeof(kPrintMethods) / sizeof(kPrintMethods[0]);
+static size_t kPrintMethodsLen = OPENSSL_ARRAY_SIZE(kPrintMethods);
static EVP_PKEY_PRINT_METHOD *find_method(int type) {
size_t i;
diff --git a/crypto/hkdf/hkdf_test.c b/crypto/hkdf/hkdf_test.c
index a0f75a9..4499cc0 100644
--- a/crypto/hkdf/hkdf_test.c
+++ b/crypto/hkdf/hkdf_test.c
@@ -20,6 +20,7 @@
#include <openssl/err.h>
#include <openssl/hkdf.h>
+#include "../internal.h"
#include "../test/test_util.h"
@@ -252,7 +253,7 @@
CRYPTO_library_init();
- for (i = 0; i < sizeof(kTests) / sizeof(kTests[0]); i++) {
+ for (i = 0; i < OPENSSL_ARRAY_SIZE(kTests); i++) {
const hkdf_test_vector_t *test = &kTests[i];
if (!HKDF_extract(prk, &prk_len, test->md_func(), test->ikm, test->ikm_len,
test->salt, test->salt_len)) {
diff --git a/crypto/internal.h b/crypto/internal.h
index 05fa569..d6e341a 100644
--- a/crypto/internal.h
+++ b/crypto/internal.h
@@ -147,6 +147,7 @@
typedef __uint128_t uint128_t;
#endif
+#define OPENSSL_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
/* buffers_alias returns one if |a| and |b| alias and zero otherwise. */
static inline int buffers_alias(const uint8_t *a, size_t a_len,
diff --git a/crypto/pkcs8/pkcs8.c b/crypto/pkcs8/pkcs8.c
index 4ecf17f..2363aa8 100644
--- a/crypto/pkcs8/pkcs8.c
+++ b/crypto/pkcs8/pkcs8.c
@@ -71,6 +71,7 @@
#include <openssl/x509.h>
#include "internal.h"
+#include "../internal.h"
#include "../bytestring/internal.h"
@@ -310,7 +311,7 @@
static const struct pbe_suite *get_pbe_suite(int pbe_nid) {
unsigned i;
- for (i = 0; i < sizeof(kBuiltinPBE) / sizeof(kBuiltinPBE[0]); i++) {
+ for (i = 0; i < OPENSSL_ARRAY_SIZE(kBuiltinPBE); i++) {
if (kBuiltinPBE[i].pbe_nid == pbe_nid) {
return &kBuiltinPBE[i];
}
diff --git a/ssl/internal.h b/ssl/internal.h
index 6e3a744..092d14b 100644
--- a/ssl/internal.h
+++ b/ssl/internal.h
@@ -148,6 +148,7 @@
#include <openssl/ssl.h>
#include <openssl/stack.h>
+
#if defined(OPENSSL_WINDOWS)
/* Windows defines struct timeval in winsock2.h. */
OPENSSL_MSVC_PRAGMA(warning(push, 3))
diff --git a/ssl/ssl_cipher.c b/ssl/ssl_cipher.c
index 079c823..52369a4 100644
--- a/ssl/ssl_cipher.c
+++ b/ssl/ssl_cipher.c
@@ -151,6 +151,7 @@
#include <openssl/stack.h>
#include "internal.h"
+#include "../crypto/internal.h"
/* kCiphers is an array of all supported ciphers, sorted by id. */
@@ -686,7 +687,7 @@
};
-static const size_t kCiphersLen = sizeof(kCiphers) / sizeof(kCiphers[0]);
+static const size_t kCiphersLen = OPENSSL_ARRAY_SIZE(kCiphers);
#define CIPHER_ADD 1
#define CIPHER_KILL 2
@@ -786,8 +787,7 @@
{"FIPS", ~SSL_kCECPQ1, ~0u, ~(SSL_eNULL|SSL_RC4), ~0u, 0},
};
-static const size_t kCipherAliasesLen =
- sizeof(kCipherAliases) / sizeof(kCipherAliases[0]);
+static const size_t kCipherAliasesLen = OPENSSL_ARRAY_SIZE(kCipherAliases);
static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
const SSL_CIPHER *a = in_a;
diff --git a/ssl/ssl_ecdh.c b/ssl/ssl_ecdh.c
index 47b6eab..079ddb5 100644
--- a/ssl/ssl_ecdh.c
+++ b/ssl/ssl_ecdh.c
@@ -27,6 +27,7 @@
#include <openssl/nid.h>
#include "internal.h"
+#include "../crypto/internal.h"
/* |EC_POINT| implementation. */
@@ -504,7 +505,7 @@
static const SSL_ECDH_METHOD *method_from_group_id(uint16_t group_id) {
size_t i;
- for (i = 0; i < sizeof(kMethods) / sizeof(kMethods[0]); i++) {
+ for (i = 0; i < OPENSSL_ARRAY_SIZE(kMethods); i++) {
if (kMethods[i].group_id == group_id) {
return &kMethods[i];
}
@@ -514,7 +515,7 @@
static const SSL_ECDH_METHOD *method_from_nid(int nid) {
size_t i;
- for (i = 0; i < sizeof(kMethods) / sizeof(kMethods[0]); i++) {
+ for (i = 0; i < OPENSSL_ARRAY_SIZE(kMethods); i++) {
if (kMethods[i].nid == nid) {
return &kMethods[i];
}
diff --git a/ssl/ssl_lib.c b/ssl/ssl_lib.c
index 9be76be..52701f4 100644
--- a/ssl/ssl_lib.c
+++ b/ssl/ssl_lib.c
@@ -2667,7 +2667,7 @@
{TLS1_3_VERSION, SSL_OP_NO_TLSv1_3},
};
-static const size_t kVersionsLen = sizeof(kVersions) / sizeof(kVersions[0]);
+static const size_t kVersionsLen = OPENSSL_ARRAY_SIZE(kVersions);
int ssl_get_full_version_range(const SSL *ssl, uint16_t *out_min_version,
uint16_t *out_fallback_version,
diff --git a/ssl/ssl_test.cc b/ssl/ssl_test.cc
index f73c37d..2493773 100644
--- a/ssl/ssl_test.cc
+++ b/ssl/ssl_test.cc
@@ -32,6 +32,7 @@
#include "internal.h"
#include "test/scoped_types.h"
+#include "../crypto/internal.h"
#include "../crypto/test/test_util.h"
#if defined(OPENSSL_WINDOWS)
@@ -763,7 +764,7 @@
static bool TestCipherGetRFCName(void) {
for (size_t i = 0;
- i < sizeof(kCipherRFCNameTests) / sizeof(kCipherRFCNameTests[0]); i++) {
+ i < OPENSSL_ARRAY_SIZE(kCipherRFCNameTests); i++) {
const CIPHER_RFC_NAME_TEST *test = &kCipherRFCNameTests[i];
std::string rfc_name;
if (!CipherGetRFCName(&rfc_name, test->id & 0xffff)) {
diff --git a/ssl/t1_lib.c b/ssl/t1_lib.c
index a9a8fa3..496c9c4 100644
--- a/ssl/t1_lib.c
+++ b/ssl/t1_lib.c
@@ -124,6 +124,7 @@
#include <openssl/type_check.h>
#include "internal.h"
+#include "../crypto/internal.h"
static int ssl_check_clienthello_tlsext(SSL *ssl);
@@ -327,7 +328,7 @@
*out_group_ids_len = ssl->supported_group_list_len;
if (!*out_group_ids) {
*out_group_ids = kDefaultGroups;
- *out_group_ids_len = sizeof(kDefaultGroups) / sizeof(kDefaultGroups[0]);
+ *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
}
}
@@ -559,13 +560,11 @@
* https://bugzilla.mozilla.org/show_bug.cgi?id=1119983 */
if (max_version >= TLS1_3_VERSION) {
*psigs = kDefaultTLS13SignatureAlgorithms;
- return sizeof(kDefaultTLS13SignatureAlgorithms) /
- sizeof(kDefaultTLS13SignatureAlgorithms[0]);
+ return OPENSSL_ARRAY_SIZE(kDefaultTLS13SignatureAlgorithms);
}
*psigs = kDefaultSignatureAlgorithms;
- return sizeof(kDefaultSignatureAlgorithms) /
- sizeof(kDefaultSignatureAlgorithms[0]);
+ return OPENSSL_ARRAY_SIZE(kDefaultSignatureAlgorithms);
}
int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
@@ -3076,8 +3075,7 @@
static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
SSL_SIGN_ECDSA_SHA1};
peer_sigalgs = kDefaultPeerAlgorithms;
- peer_sigalgs_len =
- sizeof(kDefaultPeerAlgorithms) / sizeof(kDefaultPeerAlgorithms[0]);
+ peer_sigalgs_len = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
}
for (i = 0; i < sigalgs_len; i++) {
diff --git a/ssl/test/bssl_shim.cc b/ssl/test/bssl_shim.cc
index 2a9ba88..f9ab913 100644
--- a/ssl/test/bssl_shim.cc
+++ b/ssl/test/bssl_shim.cc
@@ -56,6 +56,7 @@
#include <string>
#include <vector>
+#include "../../crypto/internal.h"
#include "../../crypto/test/scoped_types.h"
#include "async_bio.h"
#include "packeted_bio.h"
@@ -1396,7 +1397,7 @@
NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1, NID_X25519,
};
if (!SSL_set1_curves(ssl.get(), kAllCurves,
- sizeof(kAllCurves) / sizeof(kAllCurves[0]))) {
+ OPENSSL_ARRAY_SIZE(kAllCurves))) {
return false;
}
}
@@ -1535,8 +1536,7 @@
memset(buf.get(), 0x42, kBufLen);
static const size_t kRecordSizes[] = {
0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
- for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
- i++) {
+ for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kRecordSizes); i++) {
const size_t len = kRecordSizes[i];
if (len > kBufLen) {
fprintf(stderr, "Bad kRecordSizes value.\n");