From 50d606539cd13997cccad64ce11ac2dacdbc0ab6 Mon Sep 17 00:00:00 2001 From: Valentin Balaschenko <13349202+vlntb@users.noreply.github.com> Date: Thu, 13 Nov 2025 17:16:53 +0000 Subject: [PATCH] fixing test --- src/libxrpl/basics/MallocTrim.cpp | 45 ++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/src/libxrpl/basics/MallocTrim.cpp b/src/libxrpl/basics/MallocTrim.cpp index c7e547f123..a68831d1c9 100644 --- a/src/libxrpl/basics/MallocTrim.cpp +++ b/src/libxrpl/basics/MallocTrim.cpp @@ -24,18 +24,43 @@ namespace detail { long parseVmRSSkB(std::string const& status) { - // "VmRSS: 123456 kB" - auto pos = status.find("VmRSS:"); - if (pos == std::string::npos) + std::istringstream iss(status); + std::string line; + + while (std::getline(iss, line)) + { + // Allow leading spaces/tabs before the key. + auto const firstNonWs = line.find_first_not_of(" \t"); + if (firstNonWs == std::string::npos) + continue; + + constexpr char key[] = "VmRSS:"; + constexpr auto keyLen = sizeof(key) - 1; + + // Require the line (after leading whitespace) to start with "VmRSS:". + // Check if we have enough characters and the substring matches. + if (firstNonWs + keyLen > line.size() || + line.substr(firstNonWs, keyLen) != key) + continue; + + // Move past "VmRSS:" and any following whitespace. + auto pos = firstNonWs + keyLen; + while (pos < line.size() && + std::isspace(static_cast(line[pos]))) + { + ++pos; + } + + long value = -1; + if (std::sscanf(line.c_str() + pos, "%ld", &value) == 1) + return value; + + // Found the key but couldn't parse a number. return -1; + } - pos += 6; // past "VmRSS:" - while (pos < status.size() && status[pos] == ' ') - ++pos; - - long value = -1; - std::sscanf(status.c_str() + pos, "%ld", &value); - return value; // in kB + // No VmRSS line found. + return -1; } #endif // __GLIBC__ && BOOST_OS_LINUX