@@ -76,7 +76,16 @@ Arduino_ESP32_OTA::Error Arduino_ESP32_OTA::begin()
7676 DEBUG_ERROR (" %s: board is not capable to perform OTA" , __FUNCTION__);
7777 return Error::NoOtaStorage;
7878 }
79-
79+
80+ /* initialize private variables */
81+ _ota_size = 0 ;
82+ _ota_header = {0 };
83+
84+ if (Update.isRunning ()) {
85+ Update.abort ();
86+ DEBUG_DEBUG (" %s: Aborting running update" , __FUNCTION__);
87+ }
88+
8089 if (!Update.begin (UPDATE_SIZE_UNKNOWN)) {
8190 DEBUG_ERROR (" %s: failed to initialize flash update" , __FUNCTION__);
8291 return Error::OtaStorageInit;
@@ -147,6 +156,8 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
147156 if (!_client->connect (url.host_ .c_str (), port))
148157 {
149158 DEBUG_ERROR (" %s: Connection failure with OTA storage server %s" , __FUNCTION__, url.host_ .c_str ());
159+ delete _client;
160+ _client = nullptr ;
150161 return static_cast <int >(Error::ServerConnectError);
151162 }
152163
@@ -177,6 +188,8 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
177188 if (!is_header_complete)
178189 {
179190 DEBUG_ERROR (" %s: Error receiving HTTP header %s" , __FUNCTION__, is_http_header_timeout ? " (timeout)" :" " );
191+ delete _client;
192+ _client = nullptr ;
180193 return static_cast <int >(Error::HttpHeaderError);
181194 }
182195
@@ -207,6 +220,8 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
207220 if (!content_length_ptr)
208221 {
209222 DEBUG_ERROR (" %s: Failure to extract content length from http header" , __FUNCTION__);
223+ delete _client;
224+ _client = nullptr ;
210225 return static_cast <int >(Error::ParseHttpHeader);
211226 }
212227 /* Find start of numerical value. */
@@ -234,17 +249,23 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
234249
235250 /* ... check for header download timeout ... */
236251 if (is_ota_header_timeout) {
252+ delete _client;
253+ _client = nullptr ;
237254 return static_cast <int >(Error::OtaHeaderTimeout);
238255 }
239256
240257 /* ... then check if OTA header length field matches HTTP content length... */
241258 if (_ota_header.header .len != (content_length_val - sizeof (_ota_header.header .len ) - sizeof (_ota_header.header .crc32 ))) {
259+ delete _client;
260+ _client = nullptr ;
242261 return static_cast <int >(Error::OtaHeaderLength);
243262 }
244263
245264 /* ... and OTA magic number */
246265 if (_ota_header.header .magic_number != ARDUINO_ESP32_OTA_MAGIC)
247266 {
267+ delete _client;
268+ _client = nullptr ;
248269 return static_cast <int >(Error::OtaHeaterMagicNumber);
249270 }
250271
@@ -256,9 +277,13 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
256277
257278 if (_ota_size <= content_length_val - sizeof (_ota_header))
258279 {
280+ delete _client;
281+ _client = nullptr ;
259282 return static_cast <int >(Error::OtaDownload);
260283 }
261284
285+ delete _client;
286+ _client = nullptr ;
262287 return _ota_size;
263288}
264289
0 commit comments