diff --git a/src/common/format.h b/src/common/format.h index 580fc06c2..79e1c90f4 100644 --- a/src/common/format.h +++ b/src/common/format.h @@ -43,6 +43,6 @@ typedef struct FFformatarg void ffFormatAppendFormatArg(FFstrbuf* buffer, const FFformatarg* formatarg); void ffParseFormatString(FFstrbuf* buffer, const FFstrbuf* formatstr, uint32_t numArgs, const FFformatarg* arguments); #define FF_PARSE_FORMAT_STRING_CHECKED(buffer, formatstr, numArgs, arguments) do {\ - static_assert(sizeof(arguments) / sizeof(*(arguments)) == (numArgs), "Invalid number of format arguments");\ + static_assert(sizeof(arguments) / sizeof(*arguments) == (numArgs), "Invalid number of format arguments");\ ffParseFormatString((buffer), (formatstr), (numArgs), (arguments));\ } while (0) diff --git a/src/common/printing.h b/src/common/printing.h index 7ccc645f2..2060337c5 100644 --- a/src/common/printing.h +++ b/src/common/printing.h @@ -14,7 +14,7 @@ typedef enum FFPrintType { void ffPrintLogoAndKey(const char* moduleName, uint8_t moduleIndex, const FFModuleArgs* moduleArgs, FFPrintType printType); void ffPrintFormat(const char* moduleName, uint8_t moduleIndex, const FFModuleArgs* moduleArgs, FFPrintType printType, uint32_t numArgs, const FFformatarg* arguments); #define FF_PRINT_FORMAT_CHECKED(moduleName, moduleIndex, moduleArgs, printType, numArgs, arguments) do {\ - static_assert(sizeof(arguments) / sizeof(*(arguments)) == (numArgs), "Invalid number of format arguments");\ + static_assert(sizeof(arguments) / sizeof(*arguments) == (numArgs), "Invalid number of format arguments");\ ffPrintFormat((moduleName), (moduleIndex), (moduleArgs), (printType), (numArgs), (arguments));\ } while (0) FF_C_PRINTF(5, 6) void ffPrintError(const char* moduleName, uint8_t moduleIndex, const FFModuleArgs* moduleArgs, FFPrintType printType, const char* message, ...); @@ -23,6 +23,6 @@ void ffPrintCharTimes(char c, uint32_t times); void ffPrintModuleFormatHelp(const char* name, const char* def, uint32_t numArgs, const char* args[]); #define FF_PRINT_MODULE_FORMAT_HELP_CHECKED(moduleName, def, numArgs, args) do {\ - static_assert(sizeof(args) / sizeof(*(args)) == (numArgs), "Invalid number of format arguments");\ + static_assert(sizeof(args) / sizeof(*args) == (numArgs), "Invalid number of format arguments");\ ffPrintModuleFormatHelp((moduleName), (def), (numArgs), (args));\ } while (0) diff --git a/src/common/processing_linux.c b/src/common/processing_linux.c index c7c7cd556..0e6a243ce 100644 --- a/src/common/processing_linux.c +++ b/src/common/processing_linux.c @@ -153,14 +153,14 @@ void ffProcessGetInfoLinux(pid_t pid, FFstrbuf* processName, FFstrbuf* exe, cons size_t len = 0; int mibs[] = { CTL_KERN, KERN_PROCARGS2, pid }; - if (sysctl(mibs, sizeof(mibs) / sizeof(*mibs), NULL, &len, NULL, 0) == 0) + if (sysctl(mibs, ARRAY_SIZE(mibs), NULL, &len, NULL, 0) == 0) {// try get arg0 #ifndef MAC_OS_X_VERSION_10_15 //don't know why if don't let len longer, proArgs2 and len will change during the following sysctl() in old MacOS version. len++; #endif FF_AUTO_FREE char* const procArgs2 = malloc(len); - if (sysctl(mibs, sizeof(mibs) / sizeof(*mibs), procArgs2, &len, NULL, 0) == 0) + if (sysctl(mibs, ARRAY_SIZE(mibs), procArgs2, &len, NULL, 0) == 0) { // https://gist.github.com/nonowarn/770696#file-getargv-c-L46 uint32_t argc = *(uint32_t*) procArgs2; diff --git a/src/common/processing_windows.c b/src/common/processing_windows.c index df4e4cb69..e5f7086d3 100644 --- a/src/common/processing_windows.c +++ b/src/common/processing_windows.c @@ -50,7 +50,7 @@ const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool use int timeout = instance.config.general.processingTimeout; wchar_t pipeName[32]; - swprintf(pipeName, sizeof(pipeName) / sizeof(*pipeName), L"\\\\.\\pipe\\FASTFETCH-%u", GetCurrentProcessId()); + swprintf(pipeName, ARRAY_SIZE(pipeName), L"\\\\.\\pipe\\FASTFETCH-%u", GetCurrentProcessId()); FF_AUTO_CLOSE_FD HANDLE hChildPipeRead = CreateNamedPipeW( pipeName, diff --git a/src/common/properties.c b/src/common/properties.c index fbf11416e..746727d9c 100644 --- a/src/common/properties.c +++ b/src/common/properties.c @@ -100,7 +100,7 @@ bool ffParsePropFileValues(const char* filename, uint32_t numQueries, FFpropquer bool valueStorage[32]; bool* unsetValues = valueStorage; - if (numQueries > sizeof(valueStorage) / sizeof(valueStorage[0])) + if (numQueries > ARRAY_SIZE(valueStorage)) unsetValues = malloc(sizeof(bool) * numQueries); bool allSet = true; diff --git a/src/detection/bootmgr/bootmgr_windows.c b/src/detection/bootmgr/bootmgr_windows.c index 19484a7ed..54314bbad 100644 --- a/src/detection/bootmgr/bootmgr_windows.c +++ b/src/detection/bootmgr/bootmgr_windows.c @@ -38,9 +38,9 @@ const char* ffDetectBootmgr(FFBootmgrResult* result) uint8_t buffer[2048]; wchar_t key[16]; - swprintf(key, sizeof(key) / sizeof(*key), L"Boot%04X", value); + swprintf(key, ARRAY_SIZE(key), L"Boot%04X", value); uint32_t size = GetFirmwareEnvironmentVariableW(key, L"{" FF_EFI_GLOBAL_GUID L"}", buffer, sizeof(buffer)); - if (size < sizeof(FFEfiLoadOption) || size == sizeof(buffer)) + if (size < sizeof(FFEfiLoadOption) || size == ARRAY_SIZE(buffer)) return "GetFirmwareEnvironmentVariableW(Boot####) failed"; ffEfiFillLoadOption((FFEfiLoadOption *)buffer, result); diff --git a/src/detection/brightness/brightness_apple.c b/src/detection/brightness/brightness_apple.c index a9f8daf0c..e10d912be 100644 --- a/src/detection/brightness/brightness_apple.c +++ b/src/detection/brightness/brightness_apple.c @@ -92,13 +92,13 @@ static const char* detectWithDdcci(FF_MAYBE_UNUSED const FFDisplayServerResult* for (uint32_t i = 0; i < 2; ++i) { - IOAVServiceWriteI2C(service, 0x37, 0x51, i2cIn, sizeof(i2cIn)); + IOAVServiceWriteI2C(service, 0x37, 0x51, i2cIn, ARRAY_SIZE(i2cIn)); usleep(options->ddcciSleep * 1000); } } uint8_t i2cOut[12] = {}; - if (IOAVServiceReadI2C(service, 0x37, 0x51, i2cOut, sizeof(i2cOut)) == KERN_SUCCESS) + if (IOAVServiceReadI2C(service, 0x37, 0x51, i2cOut, ARRAY_SIZE(i2cOut)) == KERN_SUCCESS) { if (i2cOut[2] != 0x02 || i2cOut[3] != 0x00) continue; @@ -113,7 +113,7 @@ static const char* detectWithDdcci(FF_MAYBE_UNUSED const FFDisplayServerResult* ffStrbufInit(&brightness->name); uint8_t edid[128] = {}; - if (IOAVServiceReadI2C(service, 0x50, 0x00, edid, sizeof(edid)) == KERN_SUCCESS) + if (IOAVServiceReadI2C(service, 0x50, 0x00, edid, ARRAY_SIZE(edid)) == KERN_SUCCESS) ffEdidGetName(edid, &brightness->name); } } @@ -155,12 +155,12 @@ static const char* detectWithDdcci(const FFDisplayServerResult* displayServer, F .sendAddress = 0x6e, .sendTransactionType = kIOI2CSimpleTransactionType, .sendBuffer = (vm_address_t) i2cIn, - .sendBytes = sizeof(i2cIn) / sizeof(i2cIn[0]), + .sendBytes = ARRAY_SIZE(i2cIn), .minReplyDelay = options->ddcciSleep, .replyAddress = 0x6F, .replySubAddress = 0x51, .replyTransactionType = kIOI2CDDCciReplyTransactionType, - .replyBytes = sizeof(i2cOut) / sizeof(i2cOut[0]), + .replyBytes = ARRAY_SIZE(i2cOut), .replyBuffer = (vm_address_t) i2cOut, }; IOReturn ret = IOI2CSendRequest(connect, kNilOptions, &request); diff --git a/src/detection/brightness/brightness_bsd.c b/src/detection/brightness/brightness_bsd.c index 22b3772fb..b71f1074f 100644 --- a/src/detection/brightness/brightness_bsd.c +++ b/src/detection/brightness/brightness_bsd.c @@ -16,7 +16,7 @@ const char* ffDetectBrightness(FF_MAYBE_UNUSED FFBrightnessOptions* options, FFl for (char i = '0'; i <= '9'; ++i) { - path[sizeof(path) - 2] = i; + path[ARRAY_SIZE(path) - 2] = i; FF_AUTO_CLOSE_FD int blfd = open(path, O_RDONLY | O_CLOEXEC); if (blfd < 0) diff --git a/src/detection/brightness/brightness_linux.c b/src/detection/brightness/brightness_linux.c index f45fe823b..9682a7598 100644 --- a/src/detection/brightness/brightness_linux.c +++ b/src/detection/brightness/brightness_linux.c @@ -48,7 +48,7 @@ static const char* detectWithBacklight(FFlist* result) // if we managed to get edid, use it ffStrbufAppendS(&brightness->name, "/edid"); uint8_t edidData[128]; - if(ffReadFileData(brightness->name.chars, sizeof(edidData), edidData) == sizeof(edidData)) + if(ffReadFileData(brightness->name.chars, ARRAY_SIZE(edidData), edidData) == ARRAY_SIZE(edidData)) { ffStrbufClear(&brightness->name); ffEdidGetName(edidData, &brightness->name); diff --git a/src/detection/btrfs/btrfs_linux.c b/src/detection/btrfs/btrfs_linux.c index c18ab116f..3685a153d 100644 --- a/src/detection/btrfs/btrfs_linux.c +++ b/src/detection/btrfs/btrfs_linux.c @@ -24,8 +24,8 @@ static const char* enumerateDevices(FFBtrfsResult* item, int dfd, FFstrbuf* buff ffStrbufAppendC(&item->devices, ','); ffStrbufAppendS(&item->devices, entry->d_name); - char path[sizeof(entry->d_name) + sizeof("/size") + 1]; - snprintf(path, sizeof(path), "%s/size", entry->d_name); + char path[ARRAY_SIZE(entry->d_name) + ARRAY_SIZE("/size") + 1]; + snprintf(path, ARRAY_SIZE(path), "%s/size", entry->d_name); if (ffReadFileBufferRelative(subfd, path, buffer)) item->totalSize += ffStrbufToUInt(buffer, 0) * 512; diff --git a/src/detection/camera/camera_linux.c b/src/detection/camera/camera_linux.c index e7f5c925f..6e9658845 100644 --- a/src/detection/camera/camera_linux.c +++ b/src/detection/camera/camera_linux.c @@ -16,7 +16,7 @@ const char* ffDetectCamera(FFlist* result) for (uint32_t i = 0; i <= 9; ++i) { - path[sizeof(path) - 2] = (char) (i + '0'); + path[ARRAY_SIZE(path) - 2] = (char) (i + '0'); FF_AUTO_CLOSE_FD int fd = open(path, O_RDONLY); if (fd < 0) break; diff --git a/src/detection/camera/camera_windows.cpp b/src/detection/camera/camera_windows.cpp index b0f9112f2..6f19ce932 100644 --- a/src/detection/camera/camera_windows.cpp +++ b/src/detection/camera/camera_windows.cpp @@ -52,7 +52,7 @@ const char* ffDetectCamera(FF_MAYBE_UNUSED FFlist* result) wchar_t buffer[256]; uint32_t length = 0; - if (FAILED(device->GetString(MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME, buffer, sizeof(buffer), &length)) || length == 0) + if (FAILED(device->GetString(MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME, buffer, ARRAY_SIZE(buffer), &length)) || length == 0) continue; FFCameraResult* camera = (FFCameraResult*) ffListAdd(result); @@ -63,7 +63,7 @@ const char* ffDetectCamera(FF_MAYBE_UNUSED FFlist* result) camera->width = 0; camera->height = 0; - if (SUCCEEDED(device->GetString(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, buffer, sizeof(buffer), &length)) && length > 0) + if (SUCCEEDED(device->GetString(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, buffer, ARRAY_SIZE(buffer), &length)) && length > 0) ffStrbufSetNWS(&camera->id, length, buffer); IMFMediaSource* FF_AUTO_RELEASE_COM_OBJECT source = nullptr; diff --git a/src/detection/cpu/cpu.c b/src/detection/cpu/cpu.c index 298eb5bfe..774e747ae 100644 --- a/src/detection/cpu/cpu.c +++ b/src/detection/cpu/cpu.c @@ -13,7 +13,7 @@ const char* ffDetectCPU(const FFCPUOptions* options, FFCPUResult* cpu) " 2-Core", " 4-Core", " 6-Core", " 8-Core", " 10-Core", " 12-Core", " 14-Core", " 16-Core", " with Radeon Graphics" }; - ffStrbufRemoveStrings(&cpu->name, sizeof(removeStrings) / sizeof(removeStrings[0]), removeStrings); + ffStrbufRemoveStrings(&cpu->name, ARRAY_SIZE(removeStrings), removeStrings); ffStrbufSubstrBeforeFirstC(&cpu->name, '@'); //Cut the speed output in the name as we append our own ffStrbufTrimRight(&cpu->name, ' '); //If we removed the @ in previous step there was most likely a space before it return NULL; diff --git a/src/detection/cpu/cpu_apple.c b/src/detection/cpu/cpu_apple.c index b08a8b5bc..3c5f45940 100644 --- a/src/detection/cpu/cpu_apple.c +++ b/src/detection/cpu/cpu_apple.c @@ -85,8 +85,8 @@ static const char* detectCoreCount(FFCPUResult* cpu) if (nPerfLevels <= 0) return "sysctl(hw.nperflevels) failed"; char sysctlKey[] = "hw.perflevelN.logicalcpu"; - if (nPerfLevels > sizeof(cpu->coreTypes) / sizeof(cpu->coreTypes[0])) - nPerfLevels = sizeof(cpu->coreTypes) / sizeof(cpu->coreTypes[0]); + if (nPerfLevels > ARRAY_SIZE(cpu->coreTypes)) + nPerfLevels = ARRAY_SIZE(cpu->coreTypes); for (uint32_t i = 0; i < nPerfLevels; ++i) { sysctlKey[strlen("hw.perflevel")] = (char) ('0' + i); diff --git a/src/detection/cpu/cpu_linux.c b/src/detection/cpu/cpu_linux.c index fe08272f2..9533effd5 100644 --- a/src/detection/cpu/cpu_linux.c +++ b/src/detection/cpu/cpu_linux.c @@ -389,7 +389,7 @@ FF_MAYBE_UNUSED static void detectArmSoc(FFCPUResult* cpu) // device-vendor,device-model\0soc-vendor,soc-model\0 char content[256]; - ssize_t length = ffReadFileData("/proc/device-tree/compatible", sizeof(content), content); + ssize_t length = ffReadFileData("/proc/device-tree/compatible", ARRAY_SIZE(content), content); if (length <= 2) return; // get the second NUL terminated string if it exists diff --git a/src/detection/cpucache/cpucache_apple.c b/src/detection/cpucache/cpucache_apple.c index b0e0c4ff8..363733f8b 100644 --- a/src/detection/cpucache/cpucache_apple.c +++ b/src/detection/cpucache/cpucache_apple.c @@ -14,7 +14,7 @@ const char* ffDetectCPUCache(FFCPUCacheResult* result) char sysctlKey[128] = "hw.perflevelN."; char* pNum = sysctlKey + strlen("hw.perflevel"); char* pSubkey = sysctlKey + strlen("hw.perflevelN."); - const size_t lenLeft = sizeof(sysctlKey) - strlen("hw.perflevelN."); + const size_t lenLeft = ARRAY_SIZE(sysctlKey) - strlen("hw.perflevelN."); for (uint32_t i = 0; i < nPerfLevels; ++i) { diff --git a/src/detection/cpuusage/cpuusage_linux.c b/src/detection/cpuusage/cpuusage_linux.c index b91d6eeb1..5136b3489 100644 --- a/src/detection/cpuusage/cpuusage_linux.c +++ b/src/detection/cpuusage/cpuusage_linux.c @@ -8,13 +8,13 @@ const char* ffGetCpuUsageInfo(FFlist* cpuTimes) { char buf[PROC_FILE_BUFFSIZ]; - ssize_t nRead = ffReadFileData("/proc/stat", sizeof(buf) - 1, buf); + ssize_t nRead = ffReadFileData("/proc/stat", ARRAY_SIZE(buf) - 1, buf); if(nRead < 0) { #ifdef __ANDROID__ return "Accessing \"/proc/stat\" is restricted on Android O+"; #else - return "ffReadFileData(\"/proc/stat\", sizeof(buf) - 1, buf) failed"; + return "ffReadFileData(\"/proc/stat\", ARRAY_SIZE(buf) - 1, buf) failed"; #endif } buf[nRead] = '\0'; diff --git a/src/detection/disk/disk_linux.c b/src/detection/disk/disk_linux.c index c8e9fa276..41600e7ef 100644 --- a/src/detection/disk/disk_linux.c +++ b/src/detection/disk/disk_linux.c @@ -204,7 +204,7 @@ static bool isRemovable(FFDisk* currentDisk) return false; char sysBlockPartition[64]; - snprintf(sysBlockPartition, sizeof(sysBlockPartition), "/sys/class/block/%s", currentDisk->mountFrom.chars + strlen("/dev/")); + snprintf(sysBlockPartition, ARRAY_SIZE(sysBlockPartition), "/sys/class/block/%s", currentDisk->mountFrom.chars + strlen("/dev/")); char sysBlockVolume[PATH_MAX]; // /sys/devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3:1.0/host0/target0:0:0/0:0:0:0/block/sda/sda1 if (realpath(sysBlockPartition, sysBlockVolume) == NULL) @@ -235,7 +235,7 @@ static void detectStats(FFDisk* disk) { struct statvfs fs; if(statvfs(disk->mountpoint.chars, &fs) != 0) - memset(&fs, 0, sizeof(struct statvfs)); //Set all values to 0, so our values get initialized to 0 too + memset(&fs, 0, sizeof(fs)); //Set all values to 0, so our values get initialized to 0 too disk->bytesTotal = fs.f_blocks * fs.f_frsize; disk->bytesFree = fs.f_bfree * fs.f_frsize; diff --git a/src/detection/disk/disk_sunos.c b/src/detection/disk/disk_sunos.c index 9568a814b..e2d6d714e 100644 --- a/src/detection/disk/disk_sunos.c +++ b/src/detection/disk/disk_sunos.c @@ -86,7 +86,7 @@ static void detectStats(FFDisk* disk) { struct statvfs fs; if(statvfs(disk->mountpoint.chars, &fs) != 0) - memset(&fs, 0, sizeof(struct statvfs)); + memset(&fs, 0, sizeof(fs)); disk->bytesTotal = fs.f_blocks * fs.f_frsize; disk->bytesFree = fs.f_bfree * fs.f_frsize; diff --git a/src/detection/disk/disk_windows.c b/src/detection/disk/disk_windows.c index 9c0e06760..48c595ca0 100644 --- a/src/detection/disk/disk_windows.c +++ b/src/detection/disk/disk_windows.c @@ -22,9 +22,9 @@ static unsigned __stdcall testRemoteVolumeAccessible(void* mountpoint) const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks) { wchar_t buf[MAX_PATH + 1]; - uint32_t length = GetLogicalDriveStringsW(sizeof(buf) / sizeof(*buf), buf); - if (length == 0 || length >= sizeof(buf) / sizeof(*buf)) - return "GetLogicalDriveStringsW(sizeof(buf) / sizeof(*buf), buf) failed"; + uint32_t length = GetLogicalDriveStringsW(ARRAY_SIZE(buf), buf); + if (length == 0 || length >= ARRAY_SIZE(buf)) + return "GetLogicalDriveStringsW(ARRAY_SIZE(buf), buf) failed"; FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate(); @@ -32,7 +32,7 @@ const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks) if (__builtin_expect(options->folders.length == 1 && options->folders.chars[0] == '/', 0)) { wchar_t path[MAX_PATH + 1]; - GetSystemWindowsDirectoryW(path, sizeof(path) / sizeof(*path)); + GetSystemWindowsDirectoryW(path, ARRAY_SIZE(path)); ffStrbufSetF(&options->folders, "%c:\\", (char) path[0]); } @@ -76,7 +76,7 @@ const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks) { wchar_t volumeName[MAX_PATH + 1]; mountpoint[2] = L'\0'; - if(QueryDosDeviceW(mountpoint, volumeName, sizeof(volumeName) / sizeof(*volumeName))) + if(QueryDosDeviceW(mountpoint, volumeName, ARRAY_SIZE(volumeName))) ffStrbufSetWS(&disk->mountFrom, volumeName); mountpoint[2] = L'\\'; } @@ -104,11 +104,11 @@ const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks) DWORD diskFlags; BOOL result = GetVolumeInformationW(mountpoint, - diskName, sizeof(diskName) / sizeof(*diskName), //Volume name + diskName, ARRAY_SIZE(diskName), //Volume name NULL, //Serial number NULL, //Max component length &diskFlags, //File system flags - diskFileSystem, sizeof(diskFileSystem) / sizeof(*diskFileSystem) + diskFileSystem, ARRAY_SIZE(diskFileSystem) ); SetErrorMode(errorMode); diff --git a/src/detection/diskio/diskio_linux.c b/src/detection/diskio/diskio_linux.c index 24bc277a4..9bddb27ee 100644 --- a/src/detection/diskio/diskio_linux.c +++ b/src/detection/diskio/diskio_linux.c @@ -37,7 +37,7 @@ static void parseDiskIOCounters(int dfd, const char* devName, FFlist* result, FF if (!multiNs) { char pathSysBlock[16]; - snprintf(pathSysBlock, sizeof(pathSysBlock), "nvme%dn2", devid); + snprintf(pathSysBlock, ARRAY_SIZE(pathSysBlock), "nvme%dn2", devid); multiNs = faccessat(devfd, pathSysBlock, F_OK, 0) == 0; } if (multiNs) @@ -56,7 +56,7 @@ static void parseDiskIOCounters(int dfd, const char* devName, FFlist* result, FF uint64_t nRead, sectorRead, nWritten, sectorWritten; { char sysBlockStat[PROC_FILE_BUFFSIZ]; - ssize_t fileSize = ffReadFileDataRelative(dfd, "stat", sizeof(sysBlockStat) - 1, sysBlockStat); + ssize_t fileSize = ffReadFileDataRelative(dfd, "stat", ARRAY_SIZE(sysBlockStat) - 1, sysBlockStat); if (fileSize <= 0) return; sysBlockStat[fileSize] = '\0'; if (sscanf(sysBlockStat, "%" PRIu64 "%*u%" PRIu64 "%*u%" PRIu64 "%*u%" PRIu64 "%*u", &nRead, §orRead, &nWritten, §orWritten) <= 0) diff --git a/src/detection/diskio/diskio_windows.c b/src/detection/diskio/diskio_windows.c index 1317057d2..da9bf8daf 100644 --- a/src/detection/diskio/diskio_windows.c +++ b/src/detection/diskio/diskio_windows.c @@ -22,7 +22,7 @@ static bool detectPhysicalDisk(const wchar_t* szDevice, FFlist* result, FFDiskIO }, sizeof(STORAGE_PROPERTY_QUERY), &sddBuffer, - sizeof(sddBuffer), + ARRAY_SIZE(sddBuffer), &retSize, NULL ) || retSize == 0) diff --git a/src/detection/displayserver/displayserver_apple.c b/src/detection/displayserver/displayserver_apple.c index 006757d08..d51584923 100644 --- a/src/detection/displayserver/displayserver_apple.c +++ b/src/detection/displayserver/displayserver_apple.c @@ -21,7 +21,7 @@ static void detectDisplays(FFDisplayServerResult* ds) { CGDirectDisplayID screens[128]; uint32_t screenCount; - if(CGGetOnlineDisplayList(sizeof(screens) / sizeof(screens[0]), screens, &screenCount) != kCGErrorSuccess) + if(CGGetOnlineDisplayList(ARRAY_SIZE(screens), screens, &screenCount) != kCGErrorSuccess) return; FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate(); diff --git a/src/detection/displayserver/displayserver_windows.c b/src/detection/displayserver/displayserver_windows.c index 1b13b11a6..d0825dbc6 100644 --- a/src/detection/displayserver/displayserver_windows.c +++ b/src/detection/displayserver/displayserver_windows.c @@ -36,9 +36,9 @@ static void detectDisplays(FFDisplayServerResult* ds) EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM) &monitors); DISPLAYCONFIG_PATH_INFO paths[128]; - uint32_t pathCount = sizeof(paths) / sizeof(paths[0]); + uint32_t pathCount = ARRAY_SIZE(paths); DISPLAYCONFIG_MODE_INFO modes[256]; - uint32_t modeCount = sizeof(modes) / sizeof(modes[0]); + uint32_t modeCount = ARRAY_SIZE(modes); if (QueryDisplayConfig( QDC_ONLY_ACTIVE_PATHS, @@ -66,7 +66,7 @@ static void detectDisplays(FFDisplayServerResult* ds) { FF_LIST_FOR_EACH(FFMonitorInfo, item, monitors) { - if (wcsncmp(item->info.szDevice, sourceName.viewGdiDeviceName, sizeof(sourceName.viewGdiDeviceName) / sizeof(wchar_t)) == 0) + if (wcsncmp(item->info.szDevice, sourceName.viewGdiDeviceName, ARRAY_SIZE(sourceName.viewGdiDeviceName)) == 0) { monitorInfo = item; break; @@ -102,11 +102,11 @@ static void detectDisplays(FFDisplayServerResult* ds) *pRegPath = *pDevPath; ++pRegPath; ++pDevPath; - assert(pRegPath < regPath + sizeof(regPath) / sizeof(wchar_t) + strlen("Device Parameters")); + assert(pRegPath < regPath + ARRAY_SIZE(regPath) + strlen("Device Parameters")); } wcscpy(pRegPath, L"Device Parameters"); - edidLength = sizeof(edidData); + edidLength = ARRAY_SIZE(edidData); if (RegGetValueW(HKEY_LOCAL_MACHINE, regPath, L"EDID", RRF_RT_REG_BINARY, NULL, edidData, &edidLength) == ERROR_SUCCESS && edidLength > 0 && edidLength % 128 == 0) { diff --git a/src/detection/displayserver/linux/drm.c b/src/detection/displayserver/linux/drm.c index a17fc4974..fb837e4a8 100644 --- a/src/detection/displayserver/linux/drm.c +++ b/src/detection/displayserver/linux/drm.c @@ -58,7 +58,7 @@ static const char* drmParseSysfs(FFDisplayServerResult* result) } uint8_t edidData[512]; - ssize_t edidLength = ffReadFileData(drmDir.chars, sizeof(edidData), edidData); + ssize_t edidLength = ffReadFileData(drmDir.chars, ARRAY_SIZE(edidData), edidData); if(edidLength <= 0 || edidLength % 128 != 0) { edidLength = 0; @@ -66,7 +66,7 @@ static const char* drmParseSysfs(FFDisplayServerResult* result) ffStrbufAppendS(&drmDir, "/modes"); char modes[32]; - if (ffReadFileData(drmDir.chars, sizeof(modes), modes) >= 3) + if (ffReadFileData(drmDir.chars, ARRAY_SIZE(modes), modes) >= 3) { sscanf(modes, "%ux%u", &width, &height); ffStrbufAppendS(&name, plainName); @@ -194,7 +194,7 @@ FF_MAYBE_UNUSED static const char* drmGetEdidByConnId(uint32_t connId, uint8_t* char connectorId[16] = {}; ffStrbufAppendS(&drmDir, "/connector_id"); - ffReadFileData(drmDir.chars, sizeof(connectorId), connectorId); + ffReadFileData(drmDir.chars, ARRAY_SIZE(connectorId), connectorId); if (strtoul(connectorId, NULL, 10) != connId) { ffStrbufSubstrBefore(&drmDir, drmDirLength); @@ -229,7 +229,7 @@ static const char* drmConnectLibdrm(FFDisplayServerResult* result) FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libdrm, drmFreeDevices) drmDevice* devices[64]; - int nDevices = ffdrmGetDevices(devices, sizeof(devices) / sizeof(devices[0])); + int nDevices = ffdrmGetDevices(devices, ARRAY_SIZE(devices)); if (nDevices <= 0) return "drmGetDevices() failed"; diff --git a/src/detection/displayserver/linux/wayland/wayland.c b/src/detection/displayserver/linux/wayland/wayland.c index 457b334ee..e4b387a51 100644 --- a/src/detection/displayserver/linux/wayland/wayland.c +++ b/src/detection/displayserver/linux/wayland/wayland.c @@ -22,7 +22,7 @@ static bool waylandDetectWM(int fd, FFDisplayServerResult* result) { struct ucred ucred; - socklen_t len = sizeof(struct ucred); + socklen_t len = sizeof(ucred); if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == -1 || ucred.pid <= 0) return false; @@ -108,7 +108,7 @@ static bool matchDrmConnector(const char* connName, WaylandDisplay* wldata) FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateF("%s%s/edid", drmDirPath, entry->d_name); uint8_t edidData[512]; - ssize_t edidLength = ffReadFileData(path.chars, sizeof(edidData), edidData); + ssize_t edidLength = ffReadFileData(path.chars, ARRAY_SIZE(edidData), edidData); if (edidLength > 0 && edidLength % 128 == 0) { ffEdidGetName(edidData, &wldata->edidName); @@ -249,7 +249,7 @@ const char* ffdsConnectWayland(FFDisplayServerResult* result) FF_LIST_FOR_EACH(FFstrbuf, basePath, instance.state.platform.configDirs) { char path[1024]; - snprintf(path, sizeof(path) - 1, "%s%s", basePath->chars, fileName); + snprintf(path, ARRAY_SIZE(path) - 1, "%s%s", basePath->chars, fileName); if (ffReadFileBuffer(path, &monitorsXml)) break; } diff --git a/src/detection/displayserver/linux/wmde.c b/src/detection/displayserver/linux/wmde.c index b9d6c3248..5502da78a 100644 --- a/src/detection/displayserver/linux/wmde.c +++ b/src/detection/displayserver/linux/wmde.c @@ -260,11 +260,11 @@ static const char* getFromProcesses(FFDisplayServerResult* result) int request[] = {CTL_KERN, KERN_PROC, KERN_PROC_UID, (int) userId}; size_t length = 0; - if(sysctl(request, sizeof(request) / sizeof(*request), NULL, &length, NULL, 0) != 0) + if(sysctl(request, ARRAY_SIZE(request), NULL, &length, NULL, 0) != 0) return "sysctl({CTL_KERN, KERN_PROC, KERN_PROC_UID}, NULL) failed"; FF_AUTO_FREE struct kinfo_proc* procs = (struct kinfo_proc*) malloc(length); - if(sysctl(request, sizeof(request) / sizeof(*request), procs, &length, NULL, 0) != 0) + if(sysctl(request, ARRAY_SIZE(request), procs, &length, NULL, 0) != 0) return "sysctl({CTL_KERN, KERN_PROC, KERN_PROC_UID}, procs) failed"; length /= sizeof(*procs); @@ -283,7 +283,7 @@ static const char* getFromProcesses(FFDisplayServerResult* result) #elif __OpenBSD__ kvm_t* kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL); int count = 0; - const struct kinfo_proc* proc = kvm_getprocs(kd, KERN_PROC_UID, userId, sizeof(struct kinfo_proc), &count); + const struct kinfo_proc* proc = kvm_getprocs(kd, KERN_PROC_UID, userId, sizeof(*proc), &count); if (proc) { for (int i = 0; i < count; ++i) diff --git a/src/detection/font/font_linux.c b/src/detection/font/font_linux.c index 7a7acfe43..4d58392c9 100644 --- a/src/detection/font/font_linux.c +++ b/src/detection/font/font_linux.c @@ -11,7 +11,7 @@ static void generateString(FFFontResult* font) ffStrbufAppend(&font->display, &font->fonts[0]); ffStrbufAppendS(&font->display, " [Qt]"); - for(uint8_t i = 1; i < sizeof(font->fonts) / sizeof(font->fonts[0]); i++) + for(uint8_t i = 1; i < ARRAY_SIZE(font->fonts); i++) { if(font->fonts[i].length > 0) { diff --git a/src/detection/font/font_windows.c b/src/detection/font/font_windows.c index 46a63f520..eb5446006 100644 --- a/src/detection/font/font_windows.c +++ b/src/detection/font/font_windows.c @@ -6,7 +6,7 @@ static void generateString(FFFontResult* font) { const char* types[] = { "Caption", "Menu", "Message", "Status" }; - for(uint32_t i = 0; i < sizeof(types) / sizeof(types[0]); ++i) + for(uint32_t i = 0; i < ARRAY_SIZE(types); ++i) { if(i == 0 || !ffStrbufEqual(&font->fonts[i - 1], &font->fonts[i])) { @@ -31,7 +31,7 @@ const char* ffDetectFontImpl(FFFontResult* result) LOGFONTW* fonts[4] = { &info.lfCaptionFont, &info.lfMenuFont, &info.lfMessageFont, &info.lfStatusFont }; - for(uint32_t i = 0; i < sizeof(fonts) / sizeof(fonts[0]); ++i) + for(uint32_t i = 0; i < ARRAY_SIZE(fonts); ++i) { ffStrbufSetWS(&result->fonts[i], fonts[i]->lfFaceName); if(fonts[i]->lfHeight < 0) diff --git a/src/detection/gamepad/gamepad_bsd.c b/src/detection/gamepad/gamepad_bsd.c index 5bcc100b8..a84f4f42c 100644 --- a/src/detection/gamepad/gamepad_bsd.c +++ b/src/detection/gamepad/gamepad_bsd.c @@ -13,7 +13,7 @@ const char* ffDetectGamepad(FFlist* devices /* List of FFGamepadDevice */) char path[16]; for (int i = 0; i < MAX_UHID_JOYS; i++) { - snprintf(path, sizeof(path), "/dev/uhid%d", i); + snprintf(path, ARRAY_SIZE(path), "/dev/uhid%d", i); FF_AUTO_CLOSE_FD int fd = open(path, O_RDONLY | O_CLOEXEC); if (fd < 0) continue; diff --git a/src/detection/gamepad/gamepad_linux.c b/src/detection/gamepad/gamepad_linux.c index 808628934..d8dacb40e 100644 --- a/src/detection/gamepad/gamepad_linux.c +++ b/src/detection/gamepad/gamepad_linux.c @@ -28,7 +28,7 @@ static void detectGamepad(FFlist* devices, FFstrbuf* name, FFstrbuf* path) ffStrbufAppendS(path, entry->d_name); ffStrbufAppendS(path, "/capacity"); // /sys/class/input/jsX/device/device/power_supply/XXX/capacity char capacity[32]; - ssize_t nRead = ffReadFileData(path->chars, sizeof(capacity) - 1, capacity); + ssize_t nRead = ffReadFileData(path->chars, ARRAY_SIZE(capacity) - 1, capacity); if (nRead > 0) // Tested with a PS4 controller { capacity[nRead] = '\0'; @@ -37,7 +37,7 @@ static void detectGamepad(FFlist* devices, FFstrbuf* name, FFstrbuf* path) } ffStrbufAppendS(path, "_level"); - nRead = ffReadFileData(path->chars, sizeof(capacity) - 1, capacity); + nRead = ffReadFileData(path->chars, ARRAY_SIZE(capacity) - 1, capacity); if (nRead > 0) // Tested with a NS Pro controller { // https://github.com/torvalds/linux/blob/52b1853b080a082ec3749c3a9577f6c71b1d4a90/drivers/power/supply/power_supply_sysfs.c#L124 diff --git a/src/detection/gamepad/gamepad_windows.c b/src/detection/gamepad/gamepad_windows.c index f9652a0e6..d441b01ef 100644 --- a/src/detection/gamepad/gamepad_windows.c +++ b/src/detection/gamepad/gamepad_windows.c @@ -114,10 +114,10 @@ const char* ffDetectGamepad(FFlist* devices /* List of FFGamepadDevice */) if (!knownGamepad) { wchar_t displayName[126]; - if (HidD_GetProductString(hHidFile, displayName, sizeof(displayName))) + if (HidD_GetProductString(hHidFile, displayName, sizeof(displayName) /*in bytes*/)) { wchar_t manufacturer[126]; - if (HidD_GetManufacturerString(hHidFile, manufacturer, sizeof(manufacturer))) + if (HidD_GetManufacturerString(hHidFile, manufacturer, sizeof(manufacturer) /*in bytes*/)) { ffStrbufSetWS(&device->name, manufacturer); FF_STRBUF_AUTO_DESTROY displayNameStr = ffStrbufCreateWS(displayName); @@ -132,7 +132,7 @@ const char* ffDetectGamepad(FFlist* devices /* List of FFGamepadDevice */) } wchar_t serialNumber[127] = L""; - if (HidD_GetSerialNumberString(hHidFile, serialNumber, sizeof(serialNumber))) + if (HidD_GetSerialNumberString(hHidFile, serialNumber, sizeof(serialNumber) /*in bytes*/)) ffStrbufSetWS(&device->serial, serialNumber); PHIDP_PREPARSED_DATA preparsedData = NULL; diff --git a/src/detection/gpu/gpu_bsd.c b/src/detection/gpu/gpu_bsd.c index 99195af20..b64935df4 100644 --- a/src/detection/gpu/gpu_bsd.c +++ b/src/detection/gpu/gpu_bsd.c @@ -76,7 +76,7 @@ const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus) if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_AMD) { char query[32]; - snprintf(query, sizeof(query), "%X,\t%X,", (unsigned) pc->pc_device, (unsigned) pc->pc_revid); + snprintf(query, ARRAY_SIZE(query), "%X,\t%X,", (unsigned) pc->pc_device, (unsigned) pc->pc_revid); ffParsePropFileData("libdrm/amdgpu.ids", query, &gpu->name); } if (gpu->name.length == 0) diff --git a/src/detection/gpu/gpu_intel.c b/src/detection/gpu/gpu_intel.c index ffa04ccb8..c01c80ab4 100644 --- a/src/detection/gpu/gpu_intel.c +++ b/src/detection/gpu/gpu_intel.c @@ -128,7 +128,7 @@ const char* ffDetectIntelGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverRe if (result.memory) { ctl_mem_handle_t memoryModules[16]; - uint32_t memoryCount = sizeof(memoryModules) / sizeof(memoryModules[0]); + uint32_t memoryCount = ARRAY_SIZE(memoryModules); if (igclData.ffctlEnumMemoryModules(device, &memoryCount, memoryModules) == CTL_RESULT_SUCCESS && memoryCount > 0) { result.memory->used = 0; @@ -157,7 +157,7 @@ const char* ffDetectIntelGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverRe if (result.temp) { ctl_temp_handle_t sensors[16]; - uint32_t sensorCount = sizeof(sensors) / sizeof(sensors[0]); + uint32_t sensorCount = ARRAY_SIZE(sensors); if (igclData.ffctlEnumTemperatureSensors(device, &sensorCount, sensors) == CTL_RESULT_SUCCESS && sensorCount > 0) { double sumValue = 0; @@ -178,7 +178,7 @@ const char* ffDetectIntelGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverRe if (result.frequency) { ctl_freq_handle_t domains[16]; - uint32_t domainCount = sizeof(domains) / sizeof(domains[0]); + uint32_t domainCount = ARRAY_SIZE(domains); if (igclData.ffctlEnumFrequencyDomains(device, &domainCount, domains) == CTL_RESULT_SUCCESS && domainCount > 0) { double maxValue = 0; diff --git a/src/detection/gpu/gpu_linux.c b/src/detection/gpu/gpu_linux.c index 096395896..a915a25e9 100644 --- a/src/detection/gpu/gpu_linux.c +++ b/src/detection/gpu/gpu_linux.c @@ -32,7 +32,7 @@ static bool pciDetectDriver(FFstrbuf* result, FFstrbuf* pciDir, FFstrbuf* buffer uint32_t pciDirLength = pciDir->length; ffStrbufAppendS(pciDir, "/driver"); char pathBuf[PATH_MAX]; - ssize_t resultLength = readlink(pciDir->chars, pathBuf, sizeof(pathBuf)); + ssize_t resultLength = readlink(pciDir->chars, pathBuf, ARRAY_SIZE(pathBuf)); if(resultLength <= 0) return false; const char* slash = memrchr(pathBuf, '/', (size_t) resultLength); @@ -415,7 +415,7 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf const char* pPciPath = NULL; if (drmKey) { - ssize_t pathLength = readlink(deviceDir->chars, pciPath, sizeof(pciPath) - 1); + ssize_t pathLength = readlink(deviceDir->chars, pciPath, ARRAY_SIZE(pciPath) - 1); if (pathLength <= 0) return "Unable to get PCI device path"; pciPath[pathLength] = '\0'; @@ -460,7 +460,7 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf { if (ffStrStartsWith(entry->d_name, "card")) { - ffStrCopyN(drmKeyBuffer, entry->d_name, sizeof(drmKeyBuffer)); + ffStrCopyN(drmKeyBuffer, entry->d_name, ARRAY_SIZE(drmKeyBuffer)); drmKey = drmKeyBuffer; break; } @@ -493,7 +493,7 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf if (pend != buffer->chars) { char query[32]; - snprintf(query, sizeof(query), "%X,\t%X,", (unsigned) deviceId, (unsigned) revision); + snprintf(query, ARRAY_SIZE(query), "%X,\t%X,", (unsigned) deviceId, (unsigned) revision); #ifdef FF_CUSTOM_AMDGPU_IDS_PATH ffParsePropFile(FF_STR(FF_CUSTOM_AMDGPU_IDS_PATH), query, &gpu->name); #else diff --git a/src/detection/gpu/gpu_mthreads.c b/src/detection/gpu/gpu_mthreads.c index 80164320d..ff1347851 100644 --- a/src/detection/gpu/gpu_mthreads.c +++ b/src/detection/gpu/gpu_mthreads.c @@ -86,7 +86,7 @@ const char *ffDetectMthreadsGpuInfo(const FFGpuDriverCondition *cond, FFGpuDrive if (cond->type & FF_GPU_DRIVER_CONDITION_TYPE_BUS_ID) { char pciBusIdStr[32]; - snprintf(pciBusIdStr, sizeof(pciBusIdStr) - 1, "%04x:%02x:%02x.%d", cond->pciBusId.domain, cond->pciBusId.bus, cond->pciBusId.device, cond->pciBusId.func); + snprintf(pciBusIdStr, ARRAY_SIZE(pciBusIdStr) - 1, "%04x:%02x:%02x.%d", cond->pciBusId.domain, cond->pciBusId.bus, cond->pciBusId.device, cond->pciBusId.func); MtmlReturn ret = mtmlData.ffmtmlLibraryInitDeviceByPciSbdf(mtmlData.lib, pciBusIdStr, &device); if (ret != MTML_SUCCESS) @@ -195,7 +195,7 @@ const char *ffDetectMthreadsGpuInfo(const FFGpuDriverCondition *cond, FFGpuDrive if (result.name) { char name[MTML_DEVICE_NAME_BUFFER_SIZE]; - if (mtmlData.ffmtmlDeviceGetName(device, name, sizeof(name)) == MTML_SUCCESS) + if (mtmlData.ffmtmlDeviceGetName(device, name, ARRAY_SIZE(name)) == MTML_SUCCESS) ffStrbufSetS(result.name, name); } diff --git a/src/detection/gpu/gpu_nvidia.c b/src/detection/gpu/gpu_nvidia.c index c2cc33d1d..e91d7eeec 100644 --- a/src/detection/gpu/gpu_nvidia.c +++ b/src/detection/gpu/gpu_nvidia.c @@ -59,7 +59,7 @@ const char* ffDetectNvidiaGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverR if (cond->type & FF_GPU_DRIVER_CONDITION_TYPE_BUS_ID) { char pciBusIdStr[32]; - snprintf(pciBusIdStr, sizeof(pciBusIdStr) - 1, "%04x:%02x:%02x.%d", cond->pciBusId.domain, cond->pciBusId.bus, cond->pciBusId.device, cond->pciBusId.func); + snprintf(pciBusIdStr, ARRAY_SIZE(pciBusIdStr) - 1, "%04x:%02x:%02x.%d", cond->pciBusId.domain, cond->pciBusId.bus, cond->pciBusId.device, cond->pciBusId.func); nvmlReturn_t ret = nvmlData.ffnvmlDeviceGetHandleByPciBusId_v2(pciBusIdStr, &device); if (ret != NVML_SUCCESS) @@ -148,7 +148,7 @@ const char* ffDetectNvidiaGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverR if (result.name) { char name[NVML_DEVICE_NAME_V2_BUFFER_SIZE]; - if (nvmlData.ffnvmlDeviceGetName(device, name, sizeof(name)) == NVML_SUCCESS) + if (nvmlData.ffnvmlDeviceGetName(device, name, ARRAY_SIZE(name)) == NVML_SUCCESS) ffStrbufSetS(result.name, name); } diff --git a/src/detection/gpu/gpu_pci.c b/src/detection/gpu/gpu_pci.c index 5397f0fe6..fe0f0c939 100644 --- a/src/detection/gpu/gpu_pci.c +++ b/src/detection/gpu/gpu_pci.c @@ -55,7 +55,7 @@ static void parsePciIdsFile(const FFstrbuf* content, uint8_t subclass, uint16_t char buffer[32]; // Search for vendor - uint32_t len = (uint32_t) snprintf(buffer, sizeof(buffer), "\n%04x ", vendor); + uint32_t len = (uint32_t) snprintf(buffer, ARRAY_SIZE(buffer), "\n%04x ", vendor); char* start = (char*) memmem(content->chars, content->length, buffer, len); char* end = content->chars + content->length; if (start) @@ -83,7 +83,7 @@ static void parsePciIdsFile(const FFstrbuf* content, uint8_t subclass, uint16_t } // Search for device - len = (uint32_t) snprintf(buffer, sizeof(buffer), "\n\t%04x ", device); + len = (uint32_t) snprintf(buffer, ARRAY_SIZE(buffer), "\n\t%04x ", device); start = memmem(start, (size_t) (end - start), buffer, len); if (start) { @@ -138,7 +138,7 @@ static bool loadPciidsInc(uint8_t subclass, uint16_t vendor, uint16_t device, FF if (!gpu->vendor.length) ffStrbufSetS(&gpu->vendor, pvendor->name); - const FFPciDevice* pdevice = (const FFPciDevice*) bsearch(&device, pvendor->devices, pvendor->nDevices, sizeof(FFPciDevice), (void*) pciDeviceCmp); + const FFPciDevice* pdevice = (const FFPciDevice*) bsearch(&device, pvendor->devices, pvendor->nDevices, sizeof(*pdevice), (void*) pciDeviceCmp); if (pdevice) { diff --git a/src/detection/gpu/gpu_sunos.c b/src/detection/gpu/gpu_sunos.c index 6e8d19460..4edfb7fb0 100644 --- a/src/detection/gpu/gpu_sunos.c +++ b/src/detection/gpu/gpu_sunos.c @@ -67,7 +67,7 @@ const char* ffDetectGPUImpl(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist* if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_AMD) { char query[32]; - snprintf(query, sizeof(query), "%X,\t%X,", (unsigned) deviceId, (unsigned) revision); + snprintf(query, ARRAY_SIZE(query), "%X,\t%X,", (unsigned) deviceId, (unsigned) revision); ffParsePropFileData("libdrm/amdgpu.ids", query, &gpu->name); } diff --git a/src/detection/keyboard/keyboard_bsd.c b/src/detection/keyboard/keyboard_bsd.c index c1be06aa4..4f424ba70 100644 --- a/src/detection/keyboard/keyboard_bsd.c +++ b/src/detection/keyboard/keyboard_bsd.c @@ -13,7 +13,7 @@ const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */) char path[16]; for (int i = 0; i < MAX_UHID_JOYS; i++) { - snprintf(path, sizeof(path), "/dev/uhid%d", i); + snprintf(path, ARRAY_SIZE(path), "/dev/uhid%d", i); FF_AUTO_CLOSE_FD int fd = open(path, O_RDONLY | O_CLOEXEC); if (fd < 0) continue; diff --git a/src/detection/keyboard/keyboard_linux.c b/src/detection/keyboard/keyboard_linux.c index 42618272f..8952ca674 100644 --- a/src/detection/keyboard/keyboard_linux.c +++ b/src/detection/keyboard/keyboard_linux.c @@ -20,7 +20,7 @@ const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */) continue; char buffer[32]; // `../eventX` - ssize_t len = readlinkat(dirfd(dirp), entry->d_name, buffer, sizeof(buffer) - 1); + ssize_t len = readlinkat(dirfd(dirp), entry->d_name, buffer, ARRAY_SIZE(buffer) - 1); if (len != strlen("../eventX") || !ffStrStartsWith(buffer, "../event")) continue; buffer[len] = 0; diff --git a/src/detection/localip/localip_linux.c b/src/detection/localip/localip_linux.c index fa512fd13..adf5713da 100644 --- a/src/detection/localip/localip_linux.c +++ b/src/detection/localip/localip_linux.c @@ -198,7 +198,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) char addressBuffer[32]; uint8_t* ptr = (uint8_t*) LLADDR((struct sockaddr_dl *)ifa->ifa_addr); - snprintf(addressBuffer, sizeof(addressBuffer), "%02x:%02x:%02x:%02x:%02x:%02x", + snprintf(addressBuffer, ARRAY_SIZE(addressBuffer), "%02x:%02x:%02x:%02x:%02x:%02x", ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]); addNewIp(results, ifa->ifa_name, addressBuffer, -1, isDefaultRoute, flags, false); } @@ -210,7 +210,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) char addressBuffer[32]; uint8_t* ptr = ((struct sockaddr_ll *)ifa->ifa_addr)->sll_addr; - snprintf(addressBuffer, sizeof(addressBuffer), "%02x:%02x:%02x:%02x:%02x:%02x", + snprintf(addressBuffer, ARRAY_SIZE(addressBuffer), "%02x:%02x:%02x:%02x:%02x:%02x", ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]); addNewIp(results, ifa->ifa_name, addressBuffer, -1, isDefaultRoute, flags, false); } diff --git a/src/detection/localip/localip_windows.c b/src/detection/localip/localip_windows.c index 00fee43ed..7d5091699 100644 --- a/src/detection/localip/localip_windows.c +++ b/src/detection/localip/localip_windows.c @@ -106,7 +106,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) bool newIp = true; char name[128]; - WideCharToMultiByte(CP_UTF8, 0, adapter->FriendlyName, -1, name, sizeof(name), NULL, NULL); + WideCharToMultiByte(CP_UTF8, 0, adapter->FriendlyName, -1, name, ARRAY_SIZE(name), NULL, NULL); if (options->namePrefix.length && strncmp(name, options->namePrefix.chars, options->namePrefix.length) != 0) continue; @@ -114,7 +114,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) { char addressBuffer[32]; uint8_t* ptr = adapter->PhysicalAddress; - snprintf(addressBuffer, sizeof(addressBuffer), "%02x:%02x:%02x:%02x:%02x:%02x", + snprintf(addressBuffer, ARRAY_SIZE(addressBuffer), "%02x:%02x:%02x:%02x:%02x:%02x", ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]); addNewIp(results, name, addressBuffer, -1, newIp, isDefaultRoute); newIp = false; diff --git a/src/detection/media/media_apple.m b/src/detection/media/media_apple.m index e17ab48e2..c576c7e08 100644 --- a/src/detection/media/media_apple.m +++ b/src/detection/media/media_apple.m @@ -67,7 +67,7 @@ static const char* getMedia(FFMediaResult* result) if(result->playerId.length > 0) { char buf[128]; - snprintf(buf, sizeof(buf), "name of app id \"%s\"", result->playerId.chars); + snprintf(buf, ARRAY_SIZE(buf), "name of app id \"%s\"", result->playerId.chars); ffOsascript(buf, &result->player); } diff --git a/src/detection/memory/memory_linux.c b/src/detection/memory/memory_linux.c index 6f9e87a0f..14c86bf93 100644 --- a/src/detection/memory/memory_linux.c +++ b/src/detection/memory/memory_linux.c @@ -7,9 +7,9 @@ const char* ffDetectMemory(FFMemoryResult* ram) { char buf[PROC_FILE_BUFFSIZ]; - ssize_t nRead = ffReadFileData("/proc/meminfo", sizeof(buf) - 1, buf); + ssize_t nRead = ffReadFileData("/proc/meminfo", ARRAY_SIZE(buf) - 1, buf); if(nRead < 0) - return "ffReadFileData(\"/proc/meminfo\", sizeof(buf)-1, buf)"; + return "ffReadFileData(\"/proc/meminfo\", ARRAY_SIZE(buf)-1, buf)"; buf[nRead] = '\0'; uint64_t memTotal = 0, diff --git a/src/detection/mouse/mouse_bsd.c b/src/detection/mouse/mouse_bsd.c index ec978974b..6018d1893 100644 --- a/src/detection/mouse/mouse_bsd.c +++ b/src/detection/mouse/mouse_bsd.c @@ -13,7 +13,7 @@ const char* ffDetectMouse(FFlist* devices /* List of FFMouseDevice */) char path[16]; for (int i = 0; i < MAX_UHID_JOYS; i++) { - snprintf(path, sizeof(path), "/dev/uhid%d", i); + snprintf(path, ARRAY_SIZE(path), "/dev/uhid%d", i); FF_AUTO_CLOSE_FD int fd = open(path, O_RDONLY | O_CLOEXEC); if (fd < 0) continue; diff --git a/src/detection/netio/netio_windows.c b/src/detection/netio/netio_windows.c index 4291df3d4..1d3ab8fbe 100644 --- a/src/detection/netio/netio_windows.c +++ b/src/detection/netio/netio_windows.c @@ -47,7 +47,7 @@ const char* ffNetIOGetIoCounters(FFlist* result, FFNetIOOptions* options) continue; char name[128]; - WideCharToMultiByte(CP_UTF8, 0, adapter->FriendlyName, -1, name, sizeof(name), NULL, NULL); + WideCharToMultiByte(CP_UTF8, 0, adapter->FriendlyName, -1, name, ARRAY_SIZE(name), NULL, NULL); if (options->namePrefix.length && strncmp(name, options->namePrefix.chars, options->namePrefix.length) != 0) continue; diff --git a/src/detection/opencl/opencl.c b/src/detection/opencl/opencl.c index 71e89a36e..56f86215f 100644 --- a/src/detection/opencl/opencl.c +++ b/src/detection/opencl/opencl.c @@ -33,7 +33,7 @@ static const char* openCLHandleData(OpenCLData* data, FFOpenCLResult* result) { cl_platform_id platforms[32]; cl_uint numPlatforms = 0; - cl_int ret = data->ffclGetPlatformIDs(sizeof(platforms) / sizeof(platforms[0]), platforms, &numPlatforms); + cl_int ret = data->ffclGetPlatformIDs(ARRAY_SIZE(platforms), platforms, &numPlatforms); if (ret != CL_SUCCESS) { switch (ret) @@ -77,7 +77,7 @@ static const char* openCLHandleData(OpenCLData* data, FFOpenCLResult* result) } cl_device_id deviceIDs[32]; - cl_uint numDevices = (cl_uint) (sizeof(deviceIDs) / sizeof(deviceIDs[0])); + cl_uint numDevices = (cl_uint) ARRAY_SIZE(deviceIDs); if (data->ffclGetDeviceIDs(platforms[iplat], CL_DEVICE_TYPE_GPU, numDevices, deviceIDs, &numDevices) != CL_SUCCESS) continue; diff --git a/src/detection/opengl/opengl_windows.c b/src/detection/opengl/opengl_windows.c index 529e841ac..cc251c188 100644 --- a/src/detection/opengl/opengl_windows.c +++ b/src/detection/opengl/opengl_windows.c @@ -31,7 +31,7 @@ static const char* wglHandlePixelFormat(WGLData* wglData, HWND hWnd) { PIXELFORMATDESCRIPTOR pfd = { - sizeof(PIXELFORMATDESCRIPTOR), + sizeof(pfd), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, //Flags PFD_TYPE_RGBA, // The kind of framebuffer. RGBA or palette. diff --git a/src/detection/packages/packages_windows.c b/src/detection/packages/packages_windows.c index 103f7a6a1..afa2b12e9 100644 --- a/src/detection/packages/packages_windows.c +++ b/src/detection/packages/packages_windows.c @@ -58,8 +58,8 @@ static void detectChoco(FF_MAYBE_UNUSED FFPackagesResult* result) return; char chocoPath[MAX_PATH + 3]; - char* pend = ffStrCopyN(chocoPath, chocoInstall, sizeof(chocoPath)); - ffStrCopyN(pend, "/lib/*", sizeof(chocoPath) - (pend - chocoPath)); + char* pend = ffStrCopyN(chocoPath, chocoInstall, ARRAY_SIZE(chocoPath)); + ffStrCopyN(pend, "/lib/*", ARRAY_SIZE(chocoPath) - (size_t) (pend - chocoPath)); result->choco = getNumElements(chocoPath, FILE_ATTRIBUTE_DIRECTORY, "choco"); } @@ -71,8 +71,8 @@ static void detectPacman(FFPackagesResult* result) // MSYS2 char pacmanPath[MAX_PATH + 3]; - strcpy(pacmanPath, msystemPrefix); - strncat(pacmanPath, "/../var/lib/pacman/local/*", sizeof(pacmanPath) - 1 - strlen(pacmanPath)); + char* pend = ffStrCopyN(pacmanPath, msystemPrefix, ARRAY_SIZE(pacmanPath)); + ffStrCopyN(pend, "/../var/lib/pacman/local/*", ARRAY_SIZE(pacmanPath) - (size_t) (pend - pacmanPath)); result->pacman = getNumElements(pacmanPath, FILE_ATTRIBUTE_DIRECTORY, NULL); } diff --git a/src/detection/physicaldisk/physicaldisk_linux.c b/src/detection/physicaldisk/physicaldisk_linux.c index 848ad460b..58a8b91f2 100644 --- a/src/detection/physicaldisk/physicaldisk_linux.c +++ b/src/detection/physicaldisk/physicaldisk_linux.c @@ -16,7 +16,7 @@ static double detectNvmeTemp(int devfd) { pathHwmon[strlen("hwmon")] = c; char buffer[64]; - ssize_t size = ffReadFileDataRelative(devfd, pathHwmon, sizeof(buffer), buffer); + ssize_t size = ffReadFileDataRelative(devfd, pathHwmon, ARRAY_SIZE(buffer), buffer); if (size > 0) { buffer[size] = '\0'; @@ -58,7 +58,7 @@ static void parsePhysicalDisk(int dfd, const char* devName, FFPhysicalDiskOption if (!multiNs) { char pathSysBlock[32]; - snprintf(pathSysBlock, sizeof(pathSysBlock), "/dev/nvme%dn2", devid); + snprintf(pathSysBlock, ARRAY_SIZE(pathSysBlock), "/dev/nvme%dn2", devid); multiNs = access(pathSysBlock, F_OK) == 0; } if (multiNs) @@ -81,7 +81,7 @@ static void parsePhysicalDisk(int dfd, const char* devName, FFPhysicalDiskOption { ffStrbufInit(&device->interconnect); char pathSysDeviceReal[PATH_MAX]; - ssize_t pathLength = readlinkat(dfd, "device", pathSysDeviceReal, sizeof(pathSysDeviceReal) - 1); + ssize_t pathLength = readlinkat(dfd, "device", pathSysDeviceReal, ARRAY_SIZE(pathSysDeviceReal) - 1); if (pathLength > 0) { pathSysDeviceReal[pathLength] = '\0'; @@ -110,7 +110,7 @@ static void parsePhysicalDisk(int dfd, const char* devName, FFPhysicalDiskOption { char blkSize[32]; - ssize_t fileSize = ffReadFileDataRelative(dfd, "size", sizeof(blkSize) - 1, blkSize); + ssize_t fileSize = ffReadFileDataRelative(dfd, "size", ARRAY_SIZE(blkSize) - 1, blkSize); if (fileSize > 0) { blkSize[fileSize] = 0; @@ -169,8 +169,8 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) if (devName[0] == '.') continue; - char pathSysBlock[sizeof("/sys/block/") + sizeof(sysBlockEntry->d_name)]; - snprintf(pathSysBlock, sizeof(pathSysBlock), "/sys/block/%s", devName); + char pathSysBlock[ARRAY_SIZE("/sys/block/") + ARRAY_SIZE(sysBlockEntry->d_name)]; + snprintf(pathSysBlock, ARRAY_SIZE(pathSysBlock), "/sys/block/%s", devName); int dfd = openat(dirfd(sysBlockDirp), devName, O_RDONLY | O_CLOEXEC | O_PATH | O_DIRECTORY); if (dfd > 0) parsePhysicalDisk(dfd, devName, options, result); diff --git a/src/detection/processes/processes_bsd.c b/src/detection/processes/processes_bsd.c index 0cd169cfa..47d8d5166 100644 --- a/src/detection/processes/processes_bsd.c +++ b/src/detection/processes/processes_bsd.c @@ -15,7 +15,7 @@ const char* ffDetectProcesses(uint32_t* result) int request[] = {CTL_KERN, KERN_PROC, KERN_PROC_PROC}; size_t length; - if(sysctl(request, sizeof(request) / sizeof(*request), NULL, &length, NULL, 0) != 0) + if(sysctl(request, ARRAY_SIZE(request), NULL, &length, NULL, 0) != 0) return "sysctl({CTL_KERN, KERN_PROC, KERN_PROC_PROC}) failed"; *result = (uint32_t)(length / sizeof(struct kinfo_proc)); diff --git a/src/detection/swap/swap_bsd.c b/src/detection/swap/swap_bsd.c index 7cbb2a1fe..c50885bc9 100644 --- a/src/detection/swap/swap_bsd.c +++ b/src/detection/swap/swap_bsd.c @@ -6,7 +6,7 @@ const char* ffDetectSwap(FFSwapResult* swap) { int mib[16]; - size_t mibsize = sizeof(mib) / sizeof(*mib); + size_t mibsize = ARRAY_SIZE(mib); if (sysctlnametomib("vm.swap_info", mib, &mibsize) < 0) return "sysctlnametomib(\"vm.swap_info\") failed"; diff --git a/src/detection/swap/swap_linux.c b/src/detection/swap/swap_linux.c index 77d95d589..15f8fd24d 100644 --- a/src/detection/swap/swap_linux.c +++ b/src/detection/swap/swap_linux.c @@ -9,9 +9,9 @@ const char* ffDetectSwap(FFSwapResult* swap) { // #620 char buf[PROC_FILE_BUFFSIZ]; - ssize_t nRead = ffReadFileData("/proc/meminfo", sizeof(buf) - 1, buf); + ssize_t nRead = ffReadFileData("/proc/meminfo", ARRAY_SIZE(buf) - 1, buf); if(nRead < 0) - return "ffReadFileData(\"/proc/meminfo\", sizeof(buf)-1, buf)"; + return "ffReadFileData(\"/proc/meminfo\", ARRAY_SIZE(buf)-1, buf)"; buf[nRead] = '\0'; uint64_t swapTotal = 0, swapFree = 0; @@ -19,7 +19,7 @@ const char* ffDetectSwap(FFSwapResult* swap) char *token = NULL; if ((token = strstr(buf, "SwapTotal:")) != NULL) swapTotal = strtoul(token + strlen("SwapTotal:"), NULL, 10); - + if ((token = strstr(buf, "SwapFree:")) != NULL) swapFree = strtoul(token + strlen("SwapFree:"), NULL, 10); diff --git a/src/detection/terminalfont/terminalfont_windows.c b/src/detection/terminalfont/terminalfont_windows.c index 46b529ec5..6e59cbdfb 100644 --- a/src/detection/terminalfont/terminalfont_windows.c +++ b/src/detection/terminalfont/terminalfont_windows.c @@ -99,17 +99,17 @@ static void detectFromWindowsTerminal(const FFstrbuf* terminalExe, FFTerminalFon { char jsonPath[MAX_PATH + 1]; char* pathEnd = ffStrCopyN(jsonPath, terminalExe->chars, ffStrbufLastIndexC(terminalExe, '\\') + 1); - ffStrCopyN(pathEnd, ".portable", sizeof(jsonPath) - (size_t) (pathEnd - jsonPath) - 1); + ffStrCopyN(pathEnd, ".portable", ARRAY_SIZE(jsonPath) - (size_t) (pathEnd - jsonPath) - 1); if(ffPathExists(jsonPath, FF_PATHTYPE_ANY)) { - ffStrCopyN(pathEnd, "settings\\settings.json", sizeof(jsonPath) - (size_t) (pathEnd - jsonPath) - 1); + ffStrCopyN(pathEnd, "settings\\settings.json", ARRAY_SIZE(jsonPath) - (size_t) (pathEnd - jsonPath) - 1); if(!ffAppendFileBuffer(jsonPath, &json)) error = "Error reading Windows Terminal portable settings JSON file"; } else if(SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, jsonPath))) { - size_t remaining = sizeof(jsonPath) - strlen(jsonPath) - 1; + size_t remaining = ARRAY_SIZE(jsonPath) - strlen(jsonPath) - 1; if(ffStrbufContainIgnCaseS(terminalExe, "_8wekyb3d8bbwe\\")) { // Microsoft Store version @@ -175,7 +175,7 @@ static void detectFromWindowsTerminal(const FFstrbuf* terminalExe, FFTerminalFon else { char sizeStr[16]; - snprintf(sizeStr, sizeof(sizeStr), "%g", size); + snprintf(sizeStr, ARRAY_SIZE(sizeStr), "%g", size); ffFontInitValues(&terminalFont->font, name.chars, sizeStr); } } @@ -226,7 +226,7 @@ static void detectConEmu(FFTerminalFontResult* terminalFont) FF_STRBUF_AUTO_DESTROY fontSize = ffStrbufCreate(); const char* paths[] = { "ConEmuDir", "ConEmuBaseDir", "APPDATA" }; - for (uint32_t i = 0; i < sizeof(paths) / sizeof(paths[0]); ++i) + for (uint32_t i = 0; i < ARRAY_SIZE(paths); ++i) { ffStrbufSetS(&path, getenv(paths[i])); if(path.length > 0) diff --git a/src/detection/terminalshell/terminalshell.c b/src/detection/terminalshell/terminalshell.c index f75a56951..a68e33517 100644 --- a/src/detection/terminalshell/terminalshell.c +++ b/src/detection/terminalshell/terminalshell.c @@ -579,16 +579,16 @@ static bool getTerminalVersionKitty(FFstrbuf* exe, FFstrbuf* version) char buffer[1024] = {}; if ( #ifdef __linux__ - ffReadFileData(FASTFETCH_TARGET_DIR_USR "/lib64/kitty/kitty/constants.py", sizeof(buffer) - 1, buffer) || - ffReadFileData(FASTFETCH_TARGET_DIR_USR "/lib/kitty/kitty/constants.py", sizeof(buffer) - 1, buffer) + ffReadFileData(FASTFETCH_TARGET_DIR_USR "/lib64/kitty/kitty/constants.py", ARRAY_SIZE(buffer) - 1, buffer) || + ffReadFileData(FASTFETCH_TARGET_DIR_USR "/lib/kitty/kitty/constants.py", ARRAY_SIZE(buffer) - 1, buffer) #else - ffReadFileData(_PATH_LOCALBASE "/share/kitty/kitty/constants.py", sizeof(buffer) - 1, buffer) + ffReadFileData(_PATH_LOCALBASE "/share/kitty/kitty/constants.py", ARRAY_SIZE(buffer) - 1, buffer) #endif ) { // Starts from version 0.17.0 // https://github.com/kovidgoyal/kitty/blob/master/kitty/constants.py#L25 - const char* p = memmem(buffer, sizeof(buffer) - 1, "version: Version = Version(", strlen("version: Version = Version(")); + const char* p = memmem(buffer, ARRAY_SIZE(buffer) - 1, "version: Version = Version(", strlen("version: Version = Version(")); if (p) { p += strlen("version: Version = Version("); diff --git a/src/detection/uptime/uptime_linux.c b/src/detection/uptime/uptime_linux.c index 0e4532492..44705a5fc 100644 --- a/src/detection/uptime/uptime_linux.c +++ b/src/detection/uptime/uptime_linux.c @@ -7,10 +7,10 @@ const char* ffDetectUptime(FFUptimeResult* result) { #ifndef __ANDROID__ // cat: /proc/uptime: Permission denied - + // #620 char buf[64]; - ssize_t nRead = ffReadFileData("/proc/uptime", sizeof(buf) - 1, buf); + ssize_t nRead = ffReadFileData("/proc/uptime", ARRAY_SIZE(buf) - 1, buf); if(nRead > 0) { buf[nRead] = '\0'; diff --git a/src/detection/vulkan/vulkan.c b/src/detection/vulkan/vulkan.c index eb4c05ac8..caa6e68d3 100644 --- a/src/detection/vulkan/vulkan.c +++ b/src/detection/vulkan/vulkan.c @@ -123,7 +123,7 @@ static const char* detectVulkan(FFVulkanResult* result) instanceVersion.major = 1; VkPhysicalDevice physicalDevices[128]; - uint32_t physicalDeviceCount = (uint32_t) (sizeof(physicalDevices) / sizeof(*physicalDevices)); + uint32_t physicalDeviceCount = (uint32_t) ARRAY_SIZE(physicalDevices); res = ffvkEnumeratePhysicalDevices(vkInstance, &physicalDeviceCount, physicalDevices); if(res != VK_SUCCESS) { diff --git a/src/detection/wm/wm_apple.c b/src/detection/wm/wm_apple.c index 30f8e91e4..29b5497f8 100644 --- a/src/detection/wm/wm_apple.c +++ b/src/detection/wm/wm_apple.c @@ -9,7 +9,7 @@ const char* ffDetectWMPlugin(FFstrbuf* pluginName) { int request[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL}; - u_int requestLength = sizeof(request) / sizeof(*request); + u_int requestLength = ARRAY_SIZE(request); size_t length = 0; FF_AUTO_FREE struct kinfo_proc* processes = ffSysctlGetData(request, requestLength, &length); diff --git a/src/fastfetch.h b/src/fastfetch.h index 0d7a9a5d5..2dba4b012 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -25,6 +25,13 @@ #include "options/display.h" #include "options/general.h" +#if __has_builtin(__builtin_types_compatible_p) + #define ARRAY_SIZE(x) ({ static_assert(!__builtin_types_compatible_p(__typeof__(x), __typeof__(&*(x))), "Must not be a pointer"); sizeof(x) / sizeof(*(x)); }) +#else + #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) +#endif + + typedef struct FFconfig { FFOptionsLogo logo; diff --git a/src/flashfetch.c b/src/flashfetch.c index 2ec8f21bb..6aeb31bfe 100644 --- a/src/flashfetch.c +++ b/src/flashfetch.c @@ -49,7 +49,7 @@ int main(void) &options->colors, }; - for (size_t i = 0; i < sizeof(modules) / sizeof(modules[0]); i++) + for (size_t i = 0; i < ARRAY_SIZE(modules); i++) ((const FFModuleBaseInfo*) modules[i])->printModule(modules[i]); ffFinish(); diff --git a/src/modules/cpu/cpu.c b/src/modules/cpu/cpu.c index 0381b32f4..614b0bf26 100644 --- a/src/modules/cpu/cpu.c +++ b/src/modules/cpu/cpu.c @@ -39,7 +39,7 @@ void ffPrintCPU(FFCPUOptions* options) if (options->showPeCoreCount) { uint32_t typeCount = 0; - while (cpu.coreTypes[typeCount].count != 0 && typeCount < sizeof(cpu.coreTypes) / sizeof(cpu.coreTypes[0])) typeCount++; + while (cpu.coreTypes[typeCount].count != 0 && typeCount < ARRAY_SIZE(cpu.coreTypes)) typeCount++; if (typeCount > 0) { qsort(cpu.coreTypes, typeCount, sizeof(cpu.coreTypes[0]), (void*) sortCores); diff --git a/src/modules/datetime/datetime.c b/src/modules/datetime/datetime.c index a79a31c49..897e2c056 100644 --- a/src/modules/datetime/datetime.c +++ b/src/modules/datetime/datetime.c @@ -107,7 +107,7 @@ void ffPrintDateTime(FFDateTimeOptions* options) } char buffer[32]; - if (strftime(buffer, sizeof(buffer), "%F %T", tm) == 0) //yyyy-MM-dd HH:mm:ss + if (strftime(buffer, ARRAY_SIZE(buffer), "%F %T", tm) == 0) //yyyy-MM-dd HH:mm:ss { ffPrintError(FF_DATETIME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "strftime() failed"); return; diff --git a/src/modules/display/display.c b/src/modules/display/display.c index a1dcf78f0..e845ff164 100644 --- a/src/modules/display/display.c +++ b/src/modules/display/display.c @@ -146,9 +146,9 @@ void ffPrintDisplay(FFDisplayOptions* options) if(result->refreshRate > 0) { if(options->preciseRefreshRate) - snprintf(refreshRate, sizeof(refreshRate), "%g", ((int) (result->refreshRate * 1000 + 0.5)) / 1000.0); + snprintf(refreshRate, ARRAY_SIZE(refreshRate), "%g", ((int) (result->refreshRate * 1000 + 0.5)) / 1000.0); else - snprintf(refreshRate, sizeof(refreshRate), "%i", (uint32_t) (result->refreshRate + 0.5)); + snprintf(refreshRate, ARRAY_SIZE(refreshRate), "%i", (uint32_t) (result->refreshRate + 0.5)); } else refreshRate[0] = 0; @@ -157,7 +157,7 @@ void ffPrintDisplay(FFDisplayOptions* options) if (result->serial) { const uint8_t* nums = (uint8_t*) &result->serial; - snprintf(buf, sizeof(buf), "%2X-%2X-%2X-%2X", nums[0], nums[1], nums[2], nums[3]); + snprintf(buf, ARRAY_SIZE(buf), "%2X-%2X-%2X-%2X", nums[0], nums[1], nums[2], nums[3]); } else buf[0] = '\0'; diff --git a/src/modules/media/media.c b/src/modules/media/media.c index 6c7ac2c8e..a495c9dd4 100644 --- a/src/modules/media/media.c +++ b/src/modules/media/media.c @@ -63,7 +63,7 @@ void ffPrintMedia(FFMediaOptions* options) "[Lyric Video]", "[Official Lyric Video]", "[Lyrics]", "| Lyric Video", "| Official Lyric Video", "| Lyrics", }; - ffStrbufRemoveStrings(&songPretty, sizeof(removeStrings) / sizeof(removeStrings[0]), removeStrings); + ffStrbufRemoveStrings(&songPretty, ARRAY_SIZE(removeStrings), removeStrings); ffStrbufTrimRight(&songPretty, ' '); if(songPretty.length == 0) diff --git a/src/modules/terminaltheme/terminaltheme.c b/src/modules/terminaltheme/terminaltheme.c index 93e702918..5ff7e720e 100644 --- a/src/modules/terminaltheme/terminaltheme.c +++ b/src/modules/terminaltheme/terminaltheme.c @@ -32,8 +32,8 @@ void ffPrintTerminalTheme(FFTerminalThemeOptions* options) char fg[32], bg[32]; const char* fgType = result.fg.dark ? "Dark" : "Light"; const char* bgType = result.bg.dark ? "Dark" : "Light"; - snprintf(fg, sizeof(fg), "#%02" PRIX16 "%02" PRIX16 "%02" PRIX16, result.fg.r, result.fg.g, result.fg.b); - snprintf(bg, sizeof(bg), "#%02" PRIX16 "%02" PRIX16 "%02" PRIX16, result.bg.r, result.bg.g, result.bg.b); + snprintf(fg, ARRAY_SIZE(fg), "#%02" PRIX16 "%02" PRIX16 "%02" PRIX16, result.fg.r, result.fg.g, result.fg.b); + snprintf(bg, ARRAY_SIZE(bg), "#%02" PRIX16 "%02" PRIX16 "%02" PRIX16, result.bg.r, result.bg.g, result.bg.b); FF_PRINT_FORMAT_CHECKED(FF_TERMINALTHEME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, FF_TERMINALTHEME_NUM_FORMAT_ARGS, ((FFformatarg[]){ FF_FORMAT_ARG(fg, "fg-color"), FF_FORMAT_ARG(fgType, "fg-type"), diff --git a/src/util/platform/FFPlatform_windows.c b/src/util/platform/FFPlatform_windows.c index c66b0ab10..5357bde3d 100644 --- a/src/util/platform/FFPlatform_windows.c +++ b/src/util/platform/FFPlatform_windows.c @@ -139,7 +139,7 @@ static void getUserName(FFPlatform* platform) else { wchar_t buffer[128]; - DWORD len = sizeof(buffer) / sizeof(*buffer); + DWORD len = ARRAY_SIZE(buffer); if(GetUserNameW(buffer, &len)) ffStrbufSetWS(&platform->userName, buffer); } @@ -148,7 +148,7 @@ static void getUserName(FFPlatform* platform) static void getHostName(FFPlatform* platform) { wchar_t buffer[128]; - DWORD len = sizeof(buffer) / sizeof(*buffer); + DWORD len = ARRAY_SIZE(buffer); if(GetComputerNameExW(ComputerNameDnsHostname, buffer, &len)) ffStrbufSetWS(&platform->hostName, buffer); }