Skip to content

Commit 7562e05

Browse files
committed
Chore: fix a lot code smell
1 parent bf17f2b commit 7562e05

23 files changed

+88
-50
lines changed

src/detection/battery/battery_nbsd.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const char* ffDetectBattery(FF_MAYBE_UNUSED FFBatteryOptions* options, FFlist* r
9292
else if (dischargeRate)
9393
{
9494
ffStrbufAppendS(&battery->status, "Discharging, ");
95-
battery->timeRemaining = (int32_t)((double)curr / dischargeRate * 3600)
95+
battery->timeRemaining = (int32_t)((double)curr / dischargeRate * 3600);
9696
}
9797
if (critical)
9898
ffStrbufAppendS(&battery->status, "Critical, ");

src/detection/bios/bios_bsd.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const char* ffDetectBios(FFBiosResult* result)
2525
{
2626
ffStrbufSetStatic(&result->type,
2727
ffPathExists("/dev/efi" /*efidev*/, FF_PATHTYPE_FILE) ||
28-
ffPathExists("/boot/efi/efi/" /*efi partition*/, FF_PATHTYPE_DIRECTORY)
28+
ffPathExists("/boot/efi/efi/" /*efi partition. Note /boot/efi exists on BIOS system*/, FF_PATHTYPE_DIRECTORY)
2929
? "UEFI" : "BIOS");
3030
}
3131
}

src/detection/bios/bios_linux.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include <stdlib.h>
66

7-
const char *ffDetectBios(FFBiosResult *bios)
7+
const char* ffDetectBios(FFBiosResult* bios)
88
{
99
ffGetSmbiosValue("/sys/devices/virtual/dmi/id/bios_date", "/sys/class/dmi/id/bios_date", &bios->date);
1010
ffGetSmbiosValue("/sys/devices/virtual/dmi/id/bios_release", "/sys/class/dmi/id/bios_release", &bios->release);

src/detection/bios/bios_windows.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,9 @@ const char* ffDetectBios(FFBiosResult* bios)
9494
default: break;
9595
}
9696
}
97-
#elif __HAIKU__
98-
// Currently SMBIOS detection is supported in legency BIOS only
97+
#elif __HAIKU__ || __OpenBSD__
98+
// Currently SMBIOS detection is supported in legancy BIOS only
9999
ffStrbufSetStatic(&bios->type, "BIOS");
100-
#elif __OpenBSD__
101-
FF_AUTO_CLOSE_FD int fd = open("/dev/efi", O_RDONLY);
102-
ffStrbufSetStatic(&bios->type, fd >= 0 ? "UEFI" : "BIOS");
103100
#endif
104101

105102
return NULL;

src/detection/bluetooth/bluetooth_bsd.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ const char* ffDetectBluetooth(FF_MAYBE_UNUSED FFBluetoothOptions* options, FF_MA
2121
if (bt_devenum((void*) enumDev, devices) < 0)
2222
return "bt_devenum() failed";
2323

24-
return 0;
24+
return NULL;
2525
}

src/detection/bluetoothradio/bluetoothradio_apple.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
#import <IOBluetooth/IOBluetooth.h>
55

6-
// For some reason the official declaration of IOBluetoothHostController don't include property `controllers`
6+
// For some reason the official declaration of IOBluetoothHostController doesn't include property `controllers`
77
@interface IOBluetoothHostController()
88
+ (id)controllers;
99
@end
1010

11-
const char* ffDetectBluetoothRadio(FFlist* devices /* FFBluetoothResult */)
11+
const char* ffDetectBluetoothRadio(FFlist* devices /* FFBluetoothRadioResult */)
1212
{
1313
NSArray<IOBluetoothHostController*>* ctrls = IOBluetoothHostController.controllers;
1414
if(!ctrls)

src/detection/bluetoothradio/bluetoothradio_windows.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const char* ffDetectBluetoothRadio(FFlist* devices /* FFBluetoothRadioResult */)
8383
ffStrbufInitS(&device->name, blri.localInfo.name);
8484

8585
BLUETOOTH_ADDRESS_STRUCT addr = { .ullLong = blri.localInfo.address };
86-
ffStrbufInitF(&device->address, "%02x:%02x:%02x:%02x:%02x:%02x",
86+
ffStrbufInitF(&device->address, "%02X:%02X:%02X:%02X:%02X:%02X",
8787
addr.rgBytes[5],
8888
addr.rgBytes[4],
8989
addr.rgBytes[3],
@@ -97,6 +97,8 @@ const char* ffDetectBluetoothRadio(FFlist* devices /* FFBluetoothRadioResult */)
9797
device->enabled = true;
9898
device->connectable = ffBluetoothIsConnectable(hRadio);
9999
device->discoverable = ffBluetoothIsDiscoverable(hRadio);
100+
101+
CloseHandle(hRadio);
100102
} while (ffBluetoothFindNextRadio(hFind, &hRadio));
101103

102104
ffBluetoothFindRadioClose(hFind);

src/detection/bootmgr/bootmgr_windows.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#include "bootmgr.h"
22
#include "efi_helper.h"
3+
#include "common/io/io.h"
34

45
#include <windows.h>
56

67
const char* enablePrivilege(const wchar_t* privilege)
78
{
8-
HANDLE token;
9+
FF_AUTO_CLOSE_FD HANDLE token = NULL;
910
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token))
1011
return "OpenProcessToken() failed";
1112

src/detection/brightness/brightness_obsd.c

+29-18
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,35 @@
88

99
const char* ffDetectBrightness(FF_MAYBE_UNUSED FFBrightnessOptions* options, FFlist* result)
1010
{
11-
FF_AUTO_CLOSE_FD int devfd = open("/dev/ttyC0", O_RDONLY);
12-
13-
if (devfd < 0) return "open(dev/ttyC0, O_RDONLY) failed";
14-
15-
struct wsdisplay_param param = {
16-
.param = WSDISPLAYIO_PARAM_BRIGHTNESS,
17-
};
18-
19-
if (ioctl(devfd, WSDISPLAYIO_GETPARAM, &param) < 0)
20-
return "ioctl(WSDISPLAYIO_GETPARAM) failed";
21-
22-
FFBrightnessResult* brightness = (FFBrightnessResult*) ffListAdd(result);
23-
ffStrbufInitStatic(&brightness->name, "wsdisplay");
24-
25-
brightness->max = param.max;
26-
brightness->min = param.min;
27-
brightness->current = param.curval;
28-
brightness->builtin = true;
11+
char path[] = "/dev/ttyCX";
12+
for (char i = '0'; i <= '9'; ++i) {
13+
path[strlen("/dev/ttyC")] = i;
14+
15+
FF_AUTO_CLOSE_FD int devfd = open(path, O_RDONLY);
16+
17+
if (devfd < 0) {
18+
if (errno == EACCES && i == '0')
19+
return "Permission denied when opening tty device";
20+
if (errno == ENOENT)
21+
break;
22+
continue;
23+
}
24+
25+
struct wsdisplay_param param = {
26+
.param = WSDISPLAYIO_PARAM_BRIGHTNESS,
27+
};
28+
29+
if (ioctl(devfd, WSDISPLAYIO_GETPARAM, &param) < 0)
30+
continue;
31+
32+
FFBrightnessResult* brightness = (FFBrightnessResult*) ffListAdd(result);
33+
ffStrbufInitF(&brightness->name, "ttyC%c", i);
34+
35+
brightness->max = param.max;
36+
brightness->min = param.min;
37+
brightness->current = param.curval;
38+
brightness->builtin = true;
39+
}
2940

3041
return NULL;
3142
}

src/detection/brightness/brightness_windows.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ static const char* detectWithDdcci(const FFDisplayServerResult* displayServer, F
4343
FF_LIBRARY_LOAD(dxva2, "dlopen dxva2" FF_LIBRARY_EXTENSION " failed", "dxva2" FF_LIBRARY_EXTENSION, 1)
4444
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(dxva2, GetPhysicalMonitorsFromHMONITOR)
4545
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(dxva2, GetMonitorBrightness)
46+
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(dxva2, DestroyPhysicalMonitor)
4647

4748
FF_LIST_FOR_EACH(FFDisplayResult, display, displayServer->displays)
4849
{
@@ -64,6 +65,8 @@ static const char* detectWithDdcci(const FFDisplayServerResult* displayServer, F
6465
brightness->current = curr;
6566
brightness->builtin = false;
6667
}
68+
69+
ffDestroyPhysicalMonitor(physicalMonitor.hPhysicalMonitor);
6770
}
6871
}
6972
return NULL;

src/detection/btrfs/btrfs_linux.c

+3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ static const char* enumerateDevices(FFBtrfsResult* item, int dfd, FFstrbuf* buff
1212

1313
FF_AUTO_CLOSE_DIR DIR* dirp = fdopendir(subfd);
1414
if(dirp == NULL)
15+
{
16+
close(subfd);
1517
return "fdopendir(\"/sys/fs/btrfs/UUID/devices\") == NULL";
18+
}
1619

1720
struct dirent* entry;
1821
while ((entry = readdir(dirp)) != NULL)

src/detection/camera/camera_linux.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const char* ffDetectCamera(FFlist* result)
3737
{
3838
case V4L2_COLORSPACE_SMPTE170M: ffStrbufInitStatic(&camera->colorspace, "SMPTE 170M"); break;
3939
case V4L2_COLORSPACE_SMPTE240M: ffStrbufInitStatic(&camera->colorspace, "SMPTE 240M"); break;
40-
case V4L2_COLORSPACE_BT878: ffStrbufInitStatic(&camera->colorspace, "BT.808"); break;
40+
case V4L2_COLORSPACE_BT878: ffStrbufInitStatic(&camera->colorspace, "BT.878"); break;
4141
case V4L2_COLORSPACE_470_SYSTEM_M: ffStrbufInitStatic(&camera->colorspace, "NTSC"); break;
4242
case V4L2_COLORSPACE_470_SYSTEM_BG: ffStrbufInitStatic(&camera->colorspace, "EBU 3213"); break;
4343
case V4L2_COLORSPACE_JPEG: ffStrbufInitStatic(&camera->colorspace, "JPEG"); break;

src/detection/camera/camera_windows.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const char* ffDetectCamera(FF_MAYBE_UNUSED FFlist* result)
4848

4949
for (uint32_t i = 0; i < count; i++)
5050
{
51-
IMFActivate* device = devices[i];
51+
IMFActivate* FF_AUTO_RELEASE_COM_OBJECT device = devices[i];
5252

5353
wchar_t buffer[256];
5454
uint32_t length = 0;
@@ -90,9 +90,11 @@ const char* ffDetectCamera(FF_MAYBE_UNUSED FFlist* result)
9090
continue;
9191

9292
// Assume first type is the maximum resolution
93-
IMFMediaType* FF_AUTO_RELEASE_COM_OBJECT type = NULL;
93+
IMFMediaType* type = NULL;
9494
for (DWORD idx = 0; SUCCEEDED(handler->GetMediaTypeByIndex(idx, &type)); ++idx)
9595
{
96+
on_scope_exit destroyType([=] { type->Release(); });
97+
9698
GUID majorType;
9799
if (FAILED(type->GetMajorType(&majorType)) || majorType != MFMediaType_Video)
98100
continue;
@@ -123,7 +125,7 @@ const char* ffDetectCamera(FF_MAYBE_UNUSED FFlist* result)
123125
}
124126
}
125127

126-
CoTaskMemFree(devices);
128+
if (devices) CoTaskMemFree(devices);
127129

128130
return nullptr;
129131
}

src/detection/chassis/chassis.c

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const char* ffChassisTypeToString(uint32_t type)
3434
case 0x1A: return "Compact PCI";
3535
case 0x1B: return "Advanced TCA";
3636
case 0x1C: return "Blade";
37+
case 0x1D: return "Mobile Workstation";
3738
case 0x1E: return "Tablet";
3839
case 0x1F: return "Convertible";
3940
case 0x20: return "Detachable";

src/detection/chassis/chassis_apple.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ const char* ffDetectChassis(FFChassisResult* result)
1717

1818
if (ffStrbufStartsWithS(&host.name, "MacBook "))
1919
ffStrbufSetStatic(&result->type, "Laptop");
20-
else if (ffStrbufStartsWithS(&host.name, "Mac mini "))
20+
else if (ffStrbufStartsWithS(&host.name, "Mac mini ") ||
21+
ffStrbufStartsWithS(&host.name, "Mac Studio "))
2122
ffStrbufSetStatic(&result->type, "Mini PC");
2223
else if (ffStrbufStartsWithS(&host.name, "iMac "))
23-
ffStrbufSetStatic(&result->type, "All-in-One");
24+
ffStrbufSetStatic(&result->type, "All in One");
2425
else
2526
ffStrbufSetStatic(&result->type, "Desktop");
2627

src/detection/cpu/cpu_linux.c

-2
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,6 @@ FF_MAYBE_UNUSED static uint16_t getPackageCount(FFstrbuf* cpuinfo)
430430

431431
while ((p = memmem(p, cpuinfo->length - (uint32_t) (p - cpuinfo->chars), "\nphysical id\t:", strlen("\nphysical id\t:"))))
432432
{
433-
if (!p) break;
434433
p += strlen("\nphysical id\t:");
435434
char* pend;
436435
unsigned long id = strtoul(p, &pend, 10);
@@ -586,7 +585,6 @@ FF_MAYBE_UNUSED static uint16_t getLoongarchPropCount(FFstrbuf* cpuinfo, const c
586585

587586
while ((p = memmem(p, cpuinfo->length - (uint32_t) (p - cpuinfo->chars), key, keylen)))
588587
{
589-
if (!p) break;
590588
p += keylen;
591589
char* pend;
592590
unsigned long id = strtoul(p, &pend, 10);

src/detection/cpu/cpu_nbsd.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@
99
#include <unistd.h>
1010
#include <fcntl.h>
1111

12+
static void freePropDict(prop_dictionary_t* pdict)
13+
{
14+
assert(pdict != NULL);
15+
if (*pdict == NULL) return;
16+
prop_object_release(*pdict);
17+
}
18+
1219
static const char* detectCpuTemp(double* current)
1320
{
1421
FF_AUTO_CLOSE_FD int fd = open(_PATH_SYSMON, O_RDONLY);
1522
if (fd < 0) return "open(_PATH_SYSMON, O_RDONLY) failed";
1623

17-
prop_dictionary_t root = NULL;
24+
__attribute__((__cleanup__(freePropDict))) prop_dictionary_t root = NULL;
1825
if (prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &root) < 0)
1926
return "prop_dictionary_recv_ioctl(ENVSYS_GETDICTIONARY) failed";
2027

src/detection/cpu/cpu_sunos.c

+8-6
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,24 @@ const char* ffDetectCPUImpl(FF_MAYBE_UNUSED const FFCPUOptions* options, FFCPURe
2323

2424
{
2525
kstat_named_t* kn = kstat_data_lookup(ks, "brand");
26-
ffStrbufSetNS(&cpu->name, KSTAT_NAMED_STR_BUFLEN(kn) - 1, KSTAT_NAMED_STR_PTR(kn));
26+
if (kn) ffStrbufSetNS(&cpu->name, KSTAT_NAMED_STR_BUFLEN(kn) - 1, KSTAT_NAMED_STR_PTR(kn));
2727
}
2828
{
2929
kstat_named_t* kn = kstat_data_lookup(ks, "vendor_id");
30-
ffStrbufSetNS(&cpu->vendor, KSTAT_NAMED_STR_BUFLEN(kn) - 1, KSTAT_NAMED_STR_PTR(kn));
30+
if (kn) ffStrbufSetNS(&cpu->vendor, KSTAT_NAMED_STR_BUFLEN(kn) - 1, KSTAT_NAMED_STR_PTR(kn));
3131
}
3232
ffCPUDetectSpeedByCpuid(cpu);
33-
kstat_named_t* kn = kstat_data_lookup(ks, "clock_MHz");
34-
if (kn->value.ui32 > cpu->frequencyBase)
35-
cpu->frequencyBase = kn->value.ui32;
33+
{
34+
kstat_named_t* kn = kstat_data_lookup(ks, "clock_MHz");
35+
if (kn && kn->value.ui32 > cpu->frequencyBase)
36+
cpu->frequencyBase = kn->value.ui32;
37+
}
3638

3739
ks = kstat_lookup(kc, "unix", -1, "system_misc");
3840
if (ks && kstat_read(kc, ks, NULL) >= 0)
3941
{
4042
kstat_named_t* kn = kstat_data_lookup(ks, "ncpus");
41-
cpu->coresLogical = cpu->coresPhysical = cpu->coresOnline = (uint16_t) kn->value.ui32;
43+
if (kn) cpu->coresLogical = cpu->coresPhysical = cpu->coresOnline = (uint16_t) kn->value.ui32;
4244
}
4345

4446
return NULL;

src/detection/cpu/cpu_windows.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,13 @@ static const char* detectByRegistry(FFCPUResult* cpu)
128128

129129
if (cpu->coresLogical == 0)
130130
{
131-
DWORD cores;
132-
if (RegQueryInfoKeyW(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor", NULL, NULL, &cores, NULL, NULL, NULL, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
133-
cpu->coresOnline = cpu->coresPhysical = cpu->coresLogical = (uint16_t) cores;
131+
FF_HKEY_AUTO_DESTROY hProcsKey = NULL;
132+
if (ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor", &hProcsKey, NULL))
133+
{
134+
uint32_t cores;
135+
if (ffRegGetNSubKeys(hProcsKey, &cores, NULL))
136+
cpu->coresOnline = cpu->coresPhysical = cpu->coresLogical = (uint16_t) cores;
137+
}
134138
}
135139

136140
uint32_t mhz;

src/detection/cpu/cpu_windows.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const char* detectThermalTemp(double* current, double* critical)
2626
else
2727
*critical = 0.0/0.0;
2828
}
29+
30+
return NULL;
2931
}
3032

3133
return "No WMI result returned";

src/detection/cpucache/cpucache_windows.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const char* ffDetectCPUCache(FFCPUCacheResult* result)
3131
case CacheInstruction: cacheType = FF_CPU_CACHE_TYPE_INSTRUCTION; break;
3232
case CacheData: cacheType = FF_CPU_CACHE_TYPE_DATA; break;
3333
case CacheTrace: cacheType = FF_CPU_CACHE_TYPE_TRACE; break;
34-
default: __builtin_unreachable(); break;
34+
default: break;
3535
}
3636
ffCPUCacheAddItem(result, ptr->Cache.Level, ptr->Cache.CacheSize, ptr->Cache.LineSize, cacheType);
3737
}

src/detection/cpuusage/cpuusage_apple.c

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <mach/processor_info.h>
55
#include <mach/mach_host.h>
6+
#include <mach/vm_map.h>
67

78
const char* ffGetCpuUsageInfo(FFlist* cpuTimes)
89
{
@@ -28,5 +29,7 @@ const char* ffGetCpuUsageInfo(FFlist* cpuTimes)
2829
.totalAll = (uint64_t)total,
2930
};
3031
}
32+
33+
vm_deallocate(mach_task_self(), (vm_address_t) cpuInfo, numCpuInfo * sizeof(integer_t));
3134
return NULL;
3235
}

src/detection/wm/wm_apple.m

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
FF_AUTO_FREE struct kinfo_proc* processes = ffSysctlGetData(request, requestLength, &length);
1717
if(processes == NULL)
1818
return "sysctl(CTL_KERN, KERN_PROC, KERN_PROC_ALL) failed";
19+
assert(length % sizeof(struct kinfo_proc) == 0);
1920

2021
for(size_t i = 0; i < length / sizeof(struct kinfo_proc); i++)
2122
{

0 commit comments

Comments
 (0)