From 07b3c7d8e3b6513044d8f5cc45359cc42c14eb18 Mon Sep 17 00:00:00 2001 From: "Min Hu (Connor)" Date: Thu, 13 May 2021 09:10:37 +0800 Subject: [PATCH] test: fix division by zero Variable i is used as a denominator which may be zero, and this may result in segmentation fault. This patch fixed it. Fixes: 948bc3d6d095 ("test: add reciprocal based division") Cc: stable@dpdk.org Signed-off-by: Min Hu (Connor) Acked-by: Bruce Richardson --- app/test/test_reciprocal_division_perf.c | 41 ++++++++++++++---------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/app/test/test_reciprocal_division_perf.c b/app/test/test_reciprocal_division_perf.c index a7be8aa71a..4f625873e5 100644 --- a/app/test/test_reciprocal_division_perf.c +++ b/app/test/test_reciprocal_division_perf.c @@ -71,10 +71,12 @@ test_reciprocal_division_perf(void) tot_cyc_n); printf("Total number of cycles reciprocal division : %"PRIu64"\n", tot_cyc_r); - printf("Cycles per division(normal) : %3.2f\n", - ((double)tot_cyc_n)/i); - printf("Cycles per division(reciprocal) : %3.2f\n\n", - ((double)tot_cyc_r)/i); + if (i != 0) { + printf("Cycles per division(normal) : %3.2f\n", + ((double)tot_cyc_n)/i); + printf("Cycles per division(reciprocal) : %3.2f\n\n", + ((double)tot_cyc_r)/i); + } tot_cyc_n = 0; tot_cyc_r = 0; @@ -111,11 +113,12 @@ test_reciprocal_division_perf(void) tot_cyc_n); printf("Total number of cycles reciprocal division : %"PRIu64"\n", tot_cyc_r); - printf("Cycles per division(normal) : %3.2f\n", - ((double)tot_cyc_n)/i); - printf("Cycles per division(reciprocal) : %3.2f\n\n", - ((double)tot_cyc_r)/i); - + if (i != 0) { + printf("Cycles per division(normal) : %3.2f\n", + ((double)tot_cyc_n)/i); + printf("Cycles per division(reciprocal) : %3.2f\n\n", + ((double)tot_cyc_r)/i); + } tot_cyc_n = 0; tot_cyc_r = 0; @@ -152,10 +155,12 @@ test_reciprocal_division_perf(void) tot_cyc_n); printf("Total number of cycles reciprocal division : %"PRIu64"\n", tot_cyc_r); - printf("Cycles per division(normal) : %3.2f\n", - ((double)tot_cyc_n)/i); - printf("Cycles per division(reciprocal) : %3.2f\n\n", - ((double)tot_cyc_r)/i); + if (i != 0) { + printf("Cycles per division(normal) : %3.2f\n", + ((double)tot_cyc_n)/i); + printf("Cycles per division(reciprocal) : %3.2f\n\n", + ((double)tot_cyc_r)/i); + } tot_cyc_n = 0; tot_cyc_r = 0; @@ -190,10 +195,12 @@ test_reciprocal_division_perf(void) tot_cyc_n); printf("Total number of cycles reciprocal division : %"PRIu64"\n", tot_cyc_r); - printf("Cycles per division(normal) : %3.2f\n", - ((double)tot_cyc_n)/i); - printf("Cycles per division(reciprocal) : %3.2f\n", - ((double)tot_cyc_r)/i); + if (i != 0) { + printf("Cycles per division(normal) : %3.2f\n", + ((double)tot_cyc_n)/i); + printf("Cycles per division(reciprocal) : %3.2f\n", + ((double)tot_cyc_r)/i); + } return result; } -- 2.20.1