test: fix division by zero

Message ID 1619163330-25960-1-git-send-email-humin29@huawei.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series test: fix division by zero |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/iol-intel-Performance success Performance Testing PASS
ci/Intel-compilation success Compilation OK
ci/github-robot success github build: passed
ci/iol-testing success Testing PASS
ci/iol-abi-testing success Testing PASS
ci/intel-Testing success Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS

Commit Message

humin (Q) April 23, 2021, 7:35 a.m. UTC
  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>
---
 app/test/test_reciprocal_division_perf.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
  

Comments

Bruce Richardson May 12, 2021, 4:53 p.m. UTC | #1
On Fri, Apr 23, 2021 at 03:35:30PM +0800, Min Hu (Connor) wrote:
> 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>
> ---
>  app/test/test_reciprocal_division_perf.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/app/test/test_reciprocal_division_perf.c b/app/test/test_reciprocal_division_perf.c
> index a7be8aa..2647308 100644
> --- a/app/test/test_reciprocal_division_perf.c
> +++ b/app/test/test_reciprocal_division_perf.c
> @@ -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;
>  }
>  
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
  
humin (Q) May 13, 2021, 1:11 a.m. UTC | #2
在 2021/5/13 0:53, Bruce Richardson 写道:
> On Fri, Apr 23, 2021 at 03:35:30PM +0800, Min Hu (Connor) wrote:
>> 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>
>> ---
>>   app/test/test_reciprocal_division_perf.c | 5 +++--
>>   1 file changed, 3 insertions(+), 2 deletions(-)
>>
>> diff --git a/app/test/test_reciprocal_division_perf.c b/app/test/test_reciprocal_division_perf.c
>> index a7be8aa..2647308 100644
>> --- a/app/test/test_reciprocal_division_perf.c
>> +++ b/app/test/test_reciprocal_division_perf.c
>> @@ -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;
>>   }
>>   
> 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?
> 
Good point, fixed in v2, thanks Bruce.
> /Bruce
> .
>
  

Patch

diff --git a/app/test/test_reciprocal_division_perf.c b/app/test/test_reciprocal_division_perf.c
index a7be8aa..2647308 100644
--- a/app/test/test_reciprocal_division_perf.c
+++ b/app/test/test_reciprocal_division_perf.c
@@ -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;
 }