From 528df4d84f7908597b052bd64771a1306a581326 Mon Sep 17 00:00:00 2001 From: aristocratos Date: Sun, 26 Sep 2021 01:03:57 +0200 Subject: [PATCH] Added: ifstream check and try-catch for stod() in Tools::system_uptime() --- src/linux/btop_collect.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index 9e9bbcc..3c8b9fe 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -1611,8 +1611,15 @@ namespace Tools { double system_uptime() { string upstr; ifstream pread(Shared::procPath / "uptime"); - getline(pread, upstr, ' '); - pread.close(); - return stod(upstr); + if (pread.good()) { + try { + getline(pread, upstr, ' '); + pread.close(); + return stod(upstr); + } + catch (const std::invalid_argument&) {} + catch (const std::out_of_range&) {} + } + throw std::runtime_error("Failed get uptime from from " + (string)Shared::procPath + "/uptime"); } } \ No newline at end of file