From 388f4fd7ef45b3835a98a960c9ed299107175e6c Mon Sep 17 00:00:00 2001 From: Piyush Shah Date: Fri, 3 Apr 2020 17:08:24 +0530 Subject: [PATCH] wifi_provisioning: Add a check for number of scanned networks while allocating memory If number of scanned networks is zero, while getting the list, since we try to allocate zero bytes, we get a NULL pointer in response. This is considered as an error and the provisioning worflow breaks there. Adding a check before allocation resolves the issue. --- components/wifi_provisioning/src/wifi_scan.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/components/wifi_provisioning/src/wifi_scan.c b/components/wifi_provisioning/src/wifi_scan.c index 4235d23b91..bfc0596f52 100644 --- a/components/wifi_provisioning/src/wifi_scan.c +++ b/components/wifi_provisioning/src/wifi_scan.c @@ -134,16 +134,21 @@ static esp_err_t cmd_scan_result_handler(WiFiScanPayload *req, resp->status = STATUS__Success; resp->payload_case = WI_FI_SCAN_PAYLOAD__PAYLOAD_RESP_SCAN_RESULT; resp->resp_scan_result = resp_payload; - - results = (WiFiScanResult **) calloc(req->cmd_scan_result->count, - sizeof(WiFiScanResult *)); - if (!results) { - ESP_LOGE(TAG, "Failed to allocate memory for results array"); - return ESP_ERR_NO_MEM; + /* Allocate memory only if there are non-zero scan results */ + if (req->cmd_scan_result->count) { + results = (WiFiScanResult **) calloc(req->cmd_scan_result->count, + sizeof(WiFiScanResult *)); + if (!results) { + ESP_LOGE(TAG, "Failed to allocate memory for results array"); + return ESP_ERR_NO_MEM; + } } resp_payload->entries = results; resp_payload->n_entries = req->cmd_scan_result->count; + /* If req->cmd_scan_result->count is 0, the below loop will + * be skipped. + */ for (uint16_t i = 0; i < req->cmd_scan_result->count; i++) { err = h->scan_result(i + req->cmd_scan_result->start_index, &scan_result, &h->ctx);