Use ARRAY_SIZE() macro

Replace the common sizeof(a)/sizeof(a[0]) constructions with a more
readable version.

Signed-hostap: Jouni Malinen <j@w1.fi>
This commit is contained in:
Jouni Malinen 2013-10-26 17:00:57 +03:00
parent 39044a7033
commit e7ecab4a3b
22 changed files with 51 additions and 56 deletions

View file

@ -358,7 +358,7 @@ static int acs_usable_ht40_chan(struct hostapd_channel_data *chan)
157, 184, 192 };
unsigned int i;
for (i = 0; i < sizeof(allowed) / sizeof(allowed[0]); i++)
for (i = 0; i < ARRAY_SIZE(allowed); i++)
if (chan->chan == allowed[i])
return 1;

View file

@ -74,11 +74,11 @@ static int dfs_is_chan_allowed(struct hostapd_channel_data *chan, int n_chans)
switch (n_chans) {
case 2:
allowed = allowed_40;
allowed_no = sizeof(allowed_40) / sizeof(allowed_40[0]);
allowed_no = ARRAY_SIZE(allowed_40);
break;
case 4:
allowed = allowed_80;
allowed_no = sizeof(allowed_80) / sizeof(allowed_80[0]);
allowed_no = ARRAY_SIZE(allowed_80);
break;
default:
wpa_printf(MSG_DEBUG, "Unknown width for %d channels", n_chans);

View file

@ -270,7 +270,7 @@ static int ieee80211n_allowed_ht40_channel_pair(struct hostapd_iface *iface)
first = sec_chan;
ok = 0;
for (k = 0; k < sizeof(allowed) / sizeof(allowed[0]); k++) {
for (k = 0; k < ARRAY_SIZE(allowed); k++) {
if (first == allowed[k]) {
ok = 1;
break;

View file

@ -1169,7 +1169,7 @@ static struct dh_group dh_groups[] = {
#endif /* ALL_DH_GROUPS */
};
#define NUM_DH_GROUPS (sizeof(dh_groups) / sizeof(dh_groups[0]))
#define NUM_DH_GROUPS ARRAY_SIZE(dh_groups)
const struct dh_group * dh_groups_get(int id)

View file

@ -182,7 +182,7 @@ set80211priv(struct madwifi_driver_data *drv, int op, void *data, int len)
#endif /* MADWIFI_NG */
int idx = op - first;
if (first <= op &&
idx < (int) (sizeof(opnames) / sizeof(opnames[0])) &&
idx < (int) ARRAY_SIZE(opnames) &&
opnames[idx])
perror(opnames[idx]);
else

View file

@ -3967,7 +3967,7 @@ static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
"handle %p", bss->nl_mgmt);
for (i = 0; i < sizeof(stypes) / sizeof(stypes[0]); i++) {
for (i = 0; i < ARRAY_SIZE(stypes); i++) {
if (nl80211_register_frame(bss, bss->nl_mgmt,
(WLAN_FC_TYPE_MGMT << 2) |
(stypes[i] << 4),
@ -7238,7 +7238,7 @@ static struct sock_filter msock_filter_insns[] = {
};
static struct sock_fprog msock_filter = {
.len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
.len = ARRAY_SIZE(msock_filter_insns),
.filter = msock_filter_insns,
};

View file

@ -21,7 +21,7 @@ static struct ikev2_integ_alg ikev2_integ_algs[] = {
{ AUTH_HMAC_MD5_96, 16, 12 }
};
#define NUM_INTEG_ALGS (sizeof(ikev2_integ_algs) / sizeof(ikev2_integ_algs[0]))
#define NUM_INTEG_ALGS ARRAY_SIZE(ikev2_integ_algs)
static struct ikev2_prf_alg ikev2_prf_algs[] = {
@ -29,7 +29,7 @@ static struct ikev2_prf_alg ikev2_prf_algs[] = {
{ PRF_HMAC_MD5, 16, 16 }
};
#define NUM_PRF_ALGS (sizeof(ikev2_prf_algs) / sizeof(ikev2_prf_algs[0]))
#define NUM_PRF_ALGS ARRAY_SIZE(ikev2_prf_algs)
static struct ikev2_encr_alg ikev2_encr_algs[] = {
@ -37,7 +37,7 @@ static struct ikev2_encr_alg ikev2_encr_algs[] = {
{ ENCR_3DES, 24, 8 }
};
#define NUM_ENCR_ALGS (sizeof(ikev2_encr_algs) / sizeof(ikev2_encr_algs[0]))
#define NUM_ENCR_ALGS ARRAY_SIZE(ikev2_encr_algs)
const struct ikev2_integ_alg * ikev2_get_integ(int id)

View file

@ -233,7 +233,7 @@ static struct radius_attr_type radius_attrs[] =
{ RADIUS_ATTR_NAS_IPV6_ADDRESS, "NAS-IPv6-Address", RADIUS_ATTR_IPV6 },
{ RADIUS_ATTR_ERROR_CAUSE, "Error-Cause", RADIUS_ATTR_INT32 }
};
#define RADIUS_ATTRS (sizeof(radius_attrs) / sizeof(radius_attrs[0]))
#define RADIUS_ATTRS ARRAY_SIZE(radius_attrs)
static struct radius_attr_type *radius_get_attr_type(u8 type)

View file

@ -57,8 +57,7 @@ static const struct tls_cipher_suite tls_cipher_suites[] = {
TLS_CIPHER_AES_256_CBC, TLS_HASH_SHA256 }
};
#define NUM_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
#define NUM_TLS_CIPHER_SUITES NUM_ELEMS(tls_cipher_suites)
#define NUM_TLS_CIPHER_SUITES ARRAY_SIZE(tls_cipher_suites)
static const struct tls_cipher_data tls_ciphers[] = {
@ -84,7 +83,7 @@ static const struct tls_cipher_data tls_ciphers[] = {
CRYPTO_CIPHER_ALG_AES }
};
#define NUM_TLS_CIPHER_DATA NUM_ELEMS(tls_ciphers)
#define NUM_TLS_CIPHER_DATA ARRAY_SIZE(tls_ciphers)
/**

View file

@ -268,7 +268,7 @@ int os_program_init(void)
struct __user_cap_header_struct header;
struct __user_cap_data_struct cap;
setgroups(sizeof(groups)/sizeof(groups[0]), groups);
setgroups(ARRAY_SIZE(groups), groups);
prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);

View file

@ -134,7 +134,7 @@ static int test_cbc(void)
u8 *buf;
unsigned int i;
for (i = 0; i < sizeof(vectors) / sizeof(vectors[0]); i++) {
for (i = 0; i < ARRAY_SIZE(vectors); i++) {
struct cbc_test_vector *tv = &vectors[i];
buf = malloc(tv->len);
if (buf == NULL) {
@ -347,7 +347,7 @@ static int test_gcm(void)
u8 p[64], c[64], tmp[64];
size_t k_len, p_len, aad_len, iv_len;
for (i = 0; i < sizeof(gcm_tests) / sizeof(gcm_tests[0]); i++) {
for (i = 0; i < ARRAY_SIZE(gcm_tests); i++) {
const struct gcm_test_vector *tc = &gcm_tests[i];
k_len = os_strlen(tc->k) / 2;
@ -542,7 +542,7 @@ int main(int argc, char *argv[])
test_aes_perf();
for (i = 0; i < sizeof(test_vectors) / sizeof(test_vectors[0]); i++) {
for (i = 0; i < ARRAY_SIZE(test_vectors); i++) {
tv = &test_vectors[i];
if (omac1_aes_128(tv->k, tv->msg, tv->msg_len, result) ||
memcmp(result, tv->tag, 16) != 0) {

View file

@ -61,7 +61,7 @@ int main(int argc, char *argv[])
size_t len[2];
int errors = 0;
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
for (i = 0; i < ARRAY_SIZE(tests); i++) {
printf("MD4 test case %d:", i);
addr[0] = (u8 *) tests[i].data;

View file

@ -61,7 +61,7 @@ int main(int argc, char *argv[])
size_t len[2];
int errors = 0;
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
for (i = 0; i < ARRAY_SIZE(tests); i++) {
printf("MD5 test case %d:", i);
addr[0] = (u8 *) tests[i].data;

View file

@ -249,7 +249,7 @@ static const struct gsm_milenage_test_set gsm_test_sets[] =
}
};
#define NUM_GSM_TESTS (sizeof(gsm_test_sets) / sizeof(gsm_test_sets[0]))
#define NUM_GSM_TESTS ARRAY_SIZE(gsm_test_sets)
struct milenage_test_set {
@ -693,7 +693,7 @@ static const struct milenage_test_set test_sets[] =
}
};
#define NUM_TESTS (sizeof(test_sets) / sizeof(test_sets[0]))
#define NUM_TESTS ARRAY_SIZE(test_sets)
int main(int argc, char *argv[])

View file

@ -198,7 +198,7 @@ static const struct rc4_test_vector tests[] = {
}
};
#define NUM_TESTS (sizeof(tests) / sizeof(tests[0]))
#define NUM_TESTS ARRAY_SIZE(tests)
static int run_test(unsigned int i, const u8 *key, size_t key_len,

View file

@ -282,8 +282,7 @@ static struct passphrase_test passphrase_tests[] =
},
};
#define NUM_PASSPHRASE_TESTS \
(sizeof(passphrase_tests) / sizeof(passphrase_tests[0]))
#define NUM_PASSPHRASE_TESTS ARRAY_SIZE(passphrase_tests)
struct rfc6070_test {
@ -368,8 +367,7 @@ static struct rfc6070_test rfc6070_tests[] =
#endif
};
#define NUM_RFC6070_TESTS \
(sizeof(rfc6070_tests) / sizeof(rfc6070_tests[0]))
#define NUM_RFC6070_TESTS ARRAY_SIZE(rfc6070_tests)
int main(int argc, char *argv[])

View file

@ -251,7 +251,7 @@ int main(int argc, char *argv[])
size_t len[2];
int errors = 0;
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
for (i = 0; i < ARRAY_SIZE(tests); i++) {
printf("SHA256 test case %d:", i + 1);
addr[0] = (u8 *) tests[i].data;
@ -279,7 +279,7 @@ int main(int argc, char *argv[])
printf("\n");
}
for (i = 0; i < sizeof(hmac_tests) / sizeof(hmac_tests[0]); i++) {
for (i = 0; i < ARRAY_SIZE(hmac_tests); i++) {
struct hmac_test *t = &hmac_tests[i];
printf("HMAC-SHA256 test case %d:", i + 1);

View file

@ -622,7 +622,7 @@ static char ** complete_get_sta_counter(int s, const char *str, int pos)
switch (arg) {
case 1:
/* counter list */
count = sizeof(sta_counters) / sizeof(sta_counters[0]);
count = ARRAY_SIZE(sta_counters);
res = os_calloc(count, sizeof(char *));
if (res == NULL)
return NULL;
@ -721,7 +721,7 @@ static char ** complete_get_bss_counter(int s, const char *str, int pos)
switch (arg) {
case 1:
/* counter list */
count = sizeof(bss_counters) / sizeof(bss_counters[0]);
count = ARRAY_SIZE(bss_counters);
res = os_calloc(count, sizeof(char *));
if (res == NULL)
return NULL;
@ -835,7 +835,7 @@ static char ** complete_get_tdls_counter(int s, const char *str, int pos)
switch (arg) {
case 1:
/* counter list */
count = sizeof(tdls_counters) / sizeof(tdls_counters[0]);
count = ARRAY_SIZE(tdls_counters);
res = os_calloc(count, sizeof(char *));
if (res == NULL)
return NULL;
@ -971,7 +971,7 @@ static char ** complete_inject(int s, const char *str, int pos)
switch (arg) {
case 1:
/* frame list */
count = sizeof(inject_frames) / sizeof(inject_frames[0]);
count = ARRAY_SIZE(inject_frames);
res = os_calloc(count, sizeof(char *));
if (res == NULL)
break;
@ -1322,7 +1322,7 @@ static char ** complete_info_sta(int s, const char *str, int pos)
switch (arg) {
case 1:
/* counter list */
count = sizeof(sta_infos) / sizeof(sta_infos[0]);
count = ARRAY_SIZE(sta_infos);
res = os_calloc(count, sizeof(char *));
if (res == NULL)
return NULL;
@ -1427,7 +1427,7 @@ static char ** complete_info_bss(int s, const char *str, int pos)
switch (arg) {
case 1:
/* counter list */
count = sizeof(bss_infos) / sizeof(bss_infos[0]);
count = ARRAY_SIZE(bss_infos);
res = os_calloc(count, sizeof(char *));
if (res == NULL)
return NULL;
@ -1606,11 +1606,9 @@ static void wlantest_cli_edit_eof_cb(void *ctx)
static char ** wlantest_cli_cmd_list(void)
{
char **res;
int i, count;
int i;
count = sizeof(wlantest_cli_commands) /
sizeof(wlantest_cli_commands[0]);
res = os_calloc(count, sizeof(char *));
res = os_calloc(ARRAY_SIZE(wlantest_cli_commands), sizeof(char *));
if (res == NULL)
return NULL;

View file

@ -1686,7 +1686,7 @@ static const struct parse_data ssid_fields[] = {
#undef _FUNC
#undef FUNC
#undef FUNC_KEY
#define NUM_SSID_FIELDS (sizeof(ssid_fields) / sizeof(ssid_fields[0]))
#define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
/**
@ -3278,7 +3278,7 @@ static const struct global_parse_data global_fields[] = {
#undef STR
#undef STR_RANGE
#undef BIN
#define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
#define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
int wpa_config_process_global(struct wpa_config *config, char *pos, int line)

View file

@ -2030,7 +2030,7 @@ dbus_bool_t wpas_dbus_getter_capabilities(DBusMessageIter *iter,
const char *args[] = {"ccmp", "tkip", "none"};
if (!wpa_dbus_dict_append_string_array(
&iter_dict, "Pairwise", args,
sizeof(args) / sizeof(char*)))
ARRAY_SIZE(args)))
goto nomem;
} else {
if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Pairwise",
@ -2077,7 +2077,7 @@ dbus_bool_t wpas_dbus_getter_capabilities(DBusMessageIter *iter,
};
if (!wpa_dbus_dict_append_string_array(
&iter_dict, "Group", args,
sizeof(args) / sizeof(char*)))
ARRAY_SIZE(args)))
goto nomem;
} else {
if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Group",
@ -2134,7 +2134,7 @@ dbus_bool_t wpas_dbus_getter_capabilities(DBusMessageIter *iter,
};
if (!wpa_dbus_dict_append_string_array(
&iter_dict, "KeyMgmt", args,
sizeof(args) / sizeof(char*)))
ARRAY_SIZE(args)))
goto nomem;
} else {
if (!wpa_dbus_dict_begin_string_array(&iter_dict, "KeyMgmt",
@ -2214,7 +2214,7 @@ dbus_bool_t wpas_dbus_getter_capabilities(DBusMessageIter *iter,
const char *args[] = { "rsn", "wpa" };
if (!wpa_dbus_dict_append_string_array(
&iter_dict, "Protocol", args,
sizeof(args) / sizeof(char*)))
ARRAY_SIZE(args)))
goto nomem;
} else {
if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Protocol",
@ -2249,7 +2249,7 @@ dbus_bool_t wpas_dbus_getter_capabilities(DBusMessageIter *iter,
const char *args[] = { "open", "shared", "leap" };
if (!wpa_dbus_dict_append_string_array(
&iter_dict, "AuthAlg", args,
sizeof(args) / sizeof(char*)))
ARRAY_SIZE(args)))
goto nomem;
} else {
if (!wpa_dbus_dict_begin_string_array(&iter_dict, "AuthAlg",
@ -2285,7 +2285,7 @@ dbus_bool_t wpas_dbus_getter_capabilities(DBusMessageIter *iter,
/***** Scan */
if (!wpa_dbus_dict_append_string_array(&iter_dict, "Scan", scans,
sizeof(scans) / sizeof(char *)))
ARRAY_SIZE(scans)))
goto nomem;
/***** Modes */

View file

@ -539,7 +539,7 @@ DBusMessage * wpas_dbus_iface_capabilities(DBusMessage *message,
const char *args[] = {"CCMP", "TKIP", "NONE"};
if (!wpa_dbus_dict_append_string_array(
&iter_dict, "pairwise", args,
sizeof(args) / sizeof(char*)))
ARRAY_SIZE(args)))
goto error;
}
} else {
@ -582,7 +582,7 @@ DBusMessage * wpas_dbus_iface_capabilities(DBusMessage *message,
};
if (!wpa_dbus_dict_append_string_array(
&iter_dict, "group", args,
sizeof(args) / sizeof(char*)))
ARRAY_SIZE(args)))
goto error;
}
} else {
@ -632,7 +632,7 @@ DBusMessage * wpas_dbus_iface_capabilities(DBusMessage *message,
};
if (!wpa_dbus_dict_append_string_array(
&iter_dict, "key_mgmt", args,
sizeof(args) / sizeof(char*)))
ARRAY_SIZE(args)))
goto error;
}
} else {
@ -683,7 +683,7 @@ DBusMessage * wpas_dbus_iface_capabilities(DBusMessage *message,
const char *args[] = { "RSN", "WPA" };
if (!wpa_dbus_dict_append_string_array(
&iter_dict, "proto", args,
sizeof(args) / sizeof(char*)))
ARRAY_SIZE(args)))
goto error;
}
} else {
@ -720,7 +720,7 @@ DBusMessage * wpas_dbus_iface_capabilities(DBusMessage *message,
const char *args[] = { "OPEN", "SHARED", "LEAP" };
if (!wpa_dbus_dict_append_string_array(
&iter_dict, "auth_alg", args,
sizeof(args) / sizeof(char*)))
ARRAY_SIZE(args)))
goto error;
}
} else {

View file

@ -627,7 +627,7 @@ static char ** wpa_cli_complete_set(const char *str, int pos)
"sae_groups", "dtim_period", "beacon_int", "ap_vendor_elements",
"ignore_old_scan_res", "freq_list", "external_sim"
};
int i, num_fields = sizeof(fields) / sizeof(fields[0]);
int i, num_fields = ARRAY_SIZE(fields);
if (arg == 1) {
char **res = os_calloc(num_fields + 1, sizeof(char *));
@ -2112,7 +2112,7 @@ static char ** wpa_cli_complete_p2p_set(const char *str, int pos)
"disc_int",
"per_sta_psk",
};
int i, num_fields = sizeof(fields) / sizeof(fields[0]);
int i, num_fields = ARRAY_SIZE(fields);
if (arg == 1) {
char **res = os_calloc(num_fields + 1, sizeof(char *));
@ -2927,7 +2927,7 @@ static char ** wpa_list_cmd_list(void)
int i, count;
struct cli_txt_entry *e;
count = sizeof(wpa_cli_commands) / sizeof(wpa_cli_commands[0]);
count = ARRAY_SIZE(wpa_cli_commands);
count += dl_list_len(&p2p_groups);
count += dl_list_len(&ifnames);
res = os_calloc(count + 1, sizeof(char *));