From 78f1a3c0a67334bd66aabd6fe9337ebcb138e6d0 Mon Sep 17 00:00:00 2001 From: datalore Date: Fri, 26 Jul 2024 10:13:41 +0200 Subject: [PATCH] fix: Preventing division through 0 errors --- src/batteryinfo.cc | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/batteryinfo.cc b/src/batteryinfo.cc index be52410..d0f0ffc 100644 --- a/src/batteryinfo.cc +++ b/src/batteryinfo.cc @@ -7,7 +7,7 @@ namespace dlore std::string readData(std::filesystem::path path) { std::ifstream file(path, std::ios::in); - if(!file) return ""; + if(!file) return "0"; std::string ret; std::getline(file, ret); file.close(); @@ -26,8 +26,10 @@ namespace dlore info.ChargeFull = std::stoi(readData(classPath / "charge_full")) / 1000; info.ChargeFullDesign = std::stoi(readData(classPath / "charge_full_design")) / 1000; - info.ChargeFullPercentageDesign = 100 * info.ChargeFull / info.ChargeFullDesign; - info.CapacityDesign = 100 * info.ChargeNow / info.ChargeFullDesign; + if(info.ChargeFullDesign != 0) { + info.ChargeFullPercentageDesign = 100 * info.ChargeFull / info.ChargeFullDesign; + info.CapacityDesign = 100 * info.ChargeNow / info.ChargeFullDesign; + } int Vnow = std::stoi(readData(classPath / "voltage_now")) / 1000; int Vmin = std::stoi(readData(classPath / "voltage_min_design")) / 1000;