From 872f3178fd0888858c83ea4d4d303a7bc21c9aea Mon Sep 17 00:00:00 2001 From: Piotr Gniado Date: Tue, 4 Nov 2025 18:40:47 +0100 Subject: [PATCH 1/2] fix(eth): Del mac and phy resources - Delete _mac and _phy resources when initialization of ETH fails - Delete _mac and _phy resources when calling ETHClass::end() and ETH is not fully initialized --- libraries/Ethernet/src/ETH.cpp | 40 +++++++++++++++++++++++++--------- libraries/Ethernet/src/ETH.h | 2 ++ 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/libraries/Ethernet/src/ETH.cpp b/libraries/Ethernet/src/ETH.cpp index 59dd102cced..d6d76975736 100644 --- a/libraries/Ethernet/src/ETH.cpp +++ b/libraries/Ethernet/src/ETH.cpp @@ -281,6 +281,7 @@ bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, i _mac = esp_eth_mac_new_esp32(&mac_config, ð_mac_config); if (_mac == NULL) { log_e("esp_eth_mac_new_esp32 failed"); + _delMacAndPhy(); return false; } @@ -305,6 +306,7 @@ bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, i } if (_phy == NULL) { log_e("esp_eth_phy_new failed"); + _delMacAndPhy(); return false; } @@ -738,15 +740,29 @@ bool ETHClass::beginSPI( return false; } + if (_mac == NULL) { + log_e("esp_eth_mac_new failed"); + _delMacAndPhy(); + return false; + } + + if (_phy == NULL) { + log_e("esp_eth_phy_new failed"); + _delMacAndPhy(); + return false; + } + // Init Ethernet driver to default and install it esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(_mac, _phy); ret = esp_eth_driver_install(ð_config, &_eth_handle); if (ret != ESP_OK) { log_e("SPI Ethernet driver install failed: %d", ret); + _delMacAndPhy(); return false; } if (_eth_handle == NULL) { log_e("esp_eth_driver_install failed! eth_handle is NULL"); + _delMacAndPhy(); return false; } @@ -923,6 +939,18 @@ static bool empty_ethDetachBus(void *bus_pointer) { return true; } +void ETHClass::_delMacAndPhy() { + if (_mac != NULL) { + _mac->del(_mac); + _mac = NULL; + } + + if (_phy != NULL) { + _phy->del(_phy); + _phy = NULL; + } +} + void ETHClass::end(void) { Network.removeEvent(_eth_connected_event_handle); @@ -954,18 +982,10 @@ void ETHClass::end(void) { return; } _eth_handle = NULL; - //delete mac - if (_mac != NULL) { - _mac->del(_mac); - _mac = NULL; - } - //delete phy - if (_phy != NULL) { - _phy->del(_phy); - _phy = NULL; - } } + _delMacAndPhy(); + if (_eth_ev_instance != NULL) { bool do_not_unreg_ev_handler = false; for (int i = 0; i < NUM_SUPPORTED_ETH_PORTS; ++i) { diff --git a/libraries/Ethernet/src/ETH.h b/libraries/Ethernet/src/ETH.h index f609c651102..ceeaf789848 100644 --- a/libraries/Ethernet/src/ETH.h +++ b/libraries/Ethernet/src/ETH.h @@ -271,6 +271,8 @@ class ETHClass : public NetworkInterface { bool _setLinkSpeed(uint16_t speed); bool _setAutoNegotiation(bool on); + void _delMacAndPhy(); + friend class EthernetClass; // to access beginSPI }; From cf3d316770cc85f2c74930452992ea0479627271 Mon Sep 17 00:00:00 2001 From: Me No Dev Date: Wed, 5 Nov 2025 13:49:16 +0200 Subject: [PATCH 2/2] fx(pr): Remove call to _delMacAndPhy on MAC creation failure --- libraries/Ethernet/src/ETH.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/libraries/Ethernet/src/ETH.cpp b/libraries/Ethernet/src/ETH.cpp index d6d76975736..e1223ea446f 100644 --- a/libraries/Ethernet/src/ETH.cpp +++ b/libraries/Ethernet/src/ETH.cpp @@ -281,7 +281,6 @@ bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, i _mac = esp_eth_mac_new_esp32(&mac_config, ð_mac_config); if (_mac == NULL) { log_e("esp_eth_mac_new_esp32 failed"); - _delMacAndPhy(); return false; }