[PATCH] Check if the list selection size is zero before calculating average time

Lubomir I. Ivanov neolit123 at gmail.com
Thu Jun 28 16:10:44 PDT 2012


From: "Lubomir I. Ivanov" <neolit123 at gmail.com>

statistics.c, show_total_dive_stats():

When the list is empty and we add a dive for the first time, the list
selection size is zero (stats_ptr->selection_size) and there is
possibility for a division by zero exception, when setting the average
time label (stats_w.avg_time).

Signed-off-by: Lubomir I. Ivanov <neolit123 at gmail.com>
---
 statistics.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)

diff --git a/statistics.c b/statistics.c
index 34f487b..9711f21 100644
--- a/statistics.c
+++ b/statistics.c
@@ -287,7 +287,10 @@ static void show_total_dive_stats(struct dive *dive)
 		set_label(stats_w.max_temp, "%.1f %s", value, unit);
 	}
 	set_label(stats_w.total_time, get_time_string(stats_ptr->total_time.seconds, 0));
-	set_label(stats_w.avg_time, get_time_string(stats_ptr->total_time.seconds / stats_ptr->selection_size, 0));
+	if (stats_ptr->selection_size)
+		set_label(stats_w.avg_time, get_time_string(stats_ptr->total_time.seconds / stats_ptr->selection_size, 0));
+	else
+		set_label(stats_w.avg_time, get_time_string(stats_ptr->total_time.seconds, 0));
 	set_label(stats_w.longest_time, get_time_string(stats_ptr->longest_time.seconds, 0));
 	set_label(stats_w.shortest_time, get_time_string(stats_ptr->shortest_time.seconds, 0));
 	value = get_depth_units(stats_ptr->max_depth.mm, &decimals, &unit);
-- 
1.7.6.msysgit.0



More information about the subsurface mailing list