From 0f3286aa92c97af86babaf2203538b06f700daa0 Mon Sep 17 00:00:00 2001 From: muhaidong Date: Sat, 3 Feb 2024 16:29:26 +0800 Subject: [PATCH] fix(wifi): fix esp_wifi_scan_start memory leakage issue Closes https://github.com/espressif/esp-idf/issues/10693 --- components/wifi_provisioning/src/manager.c | 1 + components/wpa_supplicant/esp_supplicant/src/esp_scan.c | 7 ++----- examples/bluetooth/blufi/main/blufi_example_main.c | 1 + .../esp_ble_mesh/ble_mesh_wifi_coexist/main/cmd_wifi.c | 1 + examples/wifi/ftm/main/ftm_main.c | 1 + examples/wifi/iperf/main/cmd_wifi.c | 1 + examples/wifi/roaming/main/roaming_example.c | 4 +--- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/components/wifi_provisioning/src/manager.c b/components/wifi_provisioning/src/manager.c index 9c56e6d07b..b68722c2cc 100644 --- a/components/wifi_provisioning/src/manager.c +++ b/components/wifi_provisioning/src/manager.c @@ -770,6 +770,7 @@ static esp_err_t update_wifi_scan_results(void) prov_ctx->ap_list[curr_channel] = (wifi_ap_record_t *) calloc(get_count, sizeof(wifi_ap_record_t)); if (!prov_ctx->ap_list[curr_channel]) { ESP_LOGE(TAG, "Failed to allocate memory for AP list"); + esp_wifi_clear_ap_list(); goto exit; } if (esp_wifi_scan_get_ap_records(&get_count, prov_ctx->ap_list[curr_channel]) != ESP_OK) { diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_scan.c b/components/wpa_supplicant/esp_supplicant/src/esp_scan.c index 58961c8366..3fab124be5 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_scan.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_scan.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -66,13 +66,10 @@ static void handle_wnm_scan_done(struct wpa_supplicant *wpa_s) static void scan_done_cleanup(struct wpa_supplicant *wpa_s) { - uint16_t number = 1; - wifi_ap_record_t ap_records; - wpa_s->scanning = 0; wpa_s->scan_reason = 0; /* clean scan list from net80211 */ - esp_wifi_scan_get_ap_records(&number, &ap_records); + esp_wifi_clear_ap_list(); } void esp_supplicant_handle_scan_done_evt(void) diff --git a/examples/bluetooth/blufi/main/blufi_example_main.c b/examples/bluetooth/blufi/main/blufi_example_main.c index a4db69cc56..59f9ffd4a8 100644 --- a/examples/bluetooth/blufi/main/blufi_example_main.c +++ b/examples/bluetooth/blufi/main/blufi_example_main.c @@ -208,6 +208,7 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base, wifi_ap_record_t *ap_list = (wifi_ap_record_t *)malloc(sizeof(wifi_ap_record_t) * apCount); if (!ap_list) { BLUFI_ERROR("malloc error, ap_list is NULL"); + esp_wifi_clear_ap_list(); break; } ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&apCount, ap_list)); diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/main/cmd_wifi.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/main/cmd_wifi.c index da77ae8656..42da684a3a 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/main/cmd_wifi.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/main/cmd_wifi.c @@ -67,6 +67,7 @@ static void scan_done_handler(void) ap_list_buffer = malloc(sta_number * sizeof(wifi_ap_record_t)); if (ap_list_buffer == NULL) { ESP_LOGE(TAG, "Failed to malloc buffer to print scan results"); + esp_wifi_clear_ap_list(); return; } diff --git a/examples/wifi/ftm/main/ftm_main.c b/examples/wifi/ftm/main/ftm_main.c index 89fcfb544f..bc8e27fada 100644 --- a/examples/wifi/ftm/main/ftm_main.c +++ b/examples/wifi/ftm/main/ftm_main.c @@ -304,6 +304,7 @@ static bool wifi_perform_scan(const char *ssid, bool internal) g_ap_list_buffer = malloc(g_scan_ap_num * sizeof(wifi_ap_record_t)); if (g_ap_list_buffer == NULL) { ESP_LOGE(TAG_STA, "Failed to malloc buffer to print scan results"); + esp_wifi_clear_ap_list(); return false; } diff --git a/examples/wifi/iperf/main/cmd_wifi.c b/examples/wifi/iperf/main/cmd_wifi.c index 745158cb3b..6a22263a48 100644 --- a/examples/wifi/iperf/main/cmd_wifi.c +++ b/examples/wifi/iperf/main/cmd_wifi.c @@ -76,6 +76,7 @@ static void scan_done_handler(void *arg, esp_event_base_t event_base, ap_list_buffer = malloc(sta_number * sizeof(wifi_ap_record_t)); if (ap_list_buffer == NULL) { ESP_LOGE(TAG, "Failed to malloc buffer to print scan results"); + esp_wifi_clear_ap_list(); return; } diff --git a/examples/wifi/roaming/main/roaming_example.c b/examples/wifi/roaming/main/roaming_example.c index 9b61dbc284..86fd8fc01c 100644 --- a/examples/wifi/roaming/main/roaming_example.c +++ b/examples/wifi/roaming/main/roaming_example.c @@ -305,9 +305,7 @@ void neighbor_report_recv_cb(void *ctx, const uint8_t *report, size_t report_len goto cleanup; } /* cleanup from net802.11 */ - uint16_t number = 1; - wifi_ap_record_t ap_records; - esp_wifi_scan_get_ap_records(&number, &ap_records); + esp_wifi_clear_ap_list(); cand_list = 1; } /* send AP btm query, this will cause STA to roam as well */