From 0f96f02f46d8d72603535c9e1481f245c0ebe2e7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marek=20K=C3=BCthe?= Date: Sat, 15 Feb 2025 22:21:13 +0000 Subject: [PATCH] Fix https://github.com/traviscross/mtr/issues/475 With an input of `-28` the expression `(-ctl->cpacketsize - MINPACKET)` became 0, which triggered a zero division. This error is fixed by introducing a check to see if there is any room for randomness. Furthermore, a check of the input arguments in the command line and in curses is performed. --- ui/curses.c | 5 ++++- ui/mtr.c | 3 +++ ui/net.c | 9 +++++++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/ui/curses.c b/ui/curses.c index 1c41c46..7fc201d 100644 --- a/ui/curses.c +++ b/ui/curses.c @@ -202,7 +202,10 @@ int mtr_curses_keyaction( buf[i++] = c; /* need more checking on 'c' */ } buf[i] = '\0'; - ctl->cpacketsize = atoi(buf); + int new_packetsize = atoi(buf); + if (abs(ctl->cpacketsize) >= MINPACKET && abs(ctl->cpacketsize) < MAXPACKET) { + ctl->cpacketsize = new_packetsize; + } return ActionNone; case 'b': mvprintw(2, 0, "Ping Bit Pattern: %d\n", ctl->bitpattern); diff --git a/ui/mtr.c b/ui/mtr.c index 447eced..8141771 100644 --- a/ui/mtr.c +++ b/ui/mtr.c @@ -452,6 +452,9 @@ static void parse_arg( case 's': ctl->cpacketsize = strtoint_or_err(optarg, "invalid argument"); + if (abs(ctl->cpacketsize) < MINPACKET || abs(ctl->cpacketsize) > MAXPACKET) { + error(EXIT_FAILURE, 0, "value of of range (%d - %d)", MINPACKET, MAXPACKET); + } break; case 'I': ctl->InterfaceName = optarg; diff --git a/ui/net.c b/ui/net.c index 45c33ec..4df7b74 100644 --- a/ui/net.c +++ b/ui/net.c @@ -561,8 +561,13 @@ int net_send_batch( smaller (reasonable packet sizes), and our rand() range much larger, this effect is insignificant. Oh! That other formula didn't work. */ - packetsize = - MINPACKET + rand() % (-ctl->cpacketsize - MINPACKET); + if (-ctl->cpacketsize <= MINPACKET) { + /* There is no room to introduce randomness. */ + packetsize = MINPACKET; + } else { + packetsize = + MINPACKET + rand() % (-ctl->cpacketsize - MINPACKET); + } } else { packetsize = ctl->cpacketsize; } -- 2.47.2