From 81a02c5586d5b658c88d789f2d7b986ad9b0f2d7 Mon Sep 17 00:00:00 2001 From: Vitaliy Urusovskij Date: Thu, 31 Aug 2023 04:19:13 +0400 Subject: [PATCH] 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()` --- .../common_test_utils/src/common_utils.cpp | 29 ++++++++-------- .../common_test_utils/src/test_common.cpp | 2 ++ .../memory_tests_helper/memory_counter.cpp | 32 +++++++++--------- tests/stress_tests/common/utils.cpp | 33 ++++++++++--------- 4 files changed, 52 insertions(+), 44 deletions(-) diff --git a/src/tests/test_utils/common_test_utils/src/common_utils.cpp b/src/tests/test_utils/common_test_utils/src/common_utils.cpp index 5f7efd0dc64..f4cea296bb0 100644 --- a/src/tests/test_utils/common_test_utils/src/common_utils.cpp +++ b/src/tests/test_utils/common_test_utils/src/common_utils.cpp @@ -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; } diff --git a/src/tests/test_utils/common_test_utils/src/test_common.cpp b/src/tests/test_utils/common_test_utils/src/test_common.cpp index 6ac9052bd5c..95fc3f1772f 100644 --- a/src/tests/test_utils/common_test_utils/src/test_common.cpp +++ b/src/tests/test_utils/common_test_utils/src/test_common.cpp @@ -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(); } diff --git a/tests/memory_tests/src/memory_tests_helper/memory_counter.cpp b/tests/memory_tests/src/memory_tests_helper/memory_counter.cpp index 3a934d7c7c7..b0e47f00488 100644 --- a/tests/memory_tests/src/memory_tests_helper/memory_counter.cpp +++ b/tests/memory_tests/src/memory_tests_helper/memory_counter.cpp @@ -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; } diff --git a/tests/stress_tests/common/utils.cpp b/tests/stress_tests/common/utils.cpp index c7bc403576c..2dbbbb05895 100644 --- a/tests/stress_tests/common/utils.cpp +++ b/tests/stress_tests/common/utils.cpp @@ -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; }