[dpdk-dev] [PATCH] app/testpmd: fix division by zero bug

Ferruh Yigit ferruh.yigit at intel.com
Mon Apr 26 13:20:13 CEST 2021


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 at dpdk.org
> 
> Signed-off-by: Min Hu (Connor) <humin29 at 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,


More information about the dev mailing list