From d88e2eeb36502aec598ae33540edd1c4a4baf404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sat, 18 May 2024 18:22:32 +0800 Subject: [PATCH] CPU (macOS): fix compiling --- src/detection/cpu/cpu_apple.c | 12 +++++++----- src/modules/cpu/cpu.c | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/detection/cpu/cpu_apple.c b/src/detection/cpu/cpu_apple.c index 85e06cde2..efffa6d41 100644 --- a/src/detection/cpu/cpu_apple.c +++ b/src/detection/cpu/cpu_apple.c @@ -82,17 +82,19 @@ static const char* detectFrequency(FFCPUResult* cpu) static const char* detectCoreCount(FFCPUResult* cpu) { - int nPerfLevels = ffSysctlGetInt("hw.nperflevels", 0); + uint32_t nPerfLevels = (uint32_t) ffSysctlGetInt("hw.nperflevels", 0); if (nPerfLevels <= 0) return "sysctl(hw.nperflevels) failed"; char sysctlKey[] = "hw.perflevelN.logicalcpu"; - for (int i = 0; i < nPerfLevels; ++i) + if (nPerfLevels > sizeof(cpu->coreTypes) / sizeof(cpu->coreTypes[0])) + nPerfLevels = sizeof(cpu->coreTypes) / sizeof(cpu->coreTypes[0]); + for (uint32_t i = 0; i < nPerfLevels; ++i) { sysctlKey[strlen("hw.perflevel")] = (char) ('0' + i); - cpu->coreTypes[i < (int) sizeof(cpu->coreTypes) / sizeof(cpu->coreTypes[0]) : i : (int) sizeof(cpu->coreTypes) / sizeof(cpu->coreTypes[0]) - 1] = { + cpu->coreTypes[i] = (FFCPUCore) { .freq = nPerfLevels - i, - .count = ffSysctlGetInt(sysctlKey, 0), - } + .count = (uint32_t) ffSysctlGetInt(sysctlKey, 0), + }; } return NULL; } diff --git a/src/modules/cpu/cpu.c b/src/modules/cpu/cpu.c index c75e679be..16692c508 100644 --- a/src/modules/cpu/cpu.c +++ b/src/modules/cpu/cpu.c @@ -73,7 +73,7 @@ void ffPrintCPU(FFCPUOptions* options) { FF_STRBUF_AUTO_DESTROY coreTypes = ffStrbufCreate(); uint32_t typeCount = 0; - while (cpu.coreTypes[typeCount].count != 0) typeCount++; + while (cpu.coreTypes[typeCount].count != 0 && typeCount < sizeof(cpu.coreTypes) / sizeof(cpu.coreTypes[0])) typeCount++; if (typeCount > 0) { qsort(cpu.coreTypes, typeCount, sizeof(cpu.coreTypes[0]), (void*) sortCores);