From: Dr. Stephen Henson Date: Sat, 2 May 2015 17:30:00 +0000 (+0100) Subject: Add OSSL_NELEM macro. X-Git-Tag: OpenSSL_1_1_0-pre1~1213 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=b6eb9827a6866981c08cc9613ca8b4a648894fb1;p=oweals%2Fopenssl.git Add OSSL_NELEM macro. Add OSSL_NELEM macro to e_os.h to determine the number of elements in an array. Reviewed-by: Tim Hudson --- diff --git a/crypto/asn1/a_strnid.c b/crypto/asn1/a_strnid.c index 213cf63e59..5126298673 100644 --- a/crypto/asn1/a_strnid.c +++ b/crypto/asn1/a_strnid.c @@ -220,9 +220,7 @@ ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid) if (idx >= 0) return sk_ASN1_STRING_TABLE_value(stable, idx); } - return OBJ_bsearch_table(&fnd, tbl_standard, - sizeof(tbl_standard) / - sizeof(ASN1_STRING_TABLE)); + return OBJ_bsearch_table(&fnd, tbl_standard, OSSL_NELEM(tbl_standard)); } /* @@ -309,8 +307,7 @@ main() ASN1_STRING_TABLE *tmp; int i, last_nid = -1; - for (tmp = tbl_standard, i = 0; - i < sizeof(tbl_standard) / sizeof(ASN1_STRING_TABLE); i++, tmp++) { + for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++) { if (tmp->nid < last_nid) { last_nid = 0; break; @@ -323,8 +320,7 @@ main() exit(0); } - for (tmp = tbl_standard, i = 0; - i < sizeof(tbl_standard) / sizeof(ASN1_STRING_TABLE); i++, tmp++) + for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++) printf("Index %d, NID %d, Name=%s\n", i, tmp->nid, OBJ_nid2ln(tmp->nid)); diff --git a/crypto/asn1/ameth_lib.c b/crypto/asn1/ameth_lib.c index 718aa521c5..49f4e5abbf 100644 --- a/crypto/asn1/ameth_lib.c +++ b/crypto/asn1/ameth_lib.c @@ -107,8 +107,7 @@ static STACK_OF(EVP_PKEY_ASN1_METHOD) *app_methods = NULL; void main() { int i; - for (i = 0; - i < sizeof(standard_methods) / sizeof(EVP_PKEY_ASN1_METHOD *); i++) + for (i = 0; i < OSSL_NELEM(standard_methods); i++) fprintf(stderr, "Number %d id=%d (%s)\n", i, standard_methods[i]->pkey_id, OBJ_nid2sn(standard_methods[i]->pkey_id)); @@ -129,7 +128,7 @@ IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *, int EVP_PKEY_asn1_get_count(void) { - int num = sizeof(standard_methods) / sizeof(EVP_PKEY_ASN1_METHOD *); + int num = OSSL_NELEM(standard_methods); if (app_methods) num += sk_EVP_PKEY_ASN1_METHOD_num(app_methods); return num; @@ -137,7 +136,7 @@ int EVP_PKEY_asn1_get_count(void) const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_get0(int idx) { - int num = sizeof(standard_methods) / sizeof(EVP_PKEY_ASN1_METHOD *); + int num = OSSL_NELEM(standard_methods); if (idx < 0) return NULL; if (idx < num) @@ -157,8 +156,7 @@ static const EVP_PKEY_ASN1_METHOD *pkey_asn1_find(int type) if (idx >= 0) return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx); } - ret = OBJ_bsearch_ameth(&t, standard_methods, sizeof(standard_methods) - / sizeof(EVP_PKEY_ASN1_METHOD *)); + ret = OBJ_bsearch_ameth(&t, standard_methods, OSSL_NELEM(standard_methods)); if (!ret || !*ret) return NULL; return *ret; diff --git a/crypto/asn1/asn1_gen.c b/crypto/asn1/asn1_gen.c index 549bc5ef36..049515d1cf 100644 --- a/crypto/asn1/asn1_gen.c +++ b/crypto/asn1/asn1_gen.c @@ -620,7 +620,7 @@ static int asn1_str2tag(const char *tagstr, int len) len = strlen(tagstr); tntmp = tnst; - for (i = 0; i < sizeof(tnst) / sizeof(struct tag_name_st); i++, tntmp++) { + for (i = 0; i < OSSL_NELEM(tnst); i++, tntmp++) { if ((len == tntmp->len) && !strncmp(tntmp->strnam, tagstr, len)) return tntmp->tag; } diff --git a/crypto/bio/bss_file.c b/crypto/bio/bss_file.c index 0776383448..1da6b86181 100644 --- a/crypto/bio/bss_file.c +++ b/crypto/bio/bss_file.c @@ -148,7 +148,7 @@ BIO *BIO_new_file(const char *filename, const char *mode) if (MultiByteToWideChar(CP_UTF8, flags, filename, len_0, wfilename, sz) && MultiByteToWideChar(CP_UTF8, 0, mode, strlen(mode) + 1, - wmode, sizeof(wmode) / sizeof(wmode[0])) && + wmode, OSSL_NELEM(wmode)) && (file = _wfopen(wfilename, wmode)) == NULL && (errno == ENOENT || errno == EBADF) ) { diff --git a/crypto/bn/bn_dh.c b/crypto/bn/bn_dh.c index 1c007bb901..cfd8c067a8 100644 --- a/crypto/bn/bn_dh.c +++ b/crypto/bn/bn_dh.c @@ -57,6 +57,7 @@ */ #include "bn_lcl.h" +#include "e_os.h" #ifndef OPENSSL_NO_DH /* DH parameters from RFC5114 */ @@ -247,8 +248,8 @@ static const BN_ULONG dh2048_256_q[] = { /* Macro to make a BIGNUM from static data */ # define make_dh_bn(x) const BIGNUM _bignum_##x = { (BN_ULONG *) x, \ - sizeof(x)/sizeof(BN_ULONG),\ - sizeof(x)/sizeof(BN_ULONG),\ + OSSL_NELEM(x),\ + OSSL_NELEM(x),\ 0, BN_FLG_STATIC_DATA }; diff --git a/crypto/bn/bn_gf2m.c b/crypto/bn/bn_gf2m.c index 7c0d4afc71..c87c5d96f8 100644 --- a/crypto/bn/bn_gf2m.c +++ b/crypto/bn/bn_gf2m.c @@ -473,8 +473,8 @@ int BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p) int arr[6]; bn_check_top(a); bn_check_top(p); - ret = BN_GF2m_poly2arr(p, arr, sizeof(arr) / sizeof(arr[0])); - if (!ret || ret > (int)(sizeof(arr) / sizeof(arr[0]))) { + ret = BN_GF2m_poly2arr(p, arr, OSSL_NELEM(arr)); + if (!ret || ret > (int)OSSL_NELEM(arr)) { BNerr(BN_F_BN_GF2M_MOD, BN_R_INVALID_LENGTH); return 0; } diff --git a/crypto/bn/bn_nist.c b/crypto/bn/bn_nist.c index 2c5596d173..af048d3a3b 100644 --- a/crypto/bn/bn_nist.c +++ b/crypto/bn/bn_nist.c @@ -379,8 +379,8 @@ int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, PTR_SIZE_INT mask; static const BIGNUM _bignum_nist_p_192_sqr = { (BN_ULONG *)_nist_p_192_sqr, - sizeof(_nist_p_192_sqr) / sizeof(_nist_p_192_sqr[0]), - sizeof(_nist_p_192_sqr) / sizeof(_nist_p_192_sqr[0]), + OSSL_NELEM(_nist_p_192_sqr), + OSSL_NELEM(_nist_p_192_sqr), 0, BN_FLG_STATIC_DATA }; @@ -524,8 +524,8 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, } u; static const BIGNUM _bignum_nist_p_224_sqr = { (BN_ULONG *)_nist_p_224_sqr, - sizeof(_nist_p_224_sqr) / sizeof(_nist_p_224_sqr[0]), - sizeof(_nist_p_224_sqr) / sizeof(_nist_p_224_sqr[0]), + OSSL_NELEM(_nist_p_224_sqr), + OSSL_NELEM(_nist_p_224_sqr), 0, BN_FLG_STATIC_DATA }; @@ -705,8 +705,8 @@ int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, } u; static const BIGNUM _bignum_nist_p_256_sqr = { (BN_ULONG *)_nist_p_256_sqr, - sizeof(_nist_p_256_sqr) / sizeof(_nist_p_256_sqr[0]), - sizeof(_nist_p_256_sqr) / sizeof(_nist_p_256_sqr[0]), + OSSL_NELEM(_nist_p_256_sqr), + OSSL_NELEM(_nist_p_256_sqr), 0, BN_FLG_STATIC_DATA }; @@ -951,8 +951,8 @@ int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, } u; static const BIGNUM _bignum_nist_p_384_sqr = { (BN_ULONG *)_nist_p_384_sqr, - sizeof(_nist_p_384_sqr) / sizeof(_nist_p_384_sqr[0]), - sizeof(_nist_p_384_sqr) / sizeof(_nist_p_384_sqr[0]), + OSSL_NELEM(_nist_p_384_sqr), + OSSL_NELEM(_nist_p_384_sqr), 0, BN_FLG_STATIC_DATA }; @@ -1209,8 +1209,8 @@ int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, PTR_SIZE_INT mask; static const BIGNUM _bignum_nist_p_521_sqr = { (BN_ULONG *)_nist_p_521_sqr, - sizeof(_nist_p_521_sqr) / sizeof(_nist_p_521_sqr[0]), - sizeof(_nist_p_521_sqr) / sizeof(_nist_p_521_sqr[0]), + OSSL_NELEM(_nist_p_521_sqr), + OSSL_NELEM(_nist_p_521_sqr), 0, BN_FLG_STATIC_DATA }; diff --git a/crypto/bn/bn_srp.c b/crypto/bn/bn_srp.c index 0a39f2f7c1..5dd31fc7bf 100644 --- a/crypto/bn/bn_srp.c +++ b/crypto/bn/bn_srp.c @@ -1,4 +1,5 @@ #include "bn_lcl.h" +#include "e_os.h" #ifndef OPENSSL_NO_SRP @@ -37,8 +38,8 @@ static const BN_ULONG bn_group_1024_value[] = { const BIGNUM bn_group_1024 = { (BN_ULONG *)bn_group_1024_value, - (sizeof bn_group_1024_value) / sizeof(BN_ULONG), - (sizeof bn_group_1024_value) / sizeof(BN_ULONG), + OSSL_NELEM(bn_group_1024_value), + OSSL_NELEM(bn_group_1024_value), 0, BN_FLG_STATIC_DATA }; @@ -72,8 +73,8 @@ static const BN_ULONG bn_group_1536_value[] = { const BIGNUM bn_group_1536 = { (BN_ULONG *)bn_group_1536_value, - (sizeof bn_group_1536_value) / sizeof(BN_ULONG), - (sizeof bn_group_1536_value) / sizeof(BN_ULONG), + OSSL_NELEM(bn_group_1536_value), + OSSL_NELEM(bn_group_1536_value), 0, BN_FLG_STATIC_DATA }; @@ -115,8 +116,8 @@ static const BN_ULONG bn_group_2048_value[] = { const BIGNUM bn_group_2048 = { (BN_ULONG *)bn_group_2048_value, - (sizeof bn_group_2048_value) / sizeof(BN_ULONG), - (sizeof bn_group_2048_value) / sizeof(BN_ULONG), + OSSL_NELEM(bn_group_2048_value), + OSSL_NELEM(bn_group_2048_value), 0, BN_FLG_STATIC_DATA }; @@ -174,8 +175,8 @@ static const BN_ULONG bn_group_3072_value[] = { const BIGNUM bn_group_3072 = { (BN_ULONG *)bn_group_3072_value, - (sizeof bn_group_3072_value) / sizeof(BN_ULONG), - (sizeof bn_group_3072_value) / sizeof(BN_ULONG), + OSSL_NELEM(bn_group_3072_value), + OSSL_NELEM(bn_group_3072_value), 0, BN_FLG_STATIC_DATA }; @@ -249,8 +250,8 @@ static const BN_ULONG bn_group_4096_value[] = { const BIGNUM bn_group_4096 = { (BN_ULONG *)bn_group_4096_value, - (sizeof bn_group_4096_value) / sizeof(BN_ULONG), - (sizeof bn_group_4096_value) / sizeof(BN_ULONG), + OSSL_NELEM(bn_group_4096_value), + OSSL_NELEM(bn_group_4096_value), 0, BN_FLG_STATIC_DATA }; @@ -356,8 +357,8 @@ static const BN_ULONG bn_group_6144_value[] = { const BIGNUM bn_group_6144 = { (BN_ULONG *)bn_group_6144_value, - (sizeof bn_group_6144_value) / sizeof(BN_ULONG), - (sizeof bn_group_6144_value) / sizeof(BN_ULONG), + OSSL_NELEM(bn_group_6144_value), + OSSL_NELEM(bn_group_6144_value), 0, BN_FLG_STATIC_DATA }; @@ -495,8 +496,8 @@ static const BN_ULONG bn_group_8192_value[] = { const BIGNUM bn_group_8192 = { (BN_ULONG *)bn_group_8192_value, - (sizeof bn_group_8192_value) / sizeof(BN_ULONG), - (sizeof bn_group_8192_value) / sizeof(BN_ULONG), + OSSL_NELEM(bn_group_8192_value), + OSSL_NELEM(bn_group_8192_value), 0, BN_FLG_STATIC_DATA }; diff --git a/crypto/ec/ec_curve.c b/crypto/ec/ec_curve.c index 8bba3bcb83..d0f1fcb2fd 100644 --- a/crypto/ec/ec_curve.c +++ b/crypto/ec/ec_curve.c @@ -74,6 +74,7 @@ #include #include #include +#include "e_os.h" typedef struct { int field_type, /* either NID_X9_62_prime_field or @@ -3022,7 +3023,7 @@ static const ec_list_element curve_list[] = { "RFC 5639 curve over a 512 bit prime field"}, }; -#define curve_list_length (sizeof(curve_list)/sizeof(ec_list_element)) +#define curve_list_length OSSL_NELEM(curve_list) static EC_GROUP *ec_group_new_from_data(const ec_list_element curve) { @@ -3194,7 +3195,7 @@ static EC_NIST_NAME nist_curves[] = { const char *EC_curve_nid2nist(int nid) { size_t i; - for (i = 0; i < sizeof(nist_curves) / sizeof(EC_NIST_NAME); i++) { + for (i = 0; i < OSSL_NELEM(nist_curves); i++) { if (nist_curves[i].nid == nid) return nist_curves[i].name; } @@ -3204,7 +3205,7 @@ const char *EC_curve_nid2nist(int nid) int EC_curve_nist2nid(const char *name) { size_t i; - for (i = 0; i < sizeof(nist_curves) / sizeof(EC_NIST_NAME); i++) { + for (i = 0; i < OSSL_NELEM(nist_curves); i++) { if (!strcmp(nist_curves[i].name, name)) return nist_curves[i].nid; } diff --git a/crypto/evp/evp_pbe.c b/crypto/evp/evp_pbe.c index 7a716372c1..6128a73620 100644 --- a/crypto/evp/evp_pbe.c +++ b/crypto/evp/evp_pbe.c @@ -130,7 +130,7 @@ int main(int argc, char **argv) * OpenSSL_add_all_algorithms(); */ - for (i = 0; i < sizeof(builtin_pbe) / sizeof(EVP_PBE_CTL); i++) { + for (i = 0; i < OSSL_NELEM(builtin_pbe); i++) { tpbe = builtin_pbe + i; fprintf(stderr, "%d %d %s ", tpbe->pbe_type, tpbe->pbe_nid, OBJ_nid2sn(tpbe->pbe_nid)); @@ -276,8 +276,7 @@ int EVP_PBE_find(int type, int pbe_nid, pbetmp = sk_EVP_PBE_CTL_value(pbe_algs, i); } if (pbetmp == NULL) { - pbetmp = OBJ_bsearch_pbe2(&pbelu, builtin_pbe, - sizeof(builtin_pbe) / sizeof(EVP_PBE_CTL)); + pbetmp = OBJ_bsearch_pbe2(&pbelu, builtin_pbe, OSSL_NELEM(builtin_pbe)); } if (pbetmp == NULL) return 0; diff --git a/crypto/objects/obj_xref.c b/crypto/objects/obj_xref.c index b752a2c5be..10ce6397d6 100644 --- a/crypto/objects/obj_xref.c +++ b/crypto/objects/obj_xref.c @@ -59,6 +59,7 @@ #include #include "obj_xref.h" +#include "e_os.h" DECLARE_STACK_OF(nid_triple) STACK_OF(nid_triple) *sig_app, *sigx_app; @@ -102,8 +103,7 @@ int OBJ_find_sigid_algs(int signid, int *pdig_nid, int *ppkey_nid) } #ifndef OBJ_XREF_TEST2 if (rv == NULL) { - rv = OBJ_bsearch_sig(&tmp, sigoid_srt, - sizeof(sigoid_srt) / sizeof(nid_triple)); + rv = OBJ_bsearch_sig(&tmp, sigoid_srt, OSSL_NELEM(sigoid_srt)); } #endif if (rv == NULL) @@ -133,9 +133,7 @@ int OBJ_find_sigid_by_algs(int *psignid, int dig_nid, int pkey_nid) } #ifndef OBJ_XREF_TEST2 if (rv == NULL) { - rv = OBJ_bsearch_sigx(&t, sigoid_srt_xref, - sizeof(sigoid_srt_xref) / sizeof(nid_triple *) - ); + rv = OBJ_bsearch_sigx(&t, sigoid_srt_xref, OSSL_NELEM(sigoid_srt_xref)); } #endif if (rv == NULL) @@ -198,12 +196,12 @@ main() int i, rv; # ifdef OBJ_XREF_TEST2 - for (i = 0; i < sizeof(sigoid_srt) / sizeof(nid_triple); i++) { + for (i = 0; i < OSSL_NELEM(sigoid_srt); i++) { OBJ_add_sigid(sigoid_srt[i][0], sigoid_srt[i][1], sigoid_srt[i][2]); } # endif - for (i = 0; i < sizeof(sigoid_srt) / sizeof(nid_triple); i++) { + for (i = 0; i < OSSL_NELEM(sigoid_srt); i++) { n1 = sigoid_srt[i][0]; rv = OBJ_find_sigid_algs(n1, &n2, &n3); printf("Forward: %d, %s %s %s\n", rv, diff --git a/crypto/rand/rand_unix.c b/crypto/rand/rand_unix.c index 732ba3b31b..4332270004 100644 --- a/crypto/rand/rand_unix.c +++ b/crypto/rand/rand_unix.c @@ -250,7 +250,7 @@ int RAND_poll(void) # endif # ifdef DEVRANDOM static const char *randomfiles[] = { DEVRANDOM }; - struct stat randomstats[sizeof(randomfiles) / sizeof(randomfiles[0])]; + struct stat randomstats[OSSL_NELEM(randomfiles)]; int fd; unsigned int i; # endif @@ -267,8 +267,7 @@ int RAND_poll(void) * out of random entries. */ - for (i = 0; (i < sizeof(randomfiles) / sizeof(randomfiles[0])) && - (n < ENTROPY_NEEDED); i++) { + for (i = 0; (i < OSSL_NELEM(randomfiles)) && (n < ENTROPY_NEEDED); i++) { if ((fd = open(randomfiles[i], O_RDONLY # ifdef O_NONBLOCK | O_NONBLOCK diff --git a/crypto/ts/ts_rsp_print.c b/crypto/ts/ts_rsp_print.c index d64f2f4e4a..0a3e4973c8 100644 --- a/crypto/ts/ts_rsp_print.c +++ b/crypto/ts/ts_rsp_print.c @@ -130,8 +130,7 @@ int TS_STATUS_INFO_print_bio(BIO *bio, TS_STATUS_INFO *a) /* Printing status code. */ BIO_printf(bio, "Status: "); status = ASN1_INTEGER_get(a->status); - if (0 <= status - && status < (long)(sizeof(status_map) / sizeof(status_map[0]))) + if (0 <= status && status < (long)OSSL_NELEM(status_map)) BIO_printf(bio, "%s\n", status_map[status]); else BIO_printf(bio, "out of bounds\n"); diff --git a/crypto/ts/ts_rsp_verify.c b/crypto/ts/ts_rsp_verify.c index a89f9cbdfa..8381d4148b 100644 --- a/crypto/ts/ts_rsp_verify.c +++ b/crypto/ts/ts_rsp_verify.c @@ -101,7 +101,7 @@ static const char *TS_status_text[] = { "granted", "revocationNotification" }; -#define TS_STATUS_TEXT_SIZE (sizeof(TS_status_text)/sizeof(*TS_status_text)) +#define TS_STATUS_TEXT_SIZE OSSL_NELEM(TS_status_text) /* * This must be greater or equal to the sum of the strings in TS_status_text diff --git a/crypto/x509/x509_trs.c b/crypto/x509/x509_trs.c index 92ea2b586f..6632f9b9de 100644 --- a/crypto/x509/x509_trs.c +++ b/crypto/x509/x509_trs.c @@ -94,7 +94,7 @@ static X509_TRUST trstandard[] = { {X509_TRUST_TSA, 0, trust_1oidany, "TSA server", NID_time_stamp, NULL} }; -#define X509_TRUST_COUNT (sizeof(trstandard)/sizeof(X509_TRUST)) +#define X509_TRUST_COUNT OSSL_NELEM(trstandard) static STACK_OF(X509_TRUST) *trtable = NULL; diff --git a/crypto/x509/x509_vpm.c b/crypto/x509/x509_vpm.c index 6305406756..b9a974136c 100644 --- a/crypto/x509/x509_vpm.c +++ b/crypto/x509/x509_vpm.c @@ -601,7 +601,7 @@ int X509_VERIFY_PARAM_add0_table(X509_VERIFY_PARAM *param) int X509_VERIFY_PARAM_get_count(void) { - int num = sizeof(default_table) / sizeof(X509_VERIFY_PARAM); + int num = OSSL_NELEM(default_table); if (param_table) num += sk_X509_VERIFY_PARAM_num(param_table); return num; @@ -609,7 +609,7 @@ int X509_VERIFY_PARAM_get_count(void) const X509_VERIFY_PARAM *X509_VERIFY_PARAM_get0(int id) { - int num = sizeof(default_table) / sizeof(X509_VERIFY_PARAM); + int num = OSSL_NELEM(default_table); if (id < num) return default_table + id; return sk_X509_VERIFY_PARAM_value(param_table, id - num); @@ -626,9 +626,7 @@ const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name) if (idx != -1) return sk_X509_VERIFY_PARAM_value(param_table, idx); } - return OBJ_bsearch_table(&pm, default_table, - sizeof(default_table) / - sizeof(X509_VERIFY_PARAM)); + return OBJ_bsearch_table(&pm, default_table, OSSL_NELEM(default_table)); } void X509_VERIFY_PARAM_table_cleanup(void) diff --git a/crypto/x509v3/tabtest.c b/crypto/x509v3/tabtest.c index 145dc9de56..65209db972 100644 --- a/crypto/x509v3/tabtest.c +++ b/crypto/x509v3/tabtest.c @@ -72,7 +72,7 @@ main() { int i, prev = -1, bad = 0; X509V3_EXT_METHOD **tmp; - i = sizeof(standard_exts) / sizeof(X509V3_EXT_METHOD *); + i = OSSL_NELEM(standard_exts); if (i != STANDARD_EXTENSION_COUNT) fprintf(stderr, "Extension number invalid expecting %d\n", i); tmp = standard_exts; diff --git a/crypto/x509v3/v3_purp.c b/crypto/x509v3/v3_purp.c index d51f93f3f7..5cee586990 100644 --- a/crypto/x509v3/v3_purp.c +++ b/crypto/x509v3/v3_purp.c @@ -108,7 +108,7 @@ static X509_PURPOSE xstandard[] = { NULL}, }; -#define X509_PURPOSE_COUNT (sizeof(xstandard)/sizeof(X509_PURPOSE)) +#define X509_PURPOSE_COUNT OSSL_NELEM(xstandard) static STACK_OF(X509_PURPOSE) *xptable = NULL; @@ -334,8 +334,7 @@ int X509_supported_extension(X509_EXTENSION *ex) if (ex_nid == NID_undef) return 0; - if (OBJ_bsearch_nid(&ex_nid, supported_nids, - sizeof(supported_nids) / sizeof(int))) + if (OBJ_bsearch_nid(&ex_nid, supported_nids, OSSL_NELEM(supported_nids))) return 1; return 0; } diff --git a/e_os.h b/e_os.h index f4a427af0a..7f94bfcae6 100644 --- a/e_os.h +++ b/e_os.h @@ -689,6 +689,8 @@ struct servent *getservbyname(const char *name, const char *proto); # endif # endif +#define OSSL_NELEM(x) (sizeof(x)/sizeof(x[0])) + #ifdef __cplusplus } #endif diff --git a/ssl/s3_lib.c b/ssl/s3_lib.c index c2fddb8a35..e346c22eb3 100644 --- a/ssl/s3_lib.c +++ b/ssl/s3_lib.c @@ -159,7 +159,7 @@ const char ssl3_version_str[] = "SSLv3" OPENSSL_VERSION_PTEXT; -#define SSL3_NUM_CIPHERS (sizeof(ssl3_ciphers)/sizeof(SSL_CIPHER)) +#define SSL3_NUM_CIPHERS OSSL_NELEM(ssl3_ciphers) /* list of available SSLv3 ciphers (sorted by id) */ OPENSSL_GLOBAL const SSL_CIPHER ssl3_ciphers[] = { diff --git a/ssl/ssl_ciph.c b/ssl/ssl_ciph.c index 6c6ac8db62..a3dca18ad2 100644 --- a/ssl/ssl_ciph.c +++ b/ssl/ssl_ciph.c @@ -242,7 +242,7 @@ static int ssl_cipher_info_find(const ssl_cipher_table * table, } #define ssl_cipher_info_lookup(table, x) \ - ssl_cipher_info_find(table, sizeof(table)/sizeof(*table), x) + ssl_cipher_info_find(table, OSSL_NELEM(table), x) /* * PKEY_TYPE for GOST89MAC is known in advance, but, because implementation @@ -1531,7 +1531,7 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method, STACK * groups of cipher_aliases added together in one list (otherwise * we would be happy with just the cipher_aliases table). */ - num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER); + num_of_group_aliases = OSSL_NELEM(cipher_aliases); num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1; ca_list = OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max); if (ca_list == NULL) { diff --git a/ssl/ssl_conf.c b/ssl/ssl_conf.c index 5a19a75dbf..2d96b11995 100644 --- a/ssl/ssl_conf.c +++ b/ssl/ssl_conf.c @@ -220,7 +220,7 @@ static int ctrl_str_option(SSL_CONF_CTX *cctx, const char *cmd) #endif }; cctx->tbl = ssl_option_single; - cctx->ntbl = sizeof(ssl_option_single) / sizeof(ssl_flag_tbl); + cctx->ntbl = OSSL_NELEM(ssl_option_single); return ssl_set_option_list(cmd, -1, cctx); } @@ -335,7 +335,7 @@ static int cmd_Protocol(SSL_CONF_CTX *cctx, const char *value) if (!(cctx->flags & SSL_CONF_FLAG_FILE)) return -2; cctx->tbl = ssl_protocol_list; - cctx->ntbl = sizeof(ssl_protocol_list) / sizeof(ssl_flag_tbl); + cctx->ntbl = OSSL_NELEM(ssl_protocol_list); return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx); } @@ -360,7 +360,7 @@ static int cmd_Options(SSL_CONF_CTX *cctx, const char *value) if (value == NULL) return -3; cctx->tbl = ssl_option_list; - cctx->ntbl = sizeof(ssl_option_list) / sizeof(ssl_flag_tbl); + cctx->ntbl = OSSL_NELEM(ssl_option_list); return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx); } @@ -508,8 +508,7 @@ static const ssl_conf_cmd_tbl *ssl_conf_cmd_lookup(SSL_CONF_CTX *cctx, return NULL; /* Look for matching parameter name in table */ - for (i = 0, t = ssl_conf_cmds; - i < sizeof(ssl_conf_cmds) / sizeof(ssl_conf_cmd_tbl); i++, t++) { + for (i = 0, t = ssl_conf_cmds; i < OSSL_NELEM(ssl_conf_cmds); i++, t++) { if (cctx->flags & SSL_CONF_FLAG_CMDLINE) { if (t->str_cmdline && !strcmp(t->str_cmdline, cmd)) return t; diff --git a/ssl/t1_lib.c b/ssl/t1_lib.c index 31ebfdde45..7af9c7e3dd 100644 --- a/ssl/t1_lib.c +++ b/ssl/t1_lib.c @@ -307,8 +307,7 @@ static const unsigned char suiteb_curves[] = { int tls1_ec_curve_id2nid(int curve_id) { /* ECC curves from RFC 4492 and RFC 7027 */ - if ((curve_id < 1) || ((unsigned int)curve_id > - sizeof(nid_list) / sizeof(nid_list[0]))) + if ((curve_id < 1) || ((unsigned int)curve_id > OSSL_NELEM(nid_list))) return 0; return nid_list[curve_id - 1].nid; } @@ -442,8 +441,7 @@ static int tls_curve_allowed(SSL *s, const unsigned char *curve, int op) const tls_curve_info *cinfo; if (curve[0]) return 1; - if ((curve[1] < 1) || ((size_t)curve[1] > - sizeof(nid_list) / sizeof(nid_list[0]))) + if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list))) return 0; cinfo = &nid_list[curve[1] - 1]; # ifdef OPENSSL_NO_EC2M @@ -3172,8 +3170,7 @@ int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, int sig_id, md_id; if (!md) return 0; - md_id = tls12_find_id(EVP_MD_type(md), tls12_md, - sizeof(tls12_md) / sizeof(tls12_lookup)); + md_id = tls12_find_id(EVP_MD_type(md), tls12_md, OSSL_NELEM(tls12_md)); if (md_id == -1) return 0; sig_id = tls12_get_sigid(pk); @@ -3186,8 +3183,7 @@ int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, int tls12_get_sigid(const EVP_PKEY *pk) { - return tls12_find_id(pk->type, tls12_sig, - sizeof(tls12_sig) / sizeof(tls12_lookup)); + return tls12_find_id(pk->type, tls12_sig, OSSL_NELEM(tls12_sig)); } typedef struct { @@ -3213,7 +3209,7 @@ static const tls12_hash_info *tls12_get_hash_info(unsigned char hash_alg) { if (hash_alg == 0) return NULL; - if (hash_alg > sizeof(tls12_md_info) / sizeof(tls12_md_info[0])) + if (hash_alg > OSSL_NELEM(tls12_md_info)) return NULL; return tls12_md_info + hash_alg - 1; } @@ -3256,14 +3252,12 @@ static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid, if (!phash_nid && !psign_nid && !psignhash_nid) return; if (phash_nid || psignhash_nid) { - hash_nid = tls12_find_nid(data[0], tls12_md, - sizeof(tls12_md) / sizeof(tls12_lookup)); + hash_nid = tls12_find_nid(data[0], tls12_md, OSSL_NELEM(tls12_md)); if (phash_nid) *phash_nid = hash_nid; } if (psign_nid || psignhash_nid) { - sign_nid = tls12_find_nid(data[1], tls12_sig, - sizeof(tls12_sig) / sizeof(tls12_lookup)); + sign_nid = tls12_find_nid(data[1], tls12_sig, OSSL_NELEM(tls12_sig)); if (psign_nid) *psign_nid = sign_nid; } @@ -3806,10 +3800,8 @@ int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, if (sigalgs == NULL) return 0; for (i = 0, sptr = sigalgs; i < salglen; i += 2) { - rhash = tls12_find_id(*psig_nids++, tls12_md, - sizeof(tls12_md) / sizeof(tls12_lookup)); - rsign = tls12_find_id(*psig_nids++, tls12_sig, - sizeof(tls12_sig) / sizeof(tls12_lookup)); + rhash = tls12_find_id(*psig_nids++, tls12_md, OSSL_NELEM(tls12_md)); + rsign = tls12_find_id(*psig_nids++, tls12_sig, OSSL_NELEM(tls12_sig)); if (rhash == -1 || rsign == -1) goto err; diff --git a/ssl/t1_trce.c b/ssl/t1_trce.c index 77f2a9f176..f8d19b87ad 100644 --- a/ssl/t1_trce.c +++ b/ssl/t1_trce.c @@ -65,11 +65,11 @@ typedef struct { } ssl_trace_tbl; # define ssl_trace_str(val, tbl) \ - do_ssl_trace_str(val, tbl, sizeof(tbl)/sizeof(ssl_trace_tbl)) + do_ssl_trace_str(val, tbl, OSSL_NELEM(tbl)) # define ssl_trace_list(bio, indent, msg, msglen, value, table) \ do_ssl_trace_list(bio, indent, msg, msglen, value, \ - table, sizeof(table)/sizeof(ssl_trace_tbl)) + table, OSSL_NELEM(table)) static const char *do_ssl_trace_str(int val, ssl_trace_tbl *tbl, size_t ntbl) {