app/testpmd: fix division by zero bug
PATCH 1/1 app/testpmd: fix division by zero bug
Variable total, which may be zero and result in segmentation fault.
This patch fixed it.
--- app/test-pmd/cmdline.c | 3 +++ 1 file changed, 3 insertions(+)
app/test-pmd/cmdline.c +3 -0
@@ -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),
Signature · 1 line
2.7.4
Quoted 18 lines · 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")Signed-off-by Min Hu (Connor) <[email protected]>--- app/test-pmd/cmdline.c | 3 +++ 1 file changed, 3 insertions(+)app/test-pmd/cmdline.c +0
-0
app/test-pmd/cmdline.c dump_socket_mem(FILE *f) · line 9639
Quoted 9 lines
app/test-pmd/cmdline.c +3
-0@@ -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,
PATCH v2 1/1 app/testpmd: fix division by zero bug
In reply to Min Hu (Connor)Variable total, which may be zero and result in segmentation fault.
This patch fixed it.
--- app/test-pmd/cmdline.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
app/test-pmd/cmdline.c +1 -1
@@ -9727,7 +9727,7 @@ dump_socket_mem(FILE *f)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 ), - (double )alloc *100 / (double )total, + total ? ((double )alloc *100 / (double )total) :0 , (double )free / (1024 *1024 ), n_alloc, n_free);if (last_allocs)
Signature · 1 line
2.7.4
Quoted 8 lines · 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")Signed-off-by Min Hu (Connor) <[email protected]>
Applied to dpdk-next-net/main, thanks.
Quoted 18 lines · 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")Signed-off-by Min Hu (Connor) <[email protected]>--- app/test-pmd/cmdline.c | 3 +++ 1 file changed, 3 insertions(+)app/test-pmd/cmdline.c +0
-0
app/test-pmd/cmdline.c dump_socket_mem(FILE *f) · line 9639
Quoted 9 lines
app/test-pmd/cmdline.c +3
-0@@ -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.