From dda74174c55f30e108fb7391237ae38728bdf056 Mon Sep 17 00:00:00 2001 From: Adriano Cunha <35786489+adrcunha@users.noreply.github.com> Date: Thu, 21 Dec 2023 12:31:24 -0800 Subject: [PATCH] Avoid NPE and "multi-deinitialization" of ArduinoOTA 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). --- libraries/ArduinoOTA/ArduinoOTA.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/libraries/ArduinoOTA/ArduinoOTA.cpp b/libraries/ArduinoOTA/ArduinoOTA.cpp index 1ef83b895f..4ee31e3751 100644 --- a/libraries/ArduinoOTA/ArduinoOTA.cpp +++ b/libraries/ArduinoOTA/ArduinoOTA.cpp @@ -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();