test: fix division by zero
authorMin Hu (Connor) <humin29@huawei.com>
Thu, 13 May 2021 01:10:37 +0000 (09:10 +0800)
committerThomas Monjalon <thomas@monjalon.net>
Wed, 19 May 2021 07:23:00 +0000 (09:23 +0200)
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) <humin29@huawei.com>
Acked-by: Bruce Richardson <bruce.richardson@intel.com>
app/test/test_reciprocal_division_perf.c

index a7be8aa..4f62587 100644 (file)
@@ -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;
 }