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