From c8497da6766543c0e0c1dc130ab54c3d722ea017 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Fri, 12 Oct 2018 00:34:25 +0200 Subject: [PATCH] ESP8266httpClient crash-on-destructor bugfix (#5220) * Removed _client->stop() from destructor; some minor changes * Changed BasicHttpsClient.ino to allocate BearSSL::WiFiClientSecure object on the heap in stead of stack --- .../BasicHttpsClient/BasicHttpsClient.ino | 9 ++++++--- .../ESP8266HTTPClient/src/ESP8266HTTPClient.cpp | 15 ++++++++------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino index 3c5c0ed5b..e0eb2879b 100644 --- a/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino +++ b/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino @@ -41,13 +41,14 @@ void loop() { // wait for WiFi connection if ((WiFiMulti.run() == WL_CONNECTED)) { - BearSSL::WiFiClientSecure client; - client.setFingerprint(fingerprint); + BearSSL::WiFiClientSecure *client = new BearSSL::WiFiClientSecure; + + client->setFingerprint(fingerprint); HTTPClient https; Serial.print("[HTTPS] begin...\n"); - if (https.begin(client, "https://jigsaw.w3.org/HTTP/connection.html")) { // HTTPS + if (https.begin(*client, "https://jigsaw.w3.org/HTTP/connection.html")) { // HTTPS Serial.print("[HTTPS] GET...\n"); @@ -72,6 +73,8 @@ void loop() { } else { Serial.printf("[HTTPS] Unable to connect\n"); } + + delete client; } delay(10000); diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index 034a815b9..1872ed08c 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -123,7 +123,7 @@ HTTPClient::HTTPClient() HTTPClient::~HTTPClient() { if(_client) { - _client->stop(); + DEBUG_HTTPCLIENT("[HTTP-Client][~HTTPClient] end() not called before destruction of HTTPClient\n"); } if(_currentHeaders) { delete[] _currentHeaders; @@ -196,7 +196,7 @@ bool HTTPClient::begin(WiFiClient &client, String host, uint16_t port, String ur #ifdef HTTPCLIENT_1_1_COMPATIBLE bool HTTPClient::begin(String url, String httpsFingerprint) { - if(_client) _canReuse = false; + _canReuse = false; end(); _port = 443; @@ -214,7 +214,7 @@ bool HTTPClient::begin(String url, String httpsFingerprint) bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20]) { - if(_client) _canReuse = false; + _canReuse = false; end(); _port = 443; @@ -237,7 +237,7 @@ bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20]) */ bool HTTPClient::begin(String url) { - if(_client) _canReuse = false; + _canReuse = false; end(); _port = 80; @@ -299,7 +299,7 @@ bool HTTPClient::beginInternal(String url, const char* expectedProtocol) #ifdef HTTPCLIENT_1_1_COMPATIBLE bool HTTPClient::begin(String host, uint16_t port, String uri) { - if(_client) _canReuse = false; + _canReuse = false; end(); clear(); @@ -325,7 +325,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, bool https, Strin bool HTTPClient::begin(String host, uint16_t port, String uri, String httpsFingerprint) { - if(_client) _canReuse = false; + _canReuse = false; end(); clear(); @@ -343,7 +343,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, String httpsFinge bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t httpsFingerprint[20]) { - if(_client) _canReuse = false; + _canReuse = false; end(); clear(); @@ -367,6 +367,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t htt */ void HTTPClient::end(void) { + _canReuse = false; disconnect(); clear(); }