From f4d8a481addfa3d772d1d08935c299e95c060afe Mon Sep 17 00:00:00 2001 From: Juan Luis Leal Contreras Date: Wed, 2 Nov 2022 11:30:43 +0100 Subject: [PATCH] Fixed #7406 crash on WiFi STA_DISCONNECTED event with reason 0 (#7414) Fixed #7406 . The "reason2str" macro in WiFiGeneric.cpp tries to read memory from index "-1" in "system_event_reasons" array when handling STA_DISCONNECTED event with reason 0. Dealing with reason 0 as a reason 1 (WIFI_REASON_UNSPECIFIED) will solve the problem (the reason for this event to arrive with reason 0 is unknown). #7406 --- libraries/WiFi/src/WiFiGeneric.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libraries/WiFi/src/WiFiGeneric.cpp b/libraries/WiFi/src/WiFiGeneric.cpp index e64ac631020..bb41dfa18c0 100644 --- a/libraries/WiFi/src/WiFiGeneric.cpp +++ b/libraries/WiFi/src/WiFiGeneric.cpp @@ -947,6 +947,9 @@ esp_err_t WiFiGenericClass::_eventCallback(arduino_event_t *event) //esp_netif_create_ip6_linklocal(esp_netifs[ESP_IF_WIFI_STA]); } else if(event->event_id == ARDUINO_EVENT_WIFI_STA_DISCONNECTED) { uint8_t reason = event->event_info.wifi_sta_disconnected.reason; + // Reason 0 causes crash, use reason 1 (UNSPECIFIED) instead + if(!reason) + reason = WIFI_REASON_UNSPECIFIED; log_w("Reason: %u - %s", reason, reason2str(reason)); if(reason == WIFI_REASON_NO_AP_FOUND) { WiFiSTAClass::_setStatus(WL_NO_SSID_AVAIL);