Fix out-of-bounds access in Platform::getCPUName (#1115)

This commit is contained in:
vfjpl 2020-11-22 22:08:37 +01:00 committed by GitHub
parent c6d0fc0f71
commit 72cc4b2fb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -130,12 +130,13 @@ std::string Platform::getCPUName()
std::ifstream in("/proc/cpuinfo");
while(getline(in, line)) {
auto strs = stdext::split(line, ":");
std::string first = strs[0];
std::string second = strs[1];
stdext::trim(first);
stdext::trim(second);
if(strs.size() == 2 && first == "model name")
return second;
if(strs.size() == 2) {
stdext::trim(strs[0]);
if(strs[0] == "model name") {
stdext::trim(strs[1]);
return strs[1];
}
}
}
return std::string();
}
@ -146,12 +147,13 @@ double Platform::getTotalSystemMemory()
std::ifstream in("/proc/meminfo");
while(getline(in, line)) {
auto strs = stdext::split(line, ":");
std::string first = strs[0];
std::string second = strs[1];
stdext::trim(first);
stdext::trim(second);
if(strs.size() == 2 && first == "MemTotal")
return stdext::unsafe_cast<double>(second.substr(0, second.length() - 3)) * 1000.0;
if(strs.size() == 2) {
stdext::trim(strs[0]);
if(strs[0] == "MemTotal") {
stdext::trim(strs[1]);
return stdext::unsafe_cast<double>(strs[1].substr(0, strs[1].length() - 3)) * 1000;
}
}
}
return 0;
}
@ -161,8 +163,7 @@ std::string Platform::getOSName()
std::string line;
std::ifstream in("/etc/issue");
if(getline(in, line)) {
std::size_t end = line.find('\\');
std::string res = line.substr(0, end);
auto res = line.substr(0, line.find('\\'));
stdext::trim(res);
return res;
}