]> git.ipfire.org Git - thirdparty/mtr.git/commitdiff
Fix https://github.com/traviscross/mtr/issues/475 527/head
authorMarek Küthe <m.k@mk16.de>
Sat, 15 Feb 2025 22:21:13 +0000 (22:21 +0000)
committerMarek Küthe <m.k@mk16.de>
Sat, 15 Feb 2025 22:21:13 +0000 (22:21 +0000)
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
ui/mtr.c
ui/net.c

index 1c41c4670549e621ddf5052dc04c3ad1f09f6db7..7fc201d77f7815bb5d55861a2823355c2c3466f0 100644 (file)
@@ -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);
index 447eced6c91a9287f37840de928130cd4f5d9cb7..81417712af6727f0a1f3f18bd7f2ee144dea7c27 100644 (file)
--- 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;
index 45c33ec858d11b5f0b5726b8ce67bd0adc29bf64..4df7b74276cd53188ff37b7f24e25e3c13225b45 100644 (file)
--- 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;
         }