From 4cbce0e34cd3b197b85aea6c2286fa3838f6eab1 Mon Sep 17 00:00:00 2001 From: brapru Date: Tue, 15 Feb 2022 19:48:05 -0500 Subject: [PATCH] ping: Fix broken count argument error By storing count as an Optional, we can leverage count's empty state to proceed with pinging indefinitely, and ensure a proper value is passed when count does have a value. This returns pings expected behavior to send infinite packets when a count is not specified, stop after sending a specified count, and disallow any count < 1. Closes #12524 --- Userland/Utilities/ping.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Userland/Utilities/ping.cpp b/Userland/Utilities/ping.cpp index 4f9ecea2d6..6182d36abc 100644 --- a/Userland/Utilities/ping.cpp +++ b/Userland/Utilities/ping.cpp @@ -26,7 +26,7 @@ static uint32_t total_pings; static int successful_pings; -static uint32_t count; +static Optional count; static uint32_t total_ms; static int min_ms; static int max_ms; @@ -67,8 +67,8 @@ ErrorOr serenity_main(Main::Arguments arguments) args_parser.add_option(payload_size, "Amount of bytes to send as payload in the ECHO_REQUEST packets.", "size", 's', "size"); args_parser.parse(arguments); - if (count < 1 || count > UINT32_MAX) { - warnln("invalid count argument: '{}': out of range: 1 <= value <= {}", count, UINT32_MAX); + if (count.has_value() && (count.value() < 1 || count.value() > UINT32_MAX)) { + warnln("invalid count argument: '{}': out of range: 1 <= value <= {}", count.value(), UINT32_MAX); return 1; } @@ -157,7 +157,7 @@ ErrorOr serenity_main(Main::Arguments arguments) struct timeval tv_send; gettimeofday(&tv_send, nullptr); - if (count && total_pings == count) + if (count.has_value() && total_pings == count.value()) closing_statistics(); else total_pings++;