From dcbdb7360d44b4071ec0fe0757a0875a12147c8a Mon Sep 17 00:00:00 2001 From: Jos Dehaes Date: Wed, 23 Aug 2023 15:46:47 +0200 Subject: [PATCH 1/3] [macos] fix temp sensor on system with many cores --- src/osx/smc.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/osx/smc.cpp b/src/osx/smc.cpp index 95d75f0..ace9ce6 100644 --- a/src/osx/smc.cpp +++ b/src/osx/smc.cpp @@ -18,6 +18,9 @@ tab-size = 4 #include "smc.hpp" +static constexpr size_t MaxIndexCount = sizeof("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") - 1; +static constexpr const char *KeyIndexes = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + static UInt32 _strtoul(char *str, int size, int base) { UInt32 total = 0; int i; @@ -91,13 +94,16 @@ namespace Cpu { // according to VirtualSMC docs (hackintosh fake SMC) the enumeration follows with alphabetic chars - not implemented yet here (nor in VirtualSMC) long long SMCConnection::getTemp(int core) { char key[] = SMC_KEY_CPU_TEMP; + if (core > MaxIndexCount) { + return -1; + } if (core >= 0) { - snprintf(key, 5, "TC%1dc", core); + snprintf(key, 5, "TC%1cc", KeyIndexes[core]); } long long result = getSMCTemp(key); if (result == -1) { // try again with C - snprintf(key, 5, "TC%1dC", core); + snprintf(key, 5, "TC%1dC", KeyIndexes[core]); result = getSMCTemp(key); } return result; From 4d8aa6b11896dac99f81019e6dea11cc8d8856f1 Mon Sep 17 00:00:00 2001 From: Jos Dehaes Date: Fri, 25 Aug 2023 15:52:58 +0200 Subject: [PATCH 2/3] fix core check --- src/osx/smc.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/osx/smc.cpp b/src/osx/smc.cpp index ace9ce6..36ac4d9 100644 --- a/src/osx/smc.cpp +++ b/src/osx/smc.cpp @@ -94,10 +94,10 @@ namespace Cpu { // according to VirtualSMC docs (hackintosh fake SMC) the enumeration follows with alphabetic chars - not implemented yet here (nor in VirtualSMC) long long SMCConnection::getTemp(int core) { char key[] = SMC_KEY_CPU_TEMP; - if (core > MaxIndexCount) { - return -1; - } if (core >= 0) { + if ((size_t)core > MaxIndexCount) { + return -1; + } snprintf(key, 5, "TC%1cc", KeyIndexes[core]); } long long result = getSMCTemp(key); From d17e1a2dac79458940319d7117a21bdcd73ed53c Mon Sep 17 00:00:00 2001 From: Jos Dehaes Date: Fri, 25 Aug 2023 16:18:39 +0200 Subject: [PATCH 3/3] fix some warnings --- src/osx/smc.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/osx/smc.cpp b/src/osx/smc.cpp index 36ac4d9..6c483db 100644 --- a/src/osx/smc.cpp +++ b/src/osx/smc.cpp @@ -37,7 +37,7 @@ static UInt32 _strtoul(char *str, int size, int base) { static void _ultostr(char *str, UInt32 val) { str[0] = '\0'; - sprintf(str, "%c%c%c%c", + snprintf(str, 5, "%c%c%c%c", (unsigned int)val >> 24, (unsigned int)val >> 16, (unsigned int)val >> 8, @@ -47,10 +47,8 @@ static void _ultostr(char *str, UInt32 val) { namespace Cpu { SMCConnection::SMCConnection() { - IOMasterPort(kIOMasterPortDefault, &masterPort); - CFMutableDictionaryRef matchingDictionary = IOServiceMatching("AppleSMC"); - result = IOServiceGetMatchingServices(masterPort, matchingDictionary, &iterator); + result = IOServiceGetMatchingServices(0, matchingDictionary, &iterator); if (result != kIOReturnSuccess) { throw std::runtime_error("failed to get AppleSMC"); }