Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compile-time warnings and other nits #30

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ smctemp.dSYM
*.a
*.o
.DS_Store
*.swp
4 changes: 1 addition & 3 deletions main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ int main(int argc, char *argv[]) {

kern_return_t result;
int op = smctemp::kOpNone;
smctemp::UInt32Char_t key = { 0 };
smctemp::SmcVal_t val;
Comment on lines -26 to -27
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note:

main.cc:26:26: warning: unused variable 'key' [-Wunused-variable]
main.cc:27:26: warning: unused variable 'val' [-Wunused-variable]


while ((c = getopt(argc, argv, "clvhn:g")) != -1) {
switch(c) {
Expand Down Expand Up @@ -91,7 +89,7 @@ int main(int argc, char *argv[]) {
attempts--;
}
}
std::cout << std::fixed << std::setprecision(1) << temp;
std::cout << std::fixed << std::setprecision(1) << temp << std::endl;
break;
}

Expand Down
60 changes: 30 additions & 30 deletions smctemp.cc
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void printFP(SmcVal_t val, int n, float m) {
void printUInt(SmcVal_t val) {
char* bytes = (char *)val.bytes;
uint64_t data = 0;
for (int i = 0; i < val.dataSize; i++) {
for (uint32_t i = 0; i < val.dataSize; i++) {
data += uint8_t(bytes[i]) * std::pow(256, val.dataSize - 1 -i);
}
std::cout << data;
Expand Down Expand Up @@ -112,10 +112,10 @@ void printPWM(SmcVal_t val) {

void printBytesHex(SmcVal_t val) {
std::cout << " (bytes:";
for (int i = 0; i < val.dataSize; i++) {
for (uint32_t i = 0; i < val.dataSize; i++) {
std::ios_base::fmtflags f(std::cout.flags());
std::cout << " " << std::setw(2)
<< std::uppercase<< std::hex << std::setfill('0')
std::cout << " " << std::setw(2)
<< std::uppercase<< std::hex << std::setfill('0')
<< static_cast<unsigned int>(val.bytes[i]);
std::cout.flags(f);
}
Expand Down Expand Up @@ -171,7 +171,7 @@ kern_return_t SmcAccessor::Open() {
std::cerr.flags(ef);
return result;
}

result = IOServiceOpen(device, mach_task_self(), 0, &conn_);
IOObjectRelease(device);
if (result != kIOReturnSuccess) {
Expand All @@ -193,15 +193,15 @@ kern_return_t SmcAccessor::Call(int index, SmcKeyData_t *inputStructure, SmcKeyD
size_t structureOutputSize;
structureInputSize = sizeof(SmcKeyData_t);
structureOutputSize = sizeof(SmcKeyData_t);

return IOConnectCallStructMethod(conn_, index, inputStructure, structureInputSize, outputStructure, &structureOutputSize);
}
kern_return_t SmcCall2(int index, SmcKeyData_t *inputStructure, SmcKeyData_t *outputStructure,io_connect_t conn) {
size_t structureInputSize;
size_t structureOutputSize;
structureInputSize = sizeof(SmcKeyData_t);
structureOutputSize = sizeof(SmcKeyData_t);

return IOConnectCallStructMethod(conn, index, inputStructure, structureInputSize, outputStructure, &structureOutputSize);
}

Expand All @@ -210,7 +210,7 @@ kern_return_t SmcAccessor::GetKeyInfo(const uint32_t key, SmcKeyData_keyInfo_t&
SmcKeyData_t inputStructure;
SmcKeyData_t outputStructure;
kern_return_t result = kIOReturnSuccess;

OSSpinLockLock(&g_keyInfoSpinLock);
int i = 0;
for (i = 0; i < g_keyInfoCacheCount; ++i) {
Expand All @@ -219,15 +219,15 @@ kern_return_t SmcAccessor::GetKeyInfo(const uint32_t key, SmcKeyData_keyInfo_t&
break;
}
}

if (i == g_keyInfoCacheCount) {
// Not in cache, must look it up.
memset(&inputStructure, 0, sizeof(inputStructure));
memset(&outputStructure, 0, sizeof(outputStructure));

inputStructure.key = key;
inputStructure.data8 = kSmcCmdReadKeyInfo;

result = Call(kKernelIndexSmc, &inputStructure, &outputStructure);
if (result == kIOReturnSuccess) {
key_info = outputStructure.keyInfo;
Expand All @@ -238,9 +238,9 @@ kern_return_t SmcAccessor::GetKeyInfo(const uint32_t key, SmcKeyData_keyInfo_t&
}
}
}

OSSpinLockUnlock(&g_keyInfoSpinLock);

return result;
}

Expand All @@ -255,7 +255,7 @@ double SmcAccessor::ReadValue(const UInt32Char_t key) {
std::string(val.dataType) == kDataTypeUi64) {
char* bytes = (char *)val.bytes;
uint64_t tmp = 0;
for (int i = 0; i < val.dataSize; i++) {
for (uint32_t i = 0; i < val.dataSize; i++) {
tmp += uint8_t(bytes[i]) * std::pow(256, val.dataSize - 1 -i);
}
v = tmp;
Expand Down Expand Up @@ -317,37 +317,37 @@ kern_return_t SmcAccessor::ReadSmcVal(const UInt32Char_t key, SmcVal_t& val) {
kern_return_t result;
SmcKeyData_t inputStructure;
SmcKeyData_t outputStructure;

memset(&inputStructure, 0, sizeof(SmcKeyData_t));
memset(&outputStructure, 0, sizeof(SmcKeyData_t));
memset(&val, 0, sizeof(SmcVal_t));

inputStructure.key = string_util::strtoul(key, 4, 16);
sprintf(val.key, key);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note:

smctemp.cc:326:3: warning: 'sprintf' is deprecated: 
This function is provided for compatibility reasons only.  
Due to security concerns inherent in the design of sprintf(3), 
it is highly recommended that you use snprintf(3) instead. [-Wdeprecated-declarations]

snprintf(val.key, sizeof(val.key), key);

result = GetKeyInfo(inputStructure.key, outputStructure.keyInfo);
if (result != kIOReturnSuccess) {
return result;
}

val.dataSize = outputStructure.keyInfo.dataSize;
string_util::ultostr(val.dataType, outputStructure.keyInfo.dataType);
string_util::ultostr(val.dataType, 5, outputStructure.keyInfo.dataType);
inputStructure.keyInfo.dataSize = val.dataSize;
inputStructure.data8 = kSmcCmdReadBytes;

result = SmcCall2(kKernelIndexSmc, &inputStructure, &outputStructure, conn_);
if (result != kIOReturnSuccess) {
return result;
}

memcpy(val.bytes, outputStructure.bytes, sizeof(outputStructure.bytes));

return kIOReturnSuccess;
}

uint32_t SmcAccessor::ReadIndexCount() {
SmcVal_t val;

ReadSmcVal("#KEY", val);
return string_util::strtoul((const char *)val.bytes, val.dataSize, 10);
}
Expand All @@ -356,29 +356,29 @@ kern_return_t SmcAccessor::PrintAll() {
kern_return_t result;
SmcKeyData_t inputStructure;
SmcKeyData_t outputStructure;

int totalKeys, i;
UInt32Char_t key;
SmcVal_t val;

totalKeys = ReadIndexCount();
for (i = 0; i < totalKeys; i++) {
memset(&inputStructure, 0, sizeof(SmcKeyData_t));
memset(&outputStructure, 0, sizeof(SmcKeyData_t));
memset(&val, 0, sizeof(SmcVal_t));

inputStructure.data8 = kSmcCmdReadIndex;
inputStructure.data32 = i;

result = Call(kKernelIndexSmc, &inputStructure, &outputStructure);
if (result != kIOReturnSuccess)
continue;
string_util::ultostr(key, outputStructure.key);

string_util::ultostr(key, 5, outputStructure.key);
ReadSmcVal(key, val);
PrintSmcVal(val);
}

return kIOReturnSuccess;
}

Expand Down
Empty file modified smctemp.h
100755 → 100644
Empty file.
4 changes: 2 additions & 2 deletions smctemp_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ uint32_t strtoul(const char* str, int size, int base) {
return total;
}

void ultostr(char *str, uint32_t val) {
void ultostr(char* str, size_t strlen, uint32_t val) {
str[0] = '\0';
sprintf(str, "%c%c%c%c",
snprintf(str, strlen, "%c%c%c%c",
(unsigned int) val >> 24,
(unsigned int) val >> 16,
(unsigned int) val >> 8,
Expand Down
3 changes: 2 additions & 1 deletion smctemp_string.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#ifndef SMCTEMP_SMCTEMP_STRING_H_
#define SMCTEMP_SMCTEMP_STRING_H_
#include <cstdint>
#include <cstdlib>

namespace smctemp {
namespace string_util {
uint32_t strtoul(const char * str, int size, int base);
void ultostr(char *str, uint32_t val);
void ultostr(char* str, size_t strlen, uint32_t val);
}
}
#endif // #ifndef SMCTEMP_SMCTEMP_STRING_H_
Expand Down
Loading