From 5c058ac6dd9a34ab997fd387d8914eb5d438ff64 Mon Sep 17 00:00:00 2001 From: Tom Lally Date: Fri, 2 Sep 2022 15:32:15 +0100 Subject: [PATCH] Add check to std::ifstream. --- src/util/SystemInfo/SystemInfoLinux.cpp | 40 +++++++++++++++++-------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/util/SystemInfo/SystemInfoLinux.cpp b/src/util/SystemInfo/SystemInfoLinux.cpp index 9e0d4fb0..a6bbab5c 100644 --- a/src/util/SystemInfo/SystemInfoLinux.cpp +++ b/src/util/SystemInfo/SystemInfoLinux.cpp @@ -12,11 +12,18 @@ uint64 QueryRamUsage() long page_size = sysconf(_SC_PAGESIZE); std::ifstream file("/proc/self/statm"); - file.ignore(std::numeric_limits::max(), ' '); - uint64 no_pages; - file >> no_pages; + if (file) + { + file.ignore(std::numeric_limits::max(), ' '); + uint64 no_pages; + file >> no_pages; - return no_pages * page_size; + return no_pages * page_size; + } + else + { + return 0; + } } void QueryProcTime(uint64 &out_now, uint64 &out_user, uint64 &out_kernel) @@ -33,18 +40,25 @@ void QueryProcTime(uint64 &out_now, uint64 &out_user, uint64 &out_kernel) void QueryCoreTimes(uint32 count, ProcessorTime out[]) { std::ifstream file("/proc/stat"); - file.ignore(std::numeric_limits::max(), '\n'); - - for (auto i = 0; i < count; ++i) + if (file) { - uint64 user, nice, kernel, idle; - file.ignore(std::numeric_limits::max(), ' '); - file >> user >> nice >> kernel >> idle; file.ignore(std::numeric_limits::max(), '\n'); - out[i].idle = idle; - out[i].kernel = kernel; - out[i].user = user + nice; + for (auto i = 0; i < count; ++i) + { + uint64 user, nice, kernel, idle; + file.ignore(std::numeric_limits::max(), ' '); + file >> user >> nice >> kernel >> idle; + file.ignore(std::numeric_limits::max(), '\n'); + + out[i].idle = idle; + out[i].kernel = kernel; + out[i].user = user + nice; + } + } + else + { + for (auto i = 0; i < count; ++i) out[i] = { } } }