Skip to content

Commit 4b51726

Browse files
committed
Chore (macOS): refactors code for better resource management
1 parent 98a6596 commit 4b51726

File tree

3 files changed

+32
-49
lines changed

3 files changed

+32
-49
lines changed

src/detection/bios/bios_apple.c

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,69 +5,55 @@
55

66
const char* ffDetectBios(FFBiosResult* bios)
77
{
8-
io_registry_entry_t registryEntry;
9-
108
#ifndef __aarch64__
119

1210
//https://github.com/osquery/osquery/blob/master/osquery/tables/system/darwin/smbios_tables.cpp
1311
//For Intel
14-
if((registryEntry = IORegistryEntryFromPath(MACH_PORT_NULL, "IODeviceTree:/rom")))
15-
{
16-
CFMutableDictionaryRef properties;
17-
if(IORegistryEntryCreateCFProperties(registryEntry, &properties, kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess)
18-
{
19-
IOObjectRelease(registryEntry);
20-
return "IORegistryEntryCreateCFProperties(registryEntry) failed";
21-
}
12+
FF_IOOBJECT_AUTO_RELEASE io_registry_entry_t deviceRom = IORegistryEntryFromPath(MACH_PORT_NULL, "IODeviceTree:/rom");
13+
if (!deviceRom)
14+
return "IODeviceTree:/rom not found";
2215

23-
ffCfDictGetString(properties, CFSTR("vendor"), &bios->vendor);
24-
ffCfDictGetString(properties, CFSTR("version"), &bios->version);
25-
ffCfDictGetString(properties, CFSTR("release-date"), &bios->date);
26-
ffStrbufSetStatic(&bios->type, "UEFI");
16+
FF_CFTYPE_AUTO_RELEASE CFMutableDictionaryRef deviceRomProps = NULL;
17+
if(IORegistryEntryCreateCFProperties(deviceRom, &deviceRomProps, kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess)
18+
return "IORegistryEntryCreateCFProperties(deviceRom) failed";
2719

28-
CFRelease(properties);
29-
IOObjectRelease(registryEntry);
30-
return NULL;
31-
}
20+
ffCfDictGetString(deviceRomProps, CFSTR("vendor"), &bios->vendor);
21+
ffCfDictGetString(deviceRomProps, CFSTR("version"), &bios->version);
22+
ffCfDictGetString(deviceRomProps, CFSTR("release-date"), &bios->date);
23+
ffStrbufSetStatic(&bios->type, "UEFI");
3224

3325
#else
3426

3527
//For arm64
36-
if((registryEntry = IORegistryEntryFromPath(MACH_PORT_NULL, "IODeviceTree:/")))
37-
{
38-
CFMutableDictionaryRef properties;
39-
if(IORegistryEntryCreateCFProperties(registryEntry, &properties, kCFAllocatorDefault, kNilOptions) == kIOReturnSuccess)
40-
{
41-
ffCfDictGetString(properties, CFSTR("manufacturer"), &bios->vendor);
42-
ffCfDictGetString(properties, CFSTR("time-stamp"), &bios->date);
43-
CFRelease(properties);
44-
}
45-
IOObjectRelease(registryEntry);
46-
}
28+
FF_IOOBJECT_AUTO_RELEASE io_registry_entry_t device = IORegistryEntryFromPath(MACH_PORT_NULL, "IODeviceTree:/");
29+
if (!device)
30+
return "IODeviceTree:/ not found";
31+
32+
FF_CFTYPE_AUTO_RELEASE CFMutableDictionaryRef deviceProps = NULL;
33+
if(IORegistryEntryCreateCFProperties(device, &deviceProps, kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess)
34+
return "IORegistryEntryCreateCFProperties(device) failed";
35+
36+
ffCfDictGetString(deviceProps, CFSTR("manufacturer"), &bios->vendor);
37+
ffCfDictGetString(deviceProps, CFSTR("time-stamp"), &bios->date);
4738

48-
if((registryEntry = IORegistryEntryFromPath(MACH_PORT_NULL, "IODeviceTree:/chosen")))
39+
FF_IOOBJECT_AUTO_RELEASE io_registry_entry_t deviceChosen = IORegistryEntryFromPath(MACH_PORT_NULL, "IODeviceTree:/chosen");
40+
if (deviceChosen)
4941
{
50-
CFMutableDictionaryRef properties;
51-
if(IORegistryEntryCreateCFProperties(registryEntry, &properties, kCFAllocatorDefault, kNilOptions) == kIOReturnSuccess)
42+
FF_CFTYPE_AUTO_RELEASE CFStringRef systemFirmWareVersion = IORegistryEntryCreateCFProperty(deviceChosen, CFSTR("system-firmware-version"), kCFAllocatorDefault, kNilOptions);
43+
if (systemFirmWareVersion)
5244
{
53-
ffCfDictGetString(properties, CFSTR("system-firmware-version"), &bios->version);
45+
ffCfStrGetString(systemFirmWareVersion, &bios->version);
5446
uint32_t index = ffStrbufFirstIndexC(&bios->version, '-');
5547
if (index != bios->version.length)
5648
{
5749
ffStrbufAppendNS(&bios->type, index, bios->version.chars);
5850
ffStrbufRemoveSubstr(&bios->version, 0, index + 1);
5951
}
60-
else
61-
{
62-
ffStrbufSetStatic(&bios->type, "iBoot");
63-
}
64-
CFRelease(properties);
6552
}
66-
IOObjectRelease(registryEntry);
67-
return NULL;
6853
}
69-
54+
if (!bios->type.length)
55+
ffStrbufSetStatic(&bios->type, "iBoot");
7056
#endif
7157

72-
return "Failed to query bios info";
58+
return NULL;
7359
}

src/detection/cpu/cpu_apple.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static const char* detectFrequency(FFCPUResult* cpu)
3939

4040
FF_IOOBJECT_AUTO_RELEASE io_registry_entry_t entryDevice = IOServiceGetMatchingService(MACH_PORT_NULL, IOServiceNameMatching("pmgr"));
4141
if (!entryDevice)
42-
return "IOServiceGetMatchingServices() failed";
42+
return "IOServiceGetMatchingService() failed";
4343

4444
if (!IOObjectConformsTo(entryDevice, "AppleARMIODevice"))
4545
return "\"pmgr\" should conform to \"AppleARMIODevice\"";

src/util/apple/smc_temps.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include "fastfetch.h"
21
#include "smc_temps.h"
2+
#include "util/apple/cf_helpers.h"
33
#include "util/stringUtils.h"
44

55
#include <stdint.h>
@@ -137,14 +137,11 @@ static const char *smcReadSmcVal(io_connect_t conn, const UInt32Char_t key, SmcV
137137

138138
static const char *smcOpen(io_connect_t *conn)
139139
{
140-
io_object_t device = IOServiceGetMatchingService(MACH_PORT_NULL, IOServiceMatching("AppleSMC"));
140+
FF_IOOBJECT_AUTO_RELEASE io_object_t device = IOServiceGetMatchingService(MACH_PORT_NULL, IOServiceMatching("AppleSMC"));
141141
if (!device)
142142
return "No SMC device found";
143143

144-
kern_return_t result = IOServiceOpen(device, mach_task_self(), 0, conn);
145-
IOObjectRelease(device);
146-
147-
if (result != kIOReturnSuccess)
144+
if (IOServiceOpen(device, mach_task_self(), 0, conn) != kIOReturnSuccess)
148145
return "IOServiceOpen() failed";
149146

150147
return NULL;

0 commit comments

Comments
 (0)