On 2024/08/25 20:55:09 -0700, Collin Funk <collin.fu...@gmail.com> wrote:
> "Theo de Raadt" <dera...@openbsd.org> writes:
> 
> > Noone uses telnet, we (mostly) killed it!
> >
> > https://www.openbsd.org/images/tshirt-9b.jpg
> >
> > There is no way in heck this code is going to be converted in OpenBSD
> > to use strtol(), which is even more willing to eat junk.
> 
> I don't disagree. Especially on the strtol part.
> 
> > In our world, someone should adapt this to strtonum(), which is
> > a cynical string to integer API with range-control built in.
> 
> Interesting, good to know. Anyways my patch is here to draw inspiration
> from if anyone decides they care about telnet. :)

I don't usually use telnet, but seemed simple to fix the hand rolled
parser to use strtonum

OKs/opinions on the verbiage?

telnet> send dont 999
'999': too large ('send dont ?' for help).
telnet> send dont -33
'-33': too small ('send dont ?' for help).
telnet> send dont 12abc
'12abc': invalid ('send dont ?' for help).

diff /usr/src
commit - cfb8aef91a90e62370ec0c79ff80c4089a8f0cfb
path + /usr/src
blob - dcf3a77a973c60dda457cfa7da59fd1ac5a58256
file + usr.bin/telnet/commands.c
--- usr.bin/telnet/commands.c
+++ usr.bin/telnet/commands.c
@@ -358,6 +358,7 @@ send_tncmd(void (*func)(int, int), char *cmd, char *na
 {
     char **cpp;
     extern char *telopts[];
+    const char *errstr;
     int val = 0;
 
     if (isprefix(name, "help") || isprefix(name, "?")) {
@@ -389,21 +390,11 @@ send_tncmd(void (*func)(int, int), char *cmd, char *na
     if (cpp) {
        val = cpp - telopts;
     } else {
-       char *cp = name;
-
-       while (*cp >= '0' && *cp <= '9') {
-           val *= 10;
-           val += *cp - '0';
-           cp++;
-       }
-       if (*cp != 0) {
-           fprintf(stderr, "'%s': unknown argument ('send %s ?' for 
help).\r\n",
-                                       name, cmd);
+       val = strtonum(name, 0, 255, &errstr);
+       if (errstr) {
+           fprintf(stderr, "'%s': %s ('send %s ?' for help).\r\n",
+                                       name, errstr, cmd);
            return 0;
-       } else if (val < 0 || val > 255) {
-           fprintf(stderr, "'%s': bad value ('send %s ?' for help).\r\n",
-                                       name, cmd);
-           return 0;
        }
     }
     if (!connected) {

Reply via email to