Skip to content

Commit c8497da

Browse files
Jeroen88earlephilhower
Jeroen88
authored andcommitted
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
1 parent 1b1b0a2 commit c8497da

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino

+6-3
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ void loop() {
4141
// wait for WiFi connection
4242
if ((WiFiMulti.run() == WL_CONNECTED)) {
4343

44-
BearSSL::WiFiClientSecure client;
45-
client.setFingerprint(fingerprint);
44+
BearSSL::WiFiClientSecure *client = new BearSSL::WiFiClientSecure;
45+
46+
client->setFingerprint(fingerprint);
4647

4748
HTTPClient https;
4849

4950
Serial.print("[HTTPS] begin...\n");
50-
if (https.begin(client, "https://jigsaw.w3.org/HTTP/connection.html")) { // HTTPS
51+
if (https.begin(*client, "https://jigsaw.w3.org/HTTP/connection.html")) { // HTTPS
5152

5253

5354
Serial.print("[HTTPS] GET...\n");
@@ -72,6 +73,8 @@ void loop() {
7273
} else {
7374
Serial.printf("[HTTPS] Unable to connect\n");
7475
}
76+
77+
delete client;
7578
}
7679

7780
delay(10000);

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ HTTPClient::HTTPClient()
123123
HTTPClient::~HTTPClient()
124124
{
125125
if(_client) {
126-
_client->stop();
126+
DEBUG_HTTPCLIENT("[HTTP-Client][~HTTPClient] end() not called before destruction of HTTPClient\n");
127127
}
128128
if(_currentHeaders) {
129129
delete[] _currentHeaders;
@@ -196,7 +196,7 @@ bool HTTPClient::begin(WiFiClient &client, String host, uint16_t port, String ur
196196
#ifdef HTTPCLIENT_1_1_COMPATIBLE
197197
bool HTTPClient::begin(String url, String httpsFingerprint)
198198
{
199-
if(_client) _canReuse = false;
199+
_canReuse = false;
200200
end();
201201

202202
_port = 443;
@@ -214,7 +214,7 @@ bool HTTPClient::begin(String url, String httpsFingerprint)
214214

215215
bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20])
216216
{
217-
if(_client) _canReuse = false;
217+
_canReuse = false;
218218
end();
219219

220220
_port = 443;
@@ -237,7 +237,7 @@ bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20])
237237
*/
238238
bool HTTPClient::begin(String url)
239239
{
240-
if(_client) _canReuse = false;
240+
_canReuse = false;
241241
end();
242242

243243
_port = 80;
@@ -299,7 +299,7 @@ bool HTTPClient::beginInternal(String url, const char* expectedProtocol)
299299
#ifdef HTTPCLIENT_1_1_COMPATIBLE
300300
bool HTTPClient::begin(String host, uint16_t port, String uri)
301301
{
302-
if(_client) _canReuse = false;
302+
_canReuse = false;
303303
end();
304304

305305
clear();
@@ -325,7 +325,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, bool https, Strin
325325

326326
bool HTTPClient::begin(String host, uint16_t port, String uri, String httpsFingerprint)
327327
{
328-
if(_client) _canReuse = false;
328+
_canReuse = false;
329329
end();
330330

331331
clear();
@@ -343,7 +343,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, String httpsFinge
343343

344344
bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t httpsFingerprint[20])
345345
{
346-
if(_client) _canReuse = false;
346+
_canReuse = false;
347347
end();
348348

349349
clear();
@@ -367,6 +367,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t htt
367367
*/
368368
void HTTPClient::end(void)
369369
{
370+
_canReuse = false;
370371
disconnect();
371372
clear();
372373
}

0 commit comments

Comments
 (0)