From 0c1d0397dda31c0f28fc778194093f1e2beafe58 Mon Sep 17 00:00:00 2001 From: Packit Service Date: Dec 12 2020 00:23:48 +0000 Subject: Apply patch ping-add-support-for-sub-second-timeouts.patch patch_name: ping-add-support-for-sub-second-timeouts.patch present_in_specfile: true --- diff --git a/ping.c b/ping.c index d9a3f5d..fec2453 100644 --- a/ping.c +++ b/ping.c @@ -57,6 +57,7 @@ #ifndef WITHOUT_IFADDRS #include #endif +#include #ifndef ICMP_FILTER #define ICMP_FILTER 1 @@ -192,6 +193,40 @@ static void set_socket_option(socket_st *sock, int level, int optname, const voi } } +/* Much like stdtod(3, but will fails if str is not valid number. */ +static double ping_strtod(const char *str, const char *err_msg) +{ + double num; + char *end = NULL; + + if (str == NULL || *str == '\0') + goto err; + errno = 0; +#ifdef USE_IDN + setlocale(LC_ALL, "C"); +#endif + num = strtod(str, &end); +#ifdef USE_IDN + setlocale(LC_ALL, ""); +#endif + if (errno || str == end || (end && *end)) + goto err; + switch (fpclassify(num)) { + case FP_NORMAL: + case FP_ZERO: + break; + default: + errno = ERANGE; + goto err; + } + return num; +err: + if (errno == ERANGE) + error(2, errno, "%s: %s", err_msg, str); + error(2, 0, "%s: %s", err_msg, str); + return num; +} + int main(int argc, char **argv) { @@ -298,30 +333,19 @@ main(int argc, char **argv) options |= F_PTIMEOFDAY; break; case 'i': - { - double dbl; - char *ep; - - errno = 0; -#ifdef USE_IDN - setlocale(LC_ALL, "C"); -#endif - dbl = strtod(optarg, &ep); -#ifdef USE_IDN - setlocale(LC_ALL, ""); -#endif - - if (errno || *ep != '\0' || - !finite(dbl) || dbl < 0.0 || dbl >= (double)INT_MAX / 1000 - 1.0) { - fprintf(stderr, "ping: bad timing interval\n"); - exit(2); - } - - interval = (int)(dbl * 1000); - - options |= F_INTERVAL; - break; - } + { + double optval; + + optval = ping_strtod(optarg, "bad timing interval"); + if (isgreater(optval, (double)INT_MAX / 1000)) { + fprintf(stderr, "ping: bad timing interval\n"); + exit(2); + } + + interval = (int)(optval * 1000); + options |= F_INTERVAL; + } + break; case 'I': /* IPv6 */ if (strchr(optarg, ':')) { @@ -460,13 +484,18 @@ main(int argc, char **argv) } break; case 'W': - lingertime = atoi(optarg); - if (lingertime < 0 || lingertime > INT_MAX/1000000) { - fprintf(stderr, "ping: bad linger time.\n"); - exit(2); - } - lingertime *= 1000; - break; + { + double optval; + + optval = ping_strtod(optarg, "bad linger time"); + if (isless(optval, 0.001) || isgreater(optval, (double)INT_MAX / 1000)) { + fprintf(stderr, "ping: bad linger time.\n"); + exit(2); + } + /* lingertime will be converted to usec later */ + lingertime = (int)(optval * 1000); + } + break; default: usage(); break;