Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/telnet minor cleanup:



details:   https://anonhg.NetBSD.org/src/rev/9d072cb2aa16
branches:  trunk
changeset: 446698:9d072cb2aa16
user:      christos <christos%NetBSD.org@localhost>
date:      Fri Dec 14 23:40:17 2018 +0000

description:
minor cleanup:
- more const
- prevent overflow of the argument vector
- change a lot of unsigned to signed to eliminate casts

diffstat:

 usr.bin/telnet/authenc.c   |   29 ++-
 usr.bin/telnet/commands.c  |  280 +++++++++++++++++++++++---------------------
 usr.bin/telnet/externs.h   |   30 ++--
 usr.bin/telnet/main.c      |    6 +-
 usr.bin/telnet/network.c   |   14 +-
 usr.bin/telnet/ring.c      |    6 +-
 usr.bin/telnet/telnet.c    |    8 +-
 usr.bin/telnet/utilities.c |    8 +-
 8 files changed, 200 insertions(+), 181 deletions(-)

diffs (truncated from 1086 to 300 lines):

diff -r 9a037e81071b -r 9d072cb2aa16 usr.bin/telnet/authenc.c
--- a/usr.bin/telnet/authenc.c  Fri Dec 14 23:03:29 2018 +0000
+++ b/usr.bin/telnet/authenc.c  Fri Dec 14 23:40:17 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: authenc.c,v 1.13 2012/01/09 16:08:55 christos Exp $    */
+/*     $NetBSD: authenc.c,v 1.14 2018/12/14 23:40:17 christos Exp $    */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)authenc.c  8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: authenc.c,v 1.13 2012/01/09 16:08:55 christos Exp $");
+__RCSID("$NetBSD: authenc.c,v 1.14 2018/12/14 23:40:17 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -54,13 +54,15 @@
 int
 telnet_net_write(unsigned char *str, int len)
 {
-       if (NETROOM() > len) {
-               ring_supply_data(&netoring, str, len);
-               if (str[0] == IAC && str[1] == SE)
-                       printsub('>', &str[2], len-2);
-               return(len);
+       if (NETROOM() <= len)
+               return 0;
+       ring_supply_data(&netoring, str, len);
+       if (str[0] == IAC && str[1] == SE) {
+               if (len < 2)
+                       return 0;
+               printsub('>', &str[2], len - 2);
        }
-       return(0);
+       return len;
 }
 
 void
@@ -77,13 +79,13 @@
 int
 telnet_spin(void)
 {
-       return(-1);
+       return -1;
 }
 
 char *
 telnet_getenv(char *val)
 {
-       return((char *)env_getvalue((unsigned char *)val));
+       return env_getvalue(val);
 }
 
 char *
@@ -93,15 +95,18 @@
        int om = globalmode;
        char *res;
 
+       if (length < 0)
+               abort();
+
        TerminalNewMode(-1);
        if (echo) {
                printf("%s", prmpt);
                res = fgets(result, length, stdin);
        } else if ((res = getpass(prmpt)) != NULL) {
-               strlcpy(result, res, length);
+               strlcpy(result, res, (size_t)length);
                res = result;
        }
        TerminalNewMode(om);
-       return(res);
+       return res;
 }
 #endif /* defined(AUTHENTICATION) || defined(ENCRYPTION) */
diff -r 9a037e81071b -r 9d072cb2aa16 usr.bin/telnet/commands.c
--- a/usr.bin/telnet/commands.c Fri Dec 14 23:03:29 2018 +0000
+++ b/usr.bin/telnet/commands.c Fri Dec 14 23:40:17 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: commands.c,v 1.72 2018/12/14 06:08:18 maya Exp $       */
+/*     $NetBSD: commands.c,v 1.73 2018/12/14 23:40:17 christos Exp $   */
 
 /*
  * Copyright (C) 1997 and 1998 WIDE Project.
@@ -63,7 +63,7 @@
 #if 0
 static char sccsid[] = "@(#)commands.c 8.4 (Berkeley) 5/30/95";
 #else
-__RCSID("$NetBSD: commands.c,v 1.72 2018/12/14 06:08:18 maya Exp $");
+__RCSID("$NetBSD: commands.c,v 1.73 2018/12/14 23:40:17 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -81,6 +81,7 @@
 #include <signal.h>
 #include <stdarg.h>
 #include <unistd.h>
+#include <err.h>
 
 #include <arpa/telnet.h>
 
@@ -122,15 +123,15 @@
 static char *margv[20];
 
 static void makeargv(void);
-static int special(char *);
+static int special(const char *);
 static const char *control(cc_t);
 static int sendcmd(int, char **);
-static int send_esc(char *);
-static int send_docmd(char *);
-static int send_dontcmd(char *);
-static int send_willcmd(char *);
-static int send_wontcmd(char *);
-static int send_help(char *);
+static int send_esc(const char *);
+static int send_docmd(const char *);
+static int send_dontcmd(const char *);
+static int send_willcmd(const char *);
+static int send_wontcmd(const char *);
+static int send_help(const char *);
 static int lclchars(int);
 static int togdebug(int);
 static int togcrlf(int);
@@ -140,7 +141,7 @@
 static int togglehelp(int);
 static void settogglehelp(int);
 static int toggle(int, char *[]);
-static struct setlist *getset(char *);
+static struct setlist *getset(const char *);
 static int setcmd(int, char *[]);
 static int unsetcmd(int, char *[]);
 static int dokludgemode(int);
@@ -153,33 +154,45 @@
 static int togcrmod(int, char *[]);
 static int bye(int, char *[]);
 static void slc_help(int);
-static struct slclist *getslc(char *);
+static struct slclist *getslc(const char *);
 static int slccmd(int, char *[]);
-static struct env_lst *env_help(const unsigned char *, unsigned char *);
-static struct envlist *getenvcmd(char *);
+static struct env_lst *env_help(const char *, char *);
+static struct envlist *getenvcmd(const char *);
 #ifdef AUTHENTICATION
-static int auth_help(char *);
+static int auth_help(const char *);
 #endif
 static int status(int, char *[]);
 static const char *sockaddr_ntop (struct sockaddr *);
 typedef int (*intrtn_t)(int, char **);
 static int call(intrtn_t, ...);
-static Command *getcmd(char *);
+static Command *getcmd(const char *);
 static int help(int, char *[]);
 
+static int
+addarg(char *str)
+{
+       if ((size_t)margc >= __arraycount(margv) - 1)
+               return 0;
+       margv[margc++] = str;
+       margv[margc] = NULL;
+       return 1;
+}
+
+
 static void
 makeargv(void)
 {
     char *cp, *cp2, c;
-    char **argp = margv;
     static char bang[] = "!";
 
     margc = 0;
+    margv[0] = NULL;
     cp = line;
     if (*cp == '!') {          /* Special case shell escape */
-       strlcpy(saveline, line, sizeof(saveline)); /* save for shell command */
-       *argp++ = bang;         /* No room in string to get this */
-       margc++;
+       /* save for shell command */
+       strlcpy(saveline, line, sizeof(saveline));
+       if (!addarg(bang))
+           return;
        cp++;
     }
     while ((c = *cp) != '\0') {
@@ -188,8 +201,8 @@
            c = *++cp;
        if (c == '\0')
            break;
-       *argp++ = cp;
-       margc += 1;
+       if (!addarg(cp))
+           return;
        for (cp2 = cp; c != '\0'; c = *++cp) {
            if (inquote) {
                if (c == inquote) {
@@ -200,11 +213,8 @@
                if (c == '\\') {
                    if ((c = *++cp) == '\0')
                        break;
-               } else if (c == '"') {
-                   inquote = '"';
-                   continue;
-               } else if (c == '\'') {
-                   inquote = '\'';
+               } else if (c == '"' || c == '\'') {
+                   inquote = c;
                    continue;
                } else if (isspace((unsigned char)c))
                    break;
@@ -216,7 +226,6 @@
            break;
        cp++;
     }
-    *argp++ = 0;
 }
 
 /*
@@ -226,7 +235,7 @@
  */
 
 static int
-special(char *s)
+special(const char *s)
 {
        char c;
        char b;
@@ -271,17 +280,17 @@
        }
        if (uic >= 0x80) {
                buf[0] = '\\';
-               buf[1] = ((c>>6)&07) + '0';
-               buf[2] = ((c>>3)&07) + '0';
-               buf[3] = (c&07) + '0';
-               buf[4] = 0;
+               buf[1] = (char)(((c >> 6) & 07) + '0');
+               buf[2] = (char)(((c >> 3) & 07) + '0');
+               buf[3] = (char)((c & 07) + '0');
+               buf[4] = '\0';
        } else if (uic >= 0x20) {
-               buf[0] = c;
-               buf[1] = 0;
+               buf[0] = (char)c;
+               buf[1] = '\0';
        } else {
                buf[0] = '^';
-               buf[1] = '@'+c;
-               buf[2] = 0;
+               buf[1] = (char)('@' + c);
+               buf[2] = '\0';
        }
        return (buf);
 }
@@ -300,7 +309,7 @@
     int                needconnect;    /* Need to be connected */
     int                narg;           /* Number of arguments */
     int                (*handler)      /* Routine to perform (for special ops) */
-                       (char *);
+                       (const char *);
     int                nbyte;          /* Number of bytes to send this command */
     int                what;           /* Character to be sent (<0 ==> special) */
 };
@@ -411,7 +420,7 @@
            success += (*s->handler)(argv[i+1]);
            i += s->narg;
        } else {
-           NET2ADD(IAC, s->what);
+           NET2ADD(IAC, (unsigned char)s->what);
            printoption("SENT", IAC, s->what);
        }
     }
@@ -419,42 +428,44 @@
 }
 
 static int
-send_esc(char *s)
+send_esc(const char *s)
 {
     NETADD(escape);
     return 1;
 }
 
 static int
-send_docmd(char *name)
+send_docmd(const char *name)
 {
-    return(send_tncmd(send_do, "do", name));
+    return send_tncmd(send_do, "do", name);
+}
+
+static int
+send_dontcmd(const char *name)
+{
+    return send_tncmd(send_dont, "dont", name);
 }



Home | Main Index | Thread Index | Old Index