From c644692504276b7e59a0865f09e16cfc666ff117 Mon Sep 17 00:00:00 2001 From: Karsten Sperling <113487422+ksperling-apple@users.noreply.github.com> Date: Thu, 21 Mar 2024 17:03:08 +1300 Subject: [PATCH] Darwin: Fix error handling in GetMACAddressFromInterfaces (#32658) Correctly return CHIP_ERROR_NOT_FOUND when there is no active, primary interface to get the MAC address from. Also tidy up management of IOKit objects. (Use of IOObjectGetRetainCount is completely wrong here.) --- .../Darwin/ConfigurationManagerImpl.cpp | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/platform/Darwin/ConfigurationManagerImpl.cpp b/src/platform/Darwin/ConfigurationManagerImpl.cpp index b3cd31b2efde3a..10a2ad0a8eb600 100644 --- a/src/platform/Darwin/ConfigurationManagerImpl.cpp +++ b/src/platform/Darwin/ConfigurationManagerImpl.cpp @@ -93,7 +93,7 @@ CHIP_ERROR FindInterfaces(io_iterator_t * primaryInterfaceIterator) CHIP_ERROR GetMACAddressFromInterfaces(io_iterator_t primaryInterfaceIterator, uint8_t * buf) { - CHIP_ERROR err = CHIP_NO_ERROR; + CHIP_ERROR err = CHIP_ERROR_NOT_FOUND; kern_return_t kernResult; io_object_t interfaceService; @@ -101,12 +101,12 @@ CHIP_ERROR GetMACAddressFromInterfaces(io_iterator_t primaryInterfaceIterator, u while ((interfaceService = IOIteratorNext(primaryInterfaceIterator))) { - CFTypeRef MACAddressAsCFData = nullptr; - CFTypeRef linkStatusAsCFNumber = nullptr; - kernResult = IORegistryEntryGetParentEntry(interfaceService, kIOServicePlane, &controllerService); + kernResult = IORegistryEntryGetParentEntry(interfaceService, kIOServicePlane, &controllerService); + IOObjectRelease(interfaceService); VerifyOrExit(KERN_SUCCESS == kernResult, err = CHIP_ERROR_INTERNAL); - linkStatusAsCFNumber = IORegistryEntryCreateCFProperty(controllerService, CFSTR(kIOLinkStatus), kCFAllocatorDefault, 0); + CFTypeRef linkStatusAsCFNumber = + IORegistryEntryCreateCFProperty(controllerService, CFSTR(kIOLinkStatus), kCFAllocatorDefault, 0); VerifyOrExit(linkStatusAsCFNumber != nullptr, err = CHIP_ERROR_INTERNAL); uint64_t linkStatus; @@ -115,27 +115,21 @@ CHIP_ERROR GetMACAddressFromInterfaces(io_iterator_t primaryInterfaceIterator, u if ((linkStatus & kIONetworkLinkValid) && (linkStatus & kIONetworkLinkActive)) { - MACAddressAsCFData = IORegistryEntryCreateCFProperty(controllerService, CFSTR(kIOMACAddress), kCFAllocatorDefault, 0); + CFTypeRef MACAddressAsCFData = + IORegistryEntryCreateCFProperty(controllerService, CFSTR(kIOMACAddress), kCFAllocatorDefault, 0); VerifyOrExit(MACAddressAsCFData != nullptr, err = CHIP_ERROR_INTERNAL); CFDataGetBytes((CFDataRef) MACAddressAsCFData, CFRangeMake(0, kIOEthernetAddressSize), buf); CFRelease(MACAddressAsCFData); + ExitNow(err = CHIP_NO_ERROR); } - kernResult = IOObjectRelease(controllerService); - VerifyOrExit(KERN_SUCCESS == kernResult, err = CHIP_ERROR_INTERNAL); - - kernResult = IOObjectRelease(interfaceService); - VerifyOrExit(KERN_SUCCESS == kernResult, err = CHIP_ERROR_INTERNAL); + IOObjectRelease(controllerService); + controllerService = 0; } exit: - if (IOObjectGetRetainCount(interfaceService)) - { - IOObjectRelease(interfaceService); - } - - if (IOObjectGetRetainCount(controllerService)) + if (controllerService) { IOObjectRelease(controllerService); }