hal/hald util.c,1.32,1.33
Danny Kukawka
dkukawka at freedesktop.org
Mon Nov 14 13:39:50 PST 2005
Update of /cvs/hal/hal/hald
In directory gabe:/tmp/cvs-serv13773/hald
Modified Files:
util.c
Log Message:
2005-11-14 Danny Kukawka <danny.kukawka at web.de>
* hald/util.c: (util_compute_time_remaining): fixed multiple 'devision
by zero' bugs which could happen, if calculate the 'remaining time'
based on time and nothing is changed on battery.
Index: util.c
===================================================================
RCS file: /cvs/hal/hal/hald/util.c,v
retrieving revision 1.32
retrieving revision 1.33
diff -u -d -r1.32 -r1.33
--- util.c 10 Nov 2005 19:28:34 -0000 1.32
+++ util.c 14 Nov 2005 21:39:48 -0000 1.33
@@ -145,6 +145,10 @@
saved_battery_info = g_hash_table_new(g_str_hash, g_str_equal);
if ((battery_info = g_hash_table_lookup(saved_battery_info, id))) {
+ /* check this to prevent division by zero */
+ if ((cur_time == battery_info->last_time) || (chargeLevel == battery_info->last_level))
+ return -1;
+
chargeRate = ((chargeLevel - battery_info->last_level) * 60 * 60) / (cur_time - battery_info->last_time);
/*
* During discharging chargeRate would be negative, which would
@@ -163,9 +167,13 @@
battery_info->last_time = cur_time;
return -1;
}
- }
+ }
- if (isDischarging)
+ /* paranoic, but better than crash: check this to prevent division by zero */
+ if (chargeRate == 0)
+ return -1;
+
+ if (isDischarging)
remaining_time = ((double) chargeLevel / (double) chargeRate) * 60 * 60;
else if (isCharging) {
/*
More information about the hal-commit
mailing list