diff --git a/RichString.c b/RichString.c index fcb4e3589..e1350d16b 100644 --- a/RichString.c +++ b/RichString.c @@ -190,7 +190,7 @@ static inline int RichString_writeFromWide(RichString* this, int attrs, const ch } this->chptr[newLen] = 0; - return len; + return (int)len; } int RichString_appendnWideColumns(RichString* this, int attrs, const char* data_c, size_t len, int* columns) { diff --git a/linux/LibSensors.c b/linux/LibSensors.c index e4e924be8..55ebcf6af 100644 --- a/linux/LibSensors.c +++ b/linux/LibSensors.c @@ -206,6 +206,14 @@ int LibSensors_countCCDs(void) { return ccds; } +static int LibSensors_stringToID(const char* str) { + char* endptr; + unsigned long parsedID = strtoul(str, &endptr, 10); + if (parsedID >= INT_MAX || *endptr != '\0') + return -1; + return (int)parsedID; +} + void LibSensors_getCPUTemperatures(CPUData* cpus, unsigned int existingCPUs, unsigned int activeCPUs) { assert(existingCPUs > 0 && existingCPUs < 16384); @@ -344,17 +352,21 @@ void LibSensors_getCPUTemperatures(CPUData* cpus, unsigned int existingCPUs, uns char *label = sym_sensors_get_label(chip, feature); if (label) { bool skip = true; + int ID; /* Intel coretemp names, labels mention package and physical id */ if (String_startsWith(label, "Package id ")) { - physicalID = strtoul(label + strlen("Package id "), NULL, 10); + if ((ID = LibSensors_stringToID(label + strlen("Package id "))) != -1) + physicalID = ID; } else if (String_startsWith(label, "Physical id ")) { - physicalID = strtoul(label + strlen("Physical id "), NULL, 10); + if ((ID = LibSensors_stringToID(label + strlen("Physical id "))) != -1) + physicalID = ID; } else if (String_startsWith(label, "Core ")) { - int coreID = strtoul(label + strlen("Core "), NULL, 10); - for (size_t i = 1; i < existingCPUs + 1; i++) { - if (cpus[i].physicalID == physicalID && cpus[i].coreID == coreID) { - data[i] = temp; - coreTempCount++; + if ((ID = LibSensors_stringToID(label + strlen("Core "))) != -1) { + for (size_t i = 1; i < existingCPUs + 1; i++) { + if (cpus[i].physicalID == physicalID && cpus[i].coreID == ID) { + data[i] = temp; + coreTempCount++; + } } } } diff --git a/linux/LinuxProcessTable.c b/linux/LinuxProcessTable.c index 7aca7383e..3b07b5869 100644 --- a/linux/LinuxProcessTable.c +++ b/linux/LinuxProcessTable.c @@ -77,6 +77,14 @@ static FILE* fopenat(openat_arg_t openatArg, const char* pathname, const char* m return fp; } +static pid_t strtopid(const char* str) { + char* endptr; + unsigned long parsedPid = strtoul(str, &endptr, 10); + if (parsedPid == 0 || parsedPid >= INT_MAX || *endptr != '\0') + return 0; // indicate failure by an invalid pid + return (pid_t)parsedPid; +} + static inline uint64_t fast_strtoull_dec(char** str, size_t maxlen) { uint64_t result = 0; @@ -982,8 +990,8 @@ static void LinuxProcessTable_readOpenVZData(LinuxProcess* process, openat_arg_t free_and_xStrdup(&process->ctid, name_value_sep); break; case 2: - foundVPid = true; - process->vpid = strtoul(name_value_sep, NULL, 0); + if ((process->vpid = strtopid(name_value_sep)) != 0) + foundVPid = true; break; default: //Sanity Check: Should never reach here, or the implementation is missing something! @@ -1582,14 +1590,9 @@ static bool LinuxProcessTable_recurseProcTree(LinuxProcessTable* this, openat_ar } // filename is a number: process directory - int pid; - { - char* endptr; - unsigned long parsedPid = strtoul(name, &endptr, 10); - if (parsedPid == 0 || parsedPid >= INT_MAX || *endptr != '\0') - continue; - pid = (int)parsedPid; - } + pid_t pid = strtopid(name); + if (pid == 0) + continue; // Skip task directory of main thread if (mainTask && pid == Process_getPid(&mainTask->super))