From a3c1804d411217e358682bda688c3c78ae7d3bee Mon Sep 17 00:00:00 2001 From: Jouni Malinen Date: Thu, 16 Oct 2025 23:23:12 +0300 Subject: [PATCH] Replace configuration int lists with int_arrays This cleans up implementation by getting rid of very similar construction of a list of int values. hostapd used to terminate the lists with -1 while int_array were terminated with 0. There are no cases where 0 is needed to be included, so all these can be converted into the existing int_array design. Signed-off-by: Jouni Malinen --- hostapd/config_file.c | 2 +- src/ap/ap_config.c | 15 ----------- src/ap/ap_config.h | 1 - src/ap/hw_features.c | 21 +++++---------- src/drivers/driver_nl80211.c | 2 +- src/utils/common.c | 8 ++++++ src/utils/common.h | 1 + tests/hwsim/test_wpas_mesh.py | 2 +- wpa_supplicant/ap.c | 4 +-- wpa_supplicant/mesh.c | 48 +++++++---------------------------- 10 files changed, 30 insertions(+), 74 deletions(-) diff --git a/hostapd/config_file.c b/hostapd/config_file.c index 983dfad0a..54ad77bcb 100644 --- a/hostapd/config_file.c +++ b/hostapd/config_file.c @@ -861,7 +861,7 @@ static int hostapd_parse_intlist(int **int_list, char *val) break; pos = end + 1; } - list[count] = -1; + list[count] = 0; *int_list = list; return 0; diff --git a/src/ap/ap_config.c b/src/ap/ap_config.c index ed1d13b70..d1d988b5f 100644 --- a/src/ap/ap_config.c +++ b/src/ap/ap_config.c @@ -1075,21 +1075,6 @@ int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries, } -int hostapd_rate_found(int *list, int rate) -{ - int i; - - if (list == NULL) - return 0; - - for (i = 0; list[i] >= 0; i++) - if (list[i] == rate) - return 1; - - return 0; -} - - int hostapd_vlan_valid(struct hostapd_vlan *vlan, struct vlan_description *vlan_desc) { diff --git a/src/ap/ap_config.h b/src/ap/ap_config.h index db5f75ef0..44988d261 100644 --- a/src/ap/ap_config.h +++ b/src/ap/ap_config.h @@ -1382,7 +1382,6 @@ void hostapd_config_free_bss(struct hostapd_bss_config *conf); void hostapd_config_free(struct hostapd_config *conf); int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries, const u8 *addr, struct vlan_description *vlan_id); -int hostapd_rate_found(int *list, int rate); const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf, const u8 *addr, const u8 *p2p_dev_addr, const u8 *prev_psk, int *vlan_id); diff --git a/src/ap/hw_features.c b/src/ap/hw_features.c index 1f80bdd24..460b1d3ad 100644 --- a/src/ap/hw_features.c +++ b/src/ap/hw_features.c @@ -198,10 +198,10 @@ int hostapd_prepare_rates(struct hostapd_iface *iface, struct hostapd_hw_modes *mode) { int i, num_basic_rates = 0; - int basic_rates_a[] = { 60, 120, 240, -1 }; - int basic_rates_b[] = { 10, 20, -1 }; - int basic_rates_g[] = { 10, 20, 55, 110, -1 }; - int *basic_rates; + int basic_rates_a[] = { 60, 120, 240, 0 }; + int basic_rates_b[] = { 10, 20, 0 }; + int basic_rates_g[] = { 10, 20, 55, 110, 0 }; + const int *basic_rates; if (iface->conf->basic_rates) basic_rates = iface->conf->basic_rates; @@ -221,15 +221,8 @@ int hostapd_prepare_rates(struct hostapd_iface *iface, return -1; } - i = 0; - while (basic_rates[i] >= 0) - i++; - if (i) - i++; /* -1 termination */ os_free(iface->basic_rates); - iface->basic_rates = os_malloc(i * sizeof(int)); - if (iface->basic_rates) - os_memcpy(iface->basic_rates, basic_rates, i * sizeof(int)); + iface->basic_rates = int_array_dup(basic_rates); os_free(iface->current_rates); iface->num_rates = 0; @@ -246,13 +239,13 @@ int hostapd_prepare_rates(struct hostapd_iface *iface, struct hostapd_rate_data *rate; if (iface->conf->supported_rates && - !hostapd_rate_found(iface->conf->supported_rates, + !int_array_includes(iface->conf->supported_rates, mode->rates[i])) continue; rate = &iface->current_rates[iface->num_rates]; rate->rate = mode->rates[i]; - if (hostapd_rate_found(basic_rates, rate->rate)) { + if (int_array_includes(basic_rates, rate->rate)) { rate->flags |= HOSTAPD_RATE_BASIC; num_basic_rates++; } diff --git a/src/drivers/driver_nl80211.c b/src/drivers/driver_nl80211.c index f31c54dc4..bf9aeb22b 100644 --- a/src/drivers/driver_nl80211.c +++ b/src/drivers/driver_nl80211.c @@ -4747,7 +4747,7 @@ static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates) if (!basic_rates) return 0; - for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++) + for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] > 0; i++) rates[rates_len++] = basic_rates[i] / 5; return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates); diff --git a/src/utils/common.c b/src/utils/common.c index eb5a68b49..d90bdb989 100644 --- a/src/utils/common.c +++ b/src/utils/common.c @@ -1025,6 +1025,14 @@ bool int_array_equal(const int *a, const int *b) } +int * int_array_dup(const int *a) +{ + if (!a) + return NULL; + return os_memdup(a, (int_array_len(a) + 1) * sizeof(int)); +} + + void str_clear_free(char *str) { if (str) { diff --git a/src/utils/common.h b/src/utils/common.h index d7b3600f2..835818dfb 100644 --- a/src/utils/common.h +++ b/src/utils/common.h @@ -596,6 +596,7 @@ void int_array_sort_unique(int *a); void int_array_add_unique(int **res, int a); bool int_array_includes(const int *arr, int val); bool int_array_equal(const int *a, const int *b); +int * int_array_dup(const int *a); #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) diff --git a/tests/hwsim/test_wpas_mesh.py b/tests/hwsim/test_wpas_mesh.py index 1aa3b5c17..3be63fe0b 100644 --- a/tests/hwsim/test_wpas_mesh.py +++ b/tests/hwsim/test_wpas_mesh.py @@ -1780,7 +1780,7 @@ def test_mesh_oom(dev, apdev): if ev is None: raise Exception("Init failure not reported") - with alloc_fail(dev[0], 2, "=wpa_supplicant_mesh_init"): + with alloc_fail(dev[0], 1, "int_array_dup;wpa_supplicant_mesh_init"): add_open_mesh_network(dev[0], basic_rates="60 120 240") ev = dev[0].wait_event(["Failed to init mesh"]) if ev is None: diff --git a/wpa_supplicant/ap.c b/wpa_supplicant/ap.c index 7cf557f08..eddc40ad2 100644 --- a/wpa_supplicant/ap.c +++ b/wpa_supplicant/ap.c @@ -521,7 +521,7 @@ static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s, list[0] = 60; list[1] = 120; list[2] = 240; - list[3] = -1; + list[3] = 0; } conf->basic_rates = list; @@ -535,7 +535,7 @@ static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s, list[5] = 360; list[6] = 480; list[7] = 540; - list[8] = -1; + list[8] = 0; } conf->supported_rates = list; } diff --git a/wpa_supplicant/mesh.c b/wpa_supplicant/mesh.c index 297d644e5..8f2c3c5f0 100644 --- a/wpa_supplicant/mesh.c +++ b/wpa_supplicant/mesh.c @@ -150,30 +150,12 @@ static struct mesh_conf * mesh_config_create(struct wpa_supplicant *wpa_s, } -static void wpas_mesh_copy_groups(struct hostapd_data *bss, - struct wpa_supplicant *wpa_s) -{ - int num_groups; - size_t groups_size; - - for (num_groups = 0; wpa_s->conf->sae_groups[num_groups] > 0; - num_groups++) - ; - - groups_size = (num_groups + 1) * sizeof(wpa_s->conf->sae_groups[0]); - bss->conf->sae_groups = os_malloc(groups_size); - if (bss->conf->sae_groups) - os_memcpy(bss->conf->sae_groups, wpa_s->conf->sae_groups, - groups_size); -} - - static int wpas_mesh_init_rsn(struct wpa_supplicant *wpa_s) { struct hostapd_iface *ifmsh = wpa_s->ifmsh; struct wpa_ssid *ssid = wpa_s->current_ssid; struct hostapd_data *bss = ifmsh->bss[0]; - static int default_groups[] = { 19, 20, 21, 25, 26, -1 }; + static int default_groups[] = { 19, 20, 21, 25, 26, 0 }; const char *password; size_t len; @@ -189,14 +171,12 @@ static int wpas_mesh_init_rsn(struct wpa_supplicant *wpa_s) bss->conf->wpa = ssid->proto; bss->conf->wpa_key_mgmt = ssid->key_mgmt; - if (wpa_s->conf->sae_groups && wpa_s->conf->sae_groups[0] > 0) { - wpas_mesh_copy_groups(bss, wpa_s); - } else { - bss->conf->sae_groups = os_memdup(default_groups, - sizeof(default_groups)); - if (!bss->conf->sae_groups) - return -1; - } + if (wpa_s->conf->sae_groups && wpa_s->conf->sae_groups[0] > 0) + bss->conf->sae_groups = int_array_dup(wpa_s->conf->sae_groups); + else + bss->conf->sae_groups = int_array_dup(default_groups); + if (!bss->conf->sae_groups) + return -1; len = os_strlen(password); bss->conf->ssid.wpa_passphrase = dup_binstr(password, len); @@ -386,8 +366,7 @@ static int wpa_supplicant_mesh_init(struct wpa_supplicant *wpa_s, struct hostapd_data *bss; struct hostapd_config *conf; struct mesh_conf *mconf; - int basic_rates_erp[] = { 10, 20, 55, 60, 110, 120, 240, -1 }; - int rate_len; + int basic_rates_erp[] = { 10, 20, 55, 60, 110, 120, 240, 0 }; int frequency; bool is_dfs; u8 chan; @@ -554,18 +533,9 @@ static int wpa_supplicant_mesh_init(struct wpa_supplicant *wpa_s, goto out_free; } } else { - rate_len = 0; - while (1) { - if (ssid->mesh_basic_rates[rate_len] < 1) - break; - rate_len++; - } - conf->basic_rates = os_calloc(rate_len + 1, sizeof(int)); + conf->basic_rates = int_array_dup(ssid->mesh_basic_rates); if (conf->basic_rates == NULL) goto out_free; - os_memcpy(conf->basic_rates, ssid->mesh_basic_rates, - rate_len * sizeof(int)); - conf->basic_rates[rate_len] = -1; } /* While it can enhance performance to switch the primary channel, which -- 2.47.3