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.)
diff --git a/src/platform/Darwin/ConfigurationManagerImpl.cpp b/src/platform/Darwin/ConfigurationManagerImpl.cpp
index b3cd31b..10a2ad0 100644
--- a/src/platform/Darwin/ConfigurationManagerImpl.cpp
+++ b/src/platform/Darwin/ConfigurationManagerImpl.cpp
@@ -93,7 +93,7 @@
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 @@
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 @@
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);
}