Skip to content

Commit

Permalink
Darwin: Fix error handling in GetMACAddressFromInterfaces (#32658)
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
ksperling-apple authored and pull[bot] committed Apr 19, 2024
1 parent fee247a commit 545025f
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions src/platform/Darwin/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,20 @@ 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;
io_object_t controllerService = 0;

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;
Expand All @@ -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);
}
Expand Down

0 comments on commit 545025f

Please sign in to comment.