Skip to content

Commit

Permalink
Make sure _client is deleted returning from download function
Browse files Browse the repository at this point in the history
  • Loading branch information
pennam committed Oct 16, 2023
1 parent 2f84091 commit 7d34bf7
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Arduino_ESP32_OTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
if (!_client->connect(url.host_.c_str(), port))
{
DEBUG_ERROR("%s: Connection failure with OTA storage server %s", __FUNCTION__, url.host_.c_str());
delete _client;
_client = nullptr;
return static_cast<int>(Error::ServerConnectError);
}

Expand Down Expand Up @@ -176,6 +178,8 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
if (!is_header_complete)
{
DEBUG_ERROR("%s: Error receiving HTTP header %s", __FUNCTION__, is_http_header_timeout ? "(timeout)":"");
delete _client;
_client = nullptr;
return static_cast<int>(Error::HttpHeaderError);
}

Expand Down Expand Up @@ -206,6 +210,8 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
if (!content_length_ptr)
{
DEBUG_ERROR("%s: Failure to extract content length from http header", __FUNCTION__);
delete _client;
_client = nullptr;
return static_cast<int>(Error::ParseHttpHeader);
}
/* Find start of numerical value. */
Expand Down Expand Up @@ -238,12 +244,16 @@ int Arduino_ESP32_OTA::download(const char * ota_url)

/* ... then check if OTA header length field matches HTTP content length... */
if (_ota_header.header.len != (content_length_val - sizeof(_ota_header.header.len) - sizeof(_ota_header.header.crc32))) {
delete _client;
_client = nullptr;
return static_cast<int>(Error::OtaHeaderLength);
}

/* ... and OTA magic number */
if (_ota_header.header.magic_number != ARDUINO_ESP32_OTA_MAGIC)
{
delete _client;
_client = nullptr;
return static_cast<int>(Error::OtaHeaterMagicNumber);
}

Expand All @@ -255,9 +265,13 @@ int Arduino_ESP32_OTA::download(const char * ota_url)

if(_ota_size <= content_length_val - sizeof(_ota_header))
{
delete _client;
_client = nullptr;
return static_cast<int>(Error::OtaDownload);
}

delete _client;
_client = nullptr;
return _ota_size;
}

Expand Down

0 comments on commit 7d34bf7

Please sign in to comment.