From 568a37df875ed60bc638d6a0f5017a936c80d483 Mon Sep 17 00:00:00 2001 From: bad code <> Date: Sun, 22 Sep 2024 15:20:15 +0200 Subject: [PATCH 1/3] Avoid floating-point division by 0 Reported by UBSAN: src/linux/btop_collect.cpp:1933:57: runtime error: division by zero SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior src/linux/btop_collect.cpp:1933:57 src/linux/btop_collect.cpp:2038:93: runtime error: division by zero SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior src/linux/btop_collect.cpp:2038:93 --- src/linux/btop_collect.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index 4b3ab954..8a42118a 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -2184,7 +2184,7 @@ namespace Mem { for (int i = 0; i < 2; i++) { diskread >> std::ws; diskread.ignore(SSmax, ' '); } diskread >> io_ticks; - if (disk.io_activity.empty()) + if (uptime == old_uptime || disk.io_activity.empty()) disk.io_activity.push_back(0); else disk.io_activity.push_back(clamp((long)round((double)(io_ticks - disk.old_io.at(2)) / (uptime - old_uptime) / 10), 0l, 100l)); From cfad6365dabb2e2734b06b1e4d185ea46ce2787e Mon Sep 17 00:00:00 2001 From: bad code <> Date: Sun, 22 Sep 2024 15:20:20 +0200 Subject: [PATCH 2/3] Avoid stack-use-after-return The variable free_priv is a constant bool; pass by value. Reported by Address Sanitizer: SUMMARY: AddressSanitizer: stack-use-after-return src/linux/btop_collect.cpp:1931:17 in Mem::collect(bool)::$_0::operator()() const --- src/linux/btop_collect.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index 8a42118a..1de0ef1e 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -2068,7 +2068,7 @@ namespace Mem { disk.used_percent = updated_stats.used_percent; disk.free_percent = updated_stats.free_percent; } - disks_stats_promises[mountpoint] = async(std::launch::async, [mountpoint, &free_priv]() -> pair { + disks_stats_promises[mountpoint] = async(std::launch::async, [mountpoint, free_priv]() -> pair { struct statvfs vfs; disk_info disk; if (statvfs(mountpoint.c_str(), &vfs) < 0) { From d4f5919048941223b064b92507cf644ae78ae002 Mon Sep 17 00:00:00 2001 From: bad code <> Date: Sun, 22 Sep 2024 15:20:45 +0200 Subject: [PATCH 3/3] Avoid potential uninitialzed variables Reported-by clang: src/linux/btop_collect.cpp:489:10: error: variable 'high' may be uninitialized when used here [-Werror,-Wconditional-uninitialized] 489 | if (high < 1) high = 80; | ^~~~ src/linux/btop_collect.cpp:482:18: note: initialize the variable 'high' to silence this warning 482 | int64_t high, crit; | ^ | = 0 src/linux/btop_collect.cpp:490:10: error: variable 'crit' may be uninitialized when used here [-Werror,-Wconditional-uninitialized] 490 | if (crit < 1) crit = 95; | ^~~~ src/linux/btop_collect.cpp:482:24: note: initialize the variable 'crit' to silence this warning 482 | int64_t high, crit; | ^ | = 0 src/linux/btop_collect.cpp:1648:29: error: variable 'totalMem' may be uninitialized when used here [-Werror,-Wconditional-uninitialized] 1648 | if (not meminfo.good() or totalMem == 0) | ^~~~~~~~ src/linux/btop_collect.cpp:1642:19: note: initialize the variable 'totalMem' to silence this warning 1642 | int64_t totalMem; | ^ | = 0 --- src/linux/btop_collect.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index 1de0ef1e..83f37de1 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -505,7 +505,7 @@ namespace Cpu { const string sensor_name = "thermal" + to_string(i) + "/" + label; const int64_t temp = stol(readfile(basepath / "temp", "0")) / 1000; - int64_t high, crit; + int64_t high = 0, crit = 0; for (int ii = 0; fs::exists(basepath / string("trip_point_" + to_string(ii) + "_temp")); ii++) { const string trip_type = readfile(basepath / string("trip_point_" + to_string(ii) + "_type")); if (not is_in(trip_type, "high", "critical")) continue; @@ -1786,7 +1786,7 @@ namespace Mem { uint64_t get_totalMem() { ifstream meminfo(Shared::procPath / "meminfo"); - int64_t totalMem; + int64_t totalMem = 0; if (meminfo.good()) { meminfo.ignore(SSmax, ':'); meminfo >> totalMem;