strtod() happily accepts "inf" or values like 1e300 as ping interval.
Converting such a value to time_t for the nanosleep interval is undefined
behavior.

Clamp the interval to a well representable maximum before splitting it
into seconds and nanoseconds.

Fixes: 4ebe4fb7b08d ("batctl: ping: Add subsecond precision to ping interval")
Signed-off-by: Sven Eckelmann <[email protected]>
---
 ping.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/ping.c b/ping.c
index fcb8d5e..c875c38 100644
--- a/ping.c
+++ b/ping.c
@@ -106,12 +106,14 @@ static int ping(struct state *state, int argc, char 
**argv)
                case 'i':
                        errno = 0;
                        ping_interval = strtod(optarg, &endptr);
-                       if (errno || *endptr != '\0') {
+                       if (errno || *endptr != '\0' || endptr == optarg ||
+                           !isfinite(ping_interval) || ping_interval <= 0) {
                                fprintf(stderr, "Error - invalid ping interval 
'%s'\n", optarg);
                                goto out;
                        }
 
                        ping_interval = fmax(ping_interval, 0.001);
+                       ping_interval = fmin(ping_interval, 1000000000.0);
                        fractional_part = modf(ping_interval, &integral_part);
                        loop_interval.tv_sec = (time_t)integral_part;
                        loop_interval.tv_nsec = (long)(fractional_part * 
1000000000l);

-- 
2.47.3

Reply via email to