Skip to contentSkip to conversation

app/testpmd: fix division by zero bug

PATCH Superseded Min Hu (Connor) 2 participants 5 messages Apr 21, 2021 – Apr 29, 2021

lore
Share & tools
lore
#0 lore raw

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.

Fixes 9b1249d9ff69 ("app/testpmd: support dumping socket memory")
---
 app/test-pmd/cmdline.c | 3 +++
 1 file changed, 3 insertions(+)
app/test-pmd/cmdline.c +3 -0
index 08da2b1..cde0a00 100644
@@ -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
#1 lore raw
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")
---
  app/test-pmd/cmdline.c | 3 +++
  1 file changed, 3 insertions(+)
app/test-pmd/cmdline.c +0 -0
index 08da2b1..cde0a00 100644

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,

#2 lore raw

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.

Fixes 9b1249d9ff69 ("app/testpmd: support dumping socket memory")
---
 app/test-pmd/cmdline.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
app/test-pmd/cmdline.c +1 -1
index 12efbc0..51d789e 100644
@@ -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
#4 lore raw
In reply to Min Hu (Connor)
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")
---
  app/test-pmd/cmdline.c | 3 +++
  1 file changed, 3 insertions(+)
app/test-pmd/cmdline.c +0 -0
index 08da2b1..cde0a00 100644

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.