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 pmc;
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;
}
@ -70,32 +71,34 @@ size_t getVmRSSInKB() {
#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) {
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");
size_t result = 0;
bool status = false;
if (file != nullptr) {
char line[128];
while (fgets(line, 128, file) != NULL) {
if (strncmp(line, name, strlen(name)) == 0) {
result = parseLine(line);
status = true;
break;
}
}
fclose(file);
}
if (!status)
throw std::runtime_error("Can't get system memory values");
return result;
}

View File

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

View File

@ -27,8 +27,8 @@ namespace MemoryTest {
static PROCESS_MEMORY_COUNTERS getMemoryInfo() {
static PROCESS_MEMORY_COUNTERS pmc;
pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS);
GetProcessMemoryInfo(GetCurrentProcess(),&pmc, pmc.cb);
return pmc;
if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, pmc.cb))
throw std::runtime_error("Can't get system memory values");
}
size_t getVmSizeInKB() {
@ -72,32 +72,34 @@ namespace MemoryTest {
#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) {
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);
};
size_t getSystemDataByName(char *name) {
FILE *file = fopen("/proc/self/status", "r");
FILE* file = fopen("/proc/self/status", "r");
size_t result = 0;
bool status = false;
if (file != nullptr) {
char line[128];
while (fgets(line, 128, file) != NULL) {
if (strncmp(line, name, strlen(name)) == 0) {
result = parseLine(line);
status = true;
break;
}
}
fclose(file);
}
if (!status)
throw std::runtime_error("Can't get system memory values");
return result;
}

View File

@ -33,25 +33,12 @@ std::string fileNameNoExt(const std::string &filepath) {
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
static PROCESS_MEMORY_COUNTERS getMemoryInfo() {
static PROCESS_MEMORY_COUNTERS pmc;
pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS);
GetProcessMemoryInfo(GetCurrentProcess(),&pmc, pmc.cb);
return pmc;
if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, pmc.cb))
throw std::runtime_error("Can't get system memory values");
}
size_t getVmSizeInKB() {
@ -94,20 +81,34 @@ size_t getThreadsNum() {
}
#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");
size_t result = 0;
bool status = false;
if (file != nullptr) {
char line[128];
while (fgets(line, 128, file) != NULL) {
if (strncmp(line, name, strlen(name)) == 0) {
result = parseLine(line);
status = true;
break;
}
}
fclose(file);
}
if (!status)
throw std::runtime_error("Can't get system memory values");
return result;
}