Skip to contentSkip to conversation

test: fix division by zero

PATCH Superseded Min Hu (Connor) 3 participants 6 messages Apr 23, 2021 – May 19, 2021 Broadcom iproc Arm architecture

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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")
---
 app/test/test_reciprocal_division_perf.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
app/test/test_reciprocal_division_perf.c +3 -2
index a7be8aa..2647308 100644
@@ -143,7 +143,7 @@ test_reciprocal_division_perf(void)
 					"result %"PRIu64"",
 					nresult_u64, rresult_u64);
 			result = 1;
-			break;
+			goto err;
 		}
 	}
 
@@ -182,7 +182,7 @@ test_reciprocal_division_perf(void)
 					dividend_u64, divisor_u64,
 					nresult_u64, rresult_u64);
 			result = 1;
-			break;
+			goto err;
 		}
 	}
 	printf("64bit Division results:\n");
@@ -195,6 +195,7 @@ test_reciprocal_division_perf(void)
 	printf("Cycles per division(reciprocal) : %3.2f\n",
 			((double)tot_cyc_r)/i);
 
+err:
 	return result;
 }
 
Signature · 1 line
2.7.4
#1 lore raw
Quoted 36 lines · Min Hu (Connor)

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")
---
 app/test/test_reciprocal_division_perf.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
app/test/test_reciprocal_division_perf.c +2 -2
index a7be8aa..2647308 100644
@@ -143,7 +143,7 @@ test_reciprocal_division_perf(void)
 					"result %"PRIu64"",
 					nresult_u64, rresult_u64);
 			result = 1;
-			break;
+			goto err;
 		}
 	}
 
@@ -182,7 +182,7 @@ test_reciprocal_division_perf(void)
 					dividend_u64, divisor_u64,
 					nresult_u64, rresult_u64);
 			result = 1;
-			break;
+			goto err;
 		}
 	}
 	printf("64bit Division results:\n");

app/test/test_reciprocal_division_perf.c test_reciprocal_division_perf(void) · line 201

Quoted 7 lines
app/test/test_reciprocal_division_perf.c +1 -0
@@ -195,6 +195,7 @@ test_reciprocal_division_perf(void)
 	printf("Cycles per division(reciprocal) : %3.2f\n",
 			((double)tot_cyc_r)/i);
 
+err:
 	return result;
 }
 

This looks correct as far as it goes, but I believe the same fix is needed at lines 66 and 106 too.

One other thing I note is that currently the test will move on to the next test case on failure, due to break, but using the goto will change that behaviour. Therefore, I wonder if a better fix is to skip the printouts if i == 0 in each case?

/Bruce

#2 lore raw
Quoted 47 lines · Bruce Richardson
Quoted 36 lines · Min Hu (Connor)

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")
---
  app/test/test_reciprocal_division_perf.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
app/test/test_reciprocal_division_perf.c +2 -2
index a7be8aa..2647308 100644
@@ -143,7 +143,7 @@ test_reciprocal_division_perf(void)
  					"result %"PRIu64"",
  					nresult_u64, rresult_u64);
  			result = 1;
-			break;
+			goto err;
  		}
  	}
 
@@ -182,7 +182,7 @@ test_reciprocal_division_perf(void)
  					dividend_u64, divisor_u64,
  					nresult_u64, rresult_u64);
  			result = 1;
-			break;
+			goto err;
  		}
  	}
  	printf("64bit Division results:\n");

app/test/test_reciprocal_division_perf.c test_reciprocal_division_perf(void) · line 201

Quoted 7 lines
app/test/test_reciprocal_division_perf.c +1 -0
@@ -195,6 +195,7 @@ test_reciprocal_division_perf(void)
  	printf("Cycles per division(reciprocal) : %3.2f\n",
  			((double)tot_cyc_r)/i);
 
+err:
  	return result;
  }
 

This looks correct as far as it goes, but I believe the same fix is needed at lines 66 and 106 too.

Quoted 5 lines

One other thing I note is that currently the test will move on to the next test case on failure, due to break, but using the goto will change that behaviour. Therefore, I wonder if a better fix is to skip the printouts if i == 0 in each case?

Good point, fixed in v2, thanks Bruce.

Quoted 3 lines

/Bruce
.

#3 lore raw

PATCH v2 1/1 test: fix division by zero

In reply to Min Hu (Connor)

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")

---
v2:
* to skip the printouts if i == 0.

---
 app/test/test_reciprocal_division_perf.c | 41 +++++++++++++++++++-------------
 1 file changed, 24 insertions(+), 17 deletions(-)
app/test/test_reciprocal_division_perf.c +24 -17
index a7be8aa..4f62587 100644
@@ -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;
 }
Signature · 1 line
2.7.4
Acked-by on v2 1/1
#4 lore raw
Quoted 8 lines · Min Hu (Connor)

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")
Quoted 8 lines

---
v2:
* to skip the printouts if i == 0.

---
 app/test/test_reciprocal_division_perf.c | 41 +++++++++++++++++++-------------
 1 file changed, 24 insertions(+), 17 deletions(-)
#5 lore raw

13/05/2021 10:23, Bruce Richardson:

Quoted 17 lines · Min Hu (Connor)
Quoted 8 lines

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")
Quoted 8 lines

---
v2:
* to skip the printouts if i == 0.

---
 app/test/test_reciprocal_division_perf.c | 41 +++++++++++++++++++-------------
 1 file changed, 24 insertions(+), 17 deletions(-)
Quoted 1 line

Applied, thanks.