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.
This commit is contained in:
Piyush Shah 2020-04-03 17:08:24 +05:30 committed by bot
parent 317c882133
commit 388f4fd7ef

View File

@ -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);