app/testpmd: fix division by zero bug

Message ID 1619005102-38437-1-git-send-email-humin29@huawei.com (mailing list archive)
State Changes Requested, archived
Delegated to: Ferruh Yigit
Headers
Series app/testpmd: fix division by zero bug |

Checks

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

Commit Message

humin (Q) April 21, 2021, 11:38 a.m. UTC
  Variable total, which may be zero and result in segmentation fault.

This patch fixed it.

Fixes: 9b1249d9ff69 ("app/testpmd: support dumping socket memory")
Cc: stable@dpdk.org

Signed-off-by: Min Hu (Connor) <humin29@huawei.com>
---
 app/test-pmd/cmdline.c | 3 +++
 1 file changed, 3 insertions(+)
  

Comments

Ferruh Yigit April 26, 2021, 11:20 a.m. UTC | #1
On 4/21/2021 12:38 PM, Min Hu (Connor) wrote:
> Variable total, which may be zero and result in segmentation fault.
> 
> This patch fixed it.
> 
> Fixes: 9b1249d9ff69 ("app/testpmd: support dumping socket memory")
> Cc: stable@dpdk.org
> 
> Signed-off-by: Min Hu (Connor) <humin29@huawei.com>
> ---
>   app/test-pmd/cmdline.c | 3 +++
>   1 file changed, 3 insertions(+)
> 
> diff --git a/app/test-pmd/cmdline.c b/app/test-pmd/cmdline.c
> index 08da2b1..cde0a00 100644
> --- a/app/test-pmd/cmdline.c
> +++ b/app/test-pmd/cmdline.c
> @@ -9631,6 +9631,9 @@ dump_socket_mem(FILE *f)
>   			socket_stats.alloc_count,
>   			socket_stats.free_count);
>   	}
> +
> +	if (total == 0)
> +		return;
>   	fprintf(f,
>   		"Total   : size(M) total: %.6lf alloc: %.6lf(%.3lf%%) free: %.6lf \tcount alloc: %-4u free: %u\n",
>   		(double)total / (1024 * 1024), (double)alloc / (1024 * 1024),
> 

Hi Connor,

Not sure if the value can be zero on practice, but if the issue is found via 
static analyzer tool, instead of return from function without any output what 
about following instead:

  -               (double)alloc * 100 / (double)total,
  +               total ? ((double)alloc * 100 / (double)total) : 0,
  
humin (Q) April 26, 2021, 11:58 a.m. UTC | #2
在 2021/4/21 19:38, Min Hu (Connor) 写道:
> Variable total, which may be zero and result in segmentation fault.
> 
> This patch fixed it.
> 
> Fixes: 9b1249d9ff69 ("app/testpmd: support dumping socket memory")
> Cc: stable@dpdk.org
> 
> Signed-off-by: Min Hu (Connor) <humin29@huawei.com>
> ---
>   app/test-pmd/cmdline.c | 3 +++
>   1 file changed, 3 insertions(+)
> 
> diff --git a/app/test-pmd/cmdline.c b/app/test-pmd/cmdline.c
> index 08da2b1..cde0a00 100644
> --- a/app/test-pmd/cmdline.c
> +++ b/app/test-pmd/cmdline.c
> @@ -9631,6 +9631,9 @@ dump_socket_mem(FILE *f)
>   			socket_stats.alloc_count,
>   			socket_stats.free_count);
>   	}
> +
> +	if (total == 0)
> +		return;
>   	fprintf(f,
>   		"Total   : size(M) total: %.6lf alloc: %.6lf(%.3lf%%) free: %.6lf \tcount alloc: %-4u free: %u\n",
>   		(double)total / (1024 * 1024), (double)alloc / (1024 * 1024),
> 
Agreed, fixed in v2, thanks.
  

Patch

diff --git a/app/test-pmd/cmdline.c b/app/test-pmd/cmdline.c
index 08da2b1..cde0a00 100644
--- a/app/test-pmd/cmdline.c
+++ b/app/test-pmd/cmdline.c
@@ -9631,6 +9631,9 @@  dump_socket_mem(FILE *f)
 			socket_stats.alloc_count,
 			socket_stats.free_count);
 	}
+
+	if (total == 0)
+		return;
 	fprintf(f,
 		"Total   : size(M) total: %.6lf alloc: %.6lf(%.3lf%%) free: %.6lf \tcount alloc: %-4u free: %u\n",
 		(double)total / (1024 * 1024), (double)alloc / (1024 * 1024),