From 72cc4b2fb0939aa472b4039fa75118ce7f18fd88 Mon Sep 17 00:00:00 2001 From: vfjpl Date: Sun, 22 Nov 2020 22:08:37 +0100 Subject: [PATCH] Fix out-of-bounds access in Platform::getCPUName (#1115) --- src/framework/platform/unixplatform.cpp | 29 +++++++++++++------------ 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/framework/platform/unixplatform.cpp b/src/framework/platform/unixplatform.cpp index 95f03a28..0d3edc3c 100644 --- a/src/framework/platform/unixplatform.cpp +++ b/src/framework/platform/unixplatform.cpp @@ -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(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(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; }