Fix stoi out_of_range issue (#19455)

* Fix `stoi` out_of_range issue

* Handle incorrect behavior and throw exception

* Remove use of `getVmSizeInKB()` in `TestsCommon()`
This commit is contained in:
Vitaliy Urusovskij 2023-08-31 04:19:13 +04:00 committed by GitHub
parent 6ad11108b5
commit 81a02c5586
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 44 deletions

View File

@ -56,7 +56,8 @@ std::string generateTestFilePrefix() {
static PROCESS_MEMORY_COUNTERS getMemoryInfo() { static PROCESS_MEMORY_COUNTERS getMemoryInfo() {
static PROCESS_MEMORY_COUNTERS pmc; static PROCESS_MEMORY_COUNTERS pmc;
pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS); pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS);
GetProcessMemoryInfo(GetCurrentProcess(), &pmc, pmc.cb); if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, pmc.cb))
throw std::runtime_error("Can't get system memory values");
return pmc; return pmc;
} }
@ -70,32 +71,34 @@ size_t getVmRSSInKB() {
#else #else
/// Parses number from provided string
static int parseLine(std::string line) {
std::string res = "";
for (auto c : line)
if (isdigit(c))
res += c;
if (res.empty())
// If number wasn't found return -1
return -1;
return std::stoi(res);
}
size_t getSystemDataByName(char* name) { size_t getSystemDataByName(char* name) {
auto parseLine = [](std::string line) -> size_t {
std::string res = "";
for (auto c : line)
if (isdigit(c))
res += c;
if (res.empty())
throw std::runtime_error("Can't get system memory values");
return std::stoul(res);
};
FILE* file = fopen("/proc/self/status", "r"); FILE* file = fopen("/proc/self/status", "r");
size_t result = 0; size_t result = 0;
bool status = false;
if (file != nullptr) { if (file != nullptr) {
char line[128]; char line[128];
while (fgets(line, 128, file) != NULL) { while (fgets(line, 128, file) != NULL) {
if (strncmp(line, name, strlen(name)) == 0) { if (strncmp(line, name, strlen(name)) == 0) {
result = parseLine(line); result = parseLine(line);
status = true;
break; break;
} }
} }
fclose(file); fclose(file);
} }
if (!status)
throw std::runtime_error("Can't get system memory values");
return result; return result;
} }

View File

@ -30,10 +30,12 @@ TestsCommon::TestsCommon()
: PGLink(new utils::PostgreSQLLink(this)) : PGLink(new utils::PostgreSQLLink(this))
#endif #endif
{ {
#ifndef __APPLE__ // TODO: add getVmSizeInKB() for Apple platform
auto memsize = ov::test::utils::getVmSizeInKB(); auto memsize = ov::test::utils::getVmSizeInKB();
if (memsize != 0) { if (memsize != 0) {
std::cout << "\nMEM_USAGE=" << memsize << "KB\n"; std::cout << "\nMEM_USAGE=" << memsize << "KB\n";
} }
#endif
ov::threading::executor_manager()->clear(); ov::threading::executor_manager()->clear();
} }

View File

@ -27,8 +27,8 @@ namespace MemoryTest {
static PROCESS_MEMORY_COUNTERS getMemoryInfo() { static PROCESS_MEMORY_COUNTERS getMemoryInfo() {
static PROCESS_MEMORY_COUNTERS pmc; static PROCESS_MEMORY_COUNTERS pmc;
pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS); pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS);
GetProcessMemoryInfo(GetCurrentProcess(),&pmc, pmc.cb); if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, pmc.cb))
return pmc; throw std::runtime_error("Can't get system memory values");
} }
size_t getVmSizeInKB() { size_t getVmSizeInKB() {
@ -72,32 +72,34 @@ namespace MemoryTest {
#else #else
/// Parses number from provided string size_t getSystemDataByName(char* name) {
static int parseLine(std::string line) { auto parseLine = [](std::string line) -> size_t {
std::string res = ""; std::string res = "";
for (auto c: line) for (auto c : line)
if (isdigit(c)) if (isdigit(c))
res += c; res += c;
if (res.empty()) if (res.empty())
// If number wasn't found return -1 throw std::runtime_error("Can't get system memory values");
return -1; return std::stoul(res);
return std::stoi(res); };
}
size_t getSystemDataByName(char *name) { FILE* file = fopen("/proc/self/status", "r");
FILE *file = fopen("/proc/self/status", "r");
size_t result = 0; size_t result = 0;
bool status = false;
if (file != nullptr) { if (file != nullptr) {
char line[128]; char line[128];
while (fgets(line, 128, file) != NULL) { while (fgets(line, 128, file) != NULL) {
if (strncmp(line, name, strlen(name)) == 0) { if (strncmp(line, name, strlen(name)) == 0) {
result = parseLine(line); result = parseLine(line);
status = true;
break; break;
} }
} }
fclose(file); fclose(file);
} }
if (!status)
throw std::runtime_error("Can't get system memory values");
return result; return result;
} }

View File

@ -33,25 +33,12 @@ std::string fileNameNoExt(const std::string &filepath) {
return filepath.substr(0, pos); return filepath.substr(0, pos);
} }
/// Parses number from provided string
static int parseLine(std::string line) {
std::string res = "";
for (auto c: line)
if (isdigit(c))
res += c;
if (res.empty())
// If number wasn't found return -1
return -1;
return std::stoi(res);
}
#ifdef _WIN32 #ifdef _WIN32
static PROCESS_MEMORY_COUNTERS getMemoryInfo() { static PROCESS_MEMORY_COUNTERS getMemoryInfo() {
static PROCESS_MEMORY_COUNTERS pmc; static PROCESS_MEMORY_COUNTERS pmc;
pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS); pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS);
GetProcessMemoryInfo(GetCurrentProcess(),&pmc, pmc.cb); if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, pmc.cb))
return pmc; throw std::runtime_error("Can't get system memory values");
} }
size_t getVmSizeInKB() { size_t getVmSizeInKB() {
@ -94,20 +81,34 @@ size_t getThreadsNum() {
} }
#else #else
size_t getSystemDataByName(char *name){ size_t getSystemDataByName(char* name) {
auto parseLine = [](std::string line) -> size_t {
std::string res = "";
for (auto c : line)
if (isdigit(c))
res += c;
if (res.empty())
throw std::runtime_error("Can't get system memory values");
return std::stoul(res);
};
FILE* file = fopen("/proc/self/status", "r"); FILE* file = fopen("/proc/self/status", "r");
size_t result = 0; size_t result = 0;
bool status = false;
if (file != nullptr) { if (file != nullptr) {
char line[128]; char line[128];
while (fgets(line, 128, file) != NULL) { while (fgets(line, 128, file) != NULL) {
if (strncmp(line, name, strlen(name)) == 0) { if (strncmp(line, name, strlen(name)) == 0) {
result = parseLine(line); result = parseLine(line);
status = true;
break; break;
} }
} }
fclose(file); fclose(file);
} }
if (!status)
throw std::runtime_error("Can't get system memory values");
return result; return result;
} }