[PATCH 09/10] lib: fwts_framework: avoid division by zero in % calculations

Colin King colin.king at canonical.com
Thu May 1 14:01:20 UTC 2014


From: Colin Ian King <colin.king at canonical.com>

Coverity Scan is warning about potential division by zero, so guard
against that, even it if it impossible unless fwts is configured with
no tests.

Signed-off-by: Colin Ian King <colin.king at canonical.com>
---
 src/lib/src/fwts_framework.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/lib/src/fwts_framework.c b/src/lib/src/fwts_framework.c
index 669a944..7d8646c 100644
--- a/src/lib/src/fwts_framework.c
+++ b/src/lib/src/fwts_framework.c
@@ -365,11 +365,12 @@ void fwts_framework_minor_test_progress(fwts_framework *fw, const int percent, c
 	if (width > 256)
 		width = 256;
 
-	if (percent >=0 && percent <=100)
+	if (percent >= 0 && percent <= 100)
 		fw->minor_test_progress = percent;
 
-	major_percent = (float)100.0 / (float)fw->major_tests_total;
-	minor_percent = ((float)major_percent / (float)fw->current_major_test->ops->total_tests);
+	major_percent = fw->major_tests_total ? (float)100.0 / (float)fw->major_tests_total : 100.0;
+	minor_percent = fw->current_major_test->ops->total_tests ?
+			((float)major_percent / (float)fw->current_major_test->ops->total_tests) : 0.0;
 	process_percent = ((float)minor_percent / 100.0);
 
 	progress = (float)(fw->current_major_test_num-1) * major_percent;
-- 
2.0.0.rc0




More information about the fwts-devel mailing list