From: Marek Küthe Date: Sat, 15 Feb 2025 22:21:13 +0000 (+0000) Subject: Fix https://github.com/traviscross/mtr/issues/475 X-Git-Tag: v0.96~6^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F527%2Fhead;p=thirdparty%2Fmtr.git 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. --- 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; }