[batctl] Prevent division by zero when no packets were send

Message ID 1257606911-24933-1-git-send-email-sven.eckelmann@gmx.de (mailing list archive)
State Accepted, archived
Headers

Commit Message

Sven Eckelmann Nov. 7, 2009, 3:15 p.m. UTC
  We will try to print a statistic even when we receive a sigterm or
sigint before we send any data or started to count the send data. This
means that the we try to calculate the data loss by dividing through
zero as it is the number of send packets. This is not possible and we
must check for that corner case.

Signed-off-by: Sven Eckelmann <sven.eckelmann@gmx.de>
---
 batctl/ping.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)
  

Patch

diff --git a/batctl/ping.c b/batctl/ping.c
index 684a84d..ca1fbf0 100644
--- a/batctl/ping.c
+++ b/batctl/ping.c
@@ -72,7 +72,7 @@  int ping(int argc, char **argv)
 	fd_set read_socket;
 	int ret = EXIT_FAILURE, ping_fd = 0, res, optchar, found_args = 1;
 	int loop_count = -1, loop_interval = 1, timeout = 1;
-	unsigned int seq_counter = 0, packets_out = 0, packets_in = 0;
+	unsigned int seq_counter = 0, packets_out = 0, packets_in = 0, packets_loss;
 	char *dst_string, *mac_string;
 	double time_delta;
 	float min = 0.0, max = 0.0, avg = 0.0;
@@ -237,9 +237,14 @@  sleep:
 
 	}
 
+	if (packets_out == 0)
+		packets_loss = 0;
+	else
+		packets_loss = ((packets_out - packets_in) * 100) / packets_out;
+
 	printf("--- %s ping statistics ---\n", dst_string);
 	printf("%d packets transmitted, %d received, %d%% packet loss\n",
-		packets_out, packets_in, (((packets_out - packets_in) * 100) / packets_out));
+		packets_out, packets_in, packets_loss);
 	printf("rtt min/avg/max/mdev = %.3f/%.3f/%.3f/%.3f ms\n",
 		min, (packets_in ? (avg / packets_in) : 0.000), max, (max - min));