From ef3605604ec7305154f51bab4b2c42a4f2a1ecf6 Mon Sep 17 00:00:00 2001 From: brapru Date: Mon, 14 Feb 2022 06:09:28 -0500 Subject: [PATCH] ping: Fix off by one error in count argument Previously, the count and total_pings comparison was evaluated after a ping was sent for that iteration. This would cause one extra ping to be sent greater than the specific count passed. --- Userland/Utilities/ping.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Userland/Utilities/ping.cpp b/Userland/Utilities/ping.cpp index c8782b85dd..825608112d 100644 --- a/Userland/Utilities/ping.cpp +++ b/Userland/Utilities/ping.cpp @@ -152,17 +152,17 @@ ErrorOr serenity_main(Main::Arguments arguments) struct timeval tv_send; gettimeofday(&tv_send, nullptr); + if (count && total_pings == count) + closing_statistics(); + else + total_pings++; + rc = sendto(fd, ping_packet.data(), ping_packet.size(), 0, (const struct sockaddr*)&peer_address, sizeof(sockaddr_in)); if (rc < 0) { perror("sendto"); return 1; } - if (count && total_pings == count) - closing_statistics(); - else - total_pings++; - for (;;) { auto pong_packet_result = ByteBuffer::create_uninitialized( sizeof(struct ip) + max_optional_header_size_in_bytes + sizeof(struct icmphdr) + payload_size);