Skip to content

Commit

Permalink
Avoid NPE and "multi-deinitialization" of ArduinoOTA (#9058)
Browse files Browse the repository at this point in the history
Avoid a null pointer exception when ArduinoOTA.end() is called more than once and thus the UDP socket is already freed.

Also avoid unnecessary teardown if the class is not initialized yet (for example, begin() wasn't called yet, or end() is called multiple times).
  • Loading branch information
adrcunha authored Jan 18, 2024
1 parent d5eb265 commit b3b9276
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions libraries/ArduinoOTA/ArduinoOTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,14 @@ void ArduinoOTAClass::_runUpdate() {
}

void ArduinoOTAClass::end() {
if (!_initialized)
return;

_initialized = false;
_udp_ota->unref();
_udp_ota = 0;
if(_udp_ota){
_udp_ota->unref();
_udp_ota = 0;
}
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_MDNS)
if(_useMDNS){
MDNS.end();
Expand Down

0 comments on commit b3b9276

Please sign in to comment.