From 5904b98a15da7cc70986f3b350e1ee58fc603358 Mon Sep 17 00:00:00 2001 From: yvs Date: Fri, 8 May 2026 17:24:47 +0400 Subject: [PATCH] curses: keep bit pattern range consistent Port the interactive bitpattern validation from yvs2014/mtr085 so the curses b command matches the command-line --bitpattern range. Only -1 selects random mode; other negative values and values above 255 are rejected. Ported-from: yvs2014/mtr085@e100fd72be37f0a98d6893db7e0baa8a5b81b72a Original-author: yvs --- ui/curses.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/ui/curses.c b/ui/curses.c index 5c776e9..51f77b6 100644 --- a/ui/curses.c +++ b/ui/curses.c @@ -25,6 +25,7 @@ #include #endif #include +#include #include #include @@ -223,7 +224,7 @@ int mtr_curses_keyaction( return ActionNone; case 'b': mvprintw(2, 0, "Ping Bit Pattern: %d\n", ctl->bitpattern); - mvprintw(3, 0, "Pattern Range: 0(0x00)-255(0xff), <0 random.\n"); + mvprintw(3, 0, "Pattern Range: 0(0x00)-255(0xff), -1 random.\n"); move(2, 18); refresh(); while ((c = getch()) != '\n' && i < MAXFLD) { @@ -234,9 +235,15 @@ int mtr_curses_keyaction( buf[i++] = c; /* need more checking on 'c' */ } buf[i] = '\0'; - ctl->bitpattern = atoi(buf); - if (ctl->bitpattern > 255) - ctl->bitpattern = -1; + char *end = NULL; + errno = 0; + long new_bitpattern = strtol(buf, &end, 10); + if (errno != 0 || buf == end || *end != '\0' || + new_bitpattern < -1 || new_bitpattern > 255) { + printf("\a"); + return ActionNone; + } + ctl->bitpattern = new_bitpattern; return ActionNone; case 'i': mvprintw(2, 0, "Interval : %0.0f\n\n", ctl->WaitTime); @@ -381,7 +388,7 @@ int mtr_curses_keyaction( (" m set the max time-to-live, default n= # of hops\n"); printw(" s set the packet size to n or random(n<0)\n"); printw - (" b set ping bit pattern to c(0..255) or random(c<0)\n"); + (" b set ping bit pattern to c(0..255) or random(c=-1)\n"); printw(" Q set ping packet's TOS to t\n"); printw(" u switch between ICMP ECHO and UDP datagrams\n"); printw(" t switch between ICMP ECHO and TCP\n"); -- 2.47.3