diff --git a/libraries/WiFi/src/WiFiGeneric.cpp b/libraries/WiFi/src/WiFiGeneric.cpp index 7f5b572edc2..380e4c5b275 100644 --- a/libraries/WiFi/src/WiFiGeneric.cpp +++ b/libraries/WiFi/src/WiFiGeneric.cpp @@ -73,13 +73,13 @@ static void _network_event_task(void * arg){ WiFiGenericClass::_eventCallback(arg, data->sys_event, NULL); } free(data); - } + } } vTaskDelete(NULL); _network_event_task_handle = NULL; } -static esp_err_t _network_event_cb(void *arg, system_event_t *event){ +static esp_err_t _network_event_cb(void *arg, system_event_t *event){ system_prov_event_t *sys_prov_data = (system_prov_event_t *)malloc(sizeof(system_prov_event_t)); if(sys_prov_data == NULL) { return ESP_FAIL; @@ -368,7 +368,7 @@ const char * system_event_reasons[] = { "UNSPECIFIED", "AUTH_EXPIRE", "AUTH_LEAV esp_err_t WiFiGenericClass::_eventCallback(void *arg, system_event_t *event, wifi_prov_event_t *prov_event) { if(WiFi.isProvEnabled()) { - wifi_prov_mgr_event_handler(arg,event); + wifi_prov_mgr_event_handler(arg,event); } if(event->event_id < 26) { log_d("Event: %d - %s", event->event_id, system_event_names[event->event_id]); @@ -465,7 +465,7 @@ esp_err_t WiFiGenericClass::_eventCallback(void *arg, system_event_t *event, wif setStatusBits(ETH_CONNECTED_BIT | ETH_HAS_IP6_BIT); } } - + for(uint32_t i = 0; i < cbEventList.size(); i++) { WiFiEventCbList_t entry = cbEventList[i]; if(entry.cb || entry.fcb || entry.scb) { @@ -734,6 +734,7 @@ int WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult) waitStatusBits(WIFI_DNS_DONE_BIT, 15000); //real internal timeout in lwip library is 14[s] clearStatusBits(WIFI_DNS_DONE_BIT); } + clearStatusBits(WIFI_DNS_DONE_BIT); setStatusBits(WIFI_DNS_IDLE_BIT); if((uint32_t)aResult == 0){ log_e("DNS Failed for %s", aHostname); @@ -752,7 +753,7 @@ IPAddress WiFiGenericClass::calculateNetworkID(IPAddress ip, IPAddress subnet) { IPAddress WiFiGenericClass::calculateBroadcast(IPAddress ip, IPAddress subnet) { IPAddress broadcastIp; - + for (int i = 0; i < 4; i++) broadcastIp[i] = ~subnet[i] | ip[i]; diff --git a/libraries/WiFiClientSecure/src/ssl_client.cpp b/libraries/WiFiClientSecure/src/ssl_client.cpp index efe0a4494e2..77141e2cc34 100644 --- a/libraries/WiFiClientSecure/src/ssl_client.cpp +++ b/libraries/WiFiClientSecure/src/ssl_client.cpp @@ -213,11 +213,22 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p unsigned long handshake_start_time=millis(); while ((ret = mbedtls_ssl_handshake(&ssl_client->ssl_ctx)) != 0) { if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) { + // ++++++++++ ADDED TO FIX MEMORY LEAK ON FAILED CONNECTION ++++++++++ + if (cli_key != NULL) mbedtls_pk_free(&ssl_client->client_key); + if (rootCABuff != NULL) mbedtls_x509_crt_free(&ssl_client->ca_cert); + if (cli_cert != NULL) mbedtls_x509_crt_free(&ssl_client->client_cert); + // ++++++++++ END ++++++++++ return handle_error(ret); } - if((millis()-handshake_start_time)>ssl_client->handshake_timeout) - return -1; - vTaskDelay(2);//2 ticks + if ((millis()-handshake_start_time) > ssl_client->handshake_timeout) { + // ++++++++++ ADDED TO FIX MEMORY LEAK ON FAILED CONNECTION ++++++++++ + if (cli_key != NULL) mbedtls_pk_free(&ssl_client->client_key); + if (rootCABuff != NULL) mbedtls_x509_crt_free(&ssl_client->ca_cert); + if (cli_cert != NULL) mbedtls_x509_crt_free(&ssl_client->client_cert); + // ++++++++++ END ++++++++++ + return -1; + } + vTaskDelay(2);//2 ticks } @@ -241,7 +252,7 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p } else { log_v("Certificate verified."); } - + if (rootCABuff != NULL) { mbedtls_x509_crt_free(&ssl_client->ca_cert); } @@ -252,7 +263,7 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p if (cli_key != NULL) { mbedtls_pk_free(&ssl_client->client_key); - } + } log_v("Free internal heap after TLS %u", ESP.getFreeHeap());