Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 14 Nov 2022 11:23:06 GMT
From:      =?utf-8?Q?Dag-Erling=20Sm=C3=B8rgrav?= <des@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: d488e8d0372d - stable/13 - getty: code cleanup, part 1
Message-ID:  <202211141123.2AEBN6ul040575@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=d488e8d0372d5c79fb2cb1aa6c41a59fbfb62479

commit d488e8d0372d5c79fb2cb1aa6c41a59fbfb62479
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2022-11-04 13:23:33 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2022-11-14 11:22:42 +0000

    getty: code cleanup, part 1
    
    * Avoid unnecessary use of `unsigned char *`
    * Use explicit casts when assigning `unsigned char *` to `char *` or vice versa
    * Drop unused global variables (and fix memory leak in `gettable()`)
    * Use `snprintf()` instead of `strcpy()` + `strcat()`
    * Drop spurious braces in switch
    
    Sponsored by:   Klara, Inc.
    Obtained from:  Apple OSS Distributions (in part)
    Differential Revision: https://reviews.freebsd.org/D37263
    
    (cherry picked from commit 8ad7a14ab49b93240676e15f404354775be931f4)
    
    getty: code cleanup, part 2
    
    * Clean up whitespace
    * Reindent
    
    Sponsored by:   Klara, Inc.
    
    (cherry picked from commit f285f41498ebe0bfc1dbe47d67af12ec2d131521)
    
    getty: Avoid NULL deref if stdin is not a tty.
    
    Sponsored by:   Klara, Inc.
    Obtained from:  Apple OSS Distributions
    Differential Revision: https://reviews.freebsd.org/D37265
    
    (cherry picked from commit eb4d86d529e2523a19fd7454976923319954a49d)
---
 libexec/getty/chat.c   |  32 +++++------
 libexec/getty/extern.h |   2 +-
 libexec/getty/init.c   |   6 +-
 libexec/getty/main.c   | 149 +++++++++++++++++++++++++------------------------
 libexec/getty/subr.c   |  24 ++++----
 5 files changed, 109 insertions(+), 104 deletions(-)

diff --git a/libexec/getty/chat.c b/libexec/getty/chat.c
index 0f2def37b028..0a348d6b2ebb 100644
--- a/libexec/getty/chat.c
+++ b/libexec/getty/chat.c
@@ -59,10 +59,10 @@ static volatile int alarmed = 0;
 
 static void   chat_alrm(int);
 static int    chat_unalarm(void);
-static int    getdigit(unsigned char **, int, int);
+static int    getdigit(char **, int, int);
 static char   **read_chat(char **);
 static char   *cleanchr(char **, unsigned char);
-static const char *cleanstr(const unsigned char *, int);
+static const char *cleanstr(const char *, int);
 static const char *result(int);
 static int    chat_expect(const char *);
 static int    chat_send(char const *);
@@ -104,7 +104,7 @@ chat_unalarm(void)
  */
 
 static int
-getdigit(unsigned char **ptr, int base, int max)
+getdigit(char **ptr, int base, int max)
 {
 	int i, val = 0;
 	char * q;
@@ -149,10 +149,10 @@ read_chat(char **chatstr)
 			     p != NULL;
 			     p = strtok(NULL, ws))
 			{
-				unsigned char *q, *r;
+				char *q, *r;
 
 				/* Read escapes */
-				for (q = r = (unsigned char *)p; *r; ++q)
+				for (q = r = p; *r; ++q)
 				{
 					if (*q == '\\')
 					{
@@ -248,7 +248,7 @@ cleanchr(char **buf, unsigned char ch)
 		l = 2;
 		ch &= 0x7f;
 	} else
-	l = 0;
+		l = 0;
 
 	if (ch < 32) {
 		tmp[l++] = '^';
@@ -271,9 +271,9 @@ cleanchr(char **buf, unsigned char ch)
  */
 
 static const char *
-cleanstr(const unsigned char *s, int l)
+cleanstr(const char *s, int l)
 {
-	static unsigned char * tmp = NULL;
+	static char * tmp = NULL;
 	static int tmplen = 0;
 
 	if (tmplen < l * 4 + 1)
@@ -344,7 +344,7 @@ chat_expect(const char *str)
 
 						if (chat_debug & CHATDEBUG_RECEIVE)
 							syslog(LOG_DEBUG, "chat_recv '%s' m=%d",
-								cleanchr(NULL, ch), i);
+							    cleanchr(NULL, ch), i);
 
 						if (ch == str[i])
 							got[i++] = ch;
@@ -365,9 +365,9 @@ chat_expect(const char *str)
 				}
 			}
 			alarm(0);
-        		chat_unalarm();
-        		alarmed = 0;
-        		free(got);
+			chat_unalarm();
+			alarmed = 0;
+			free(got);
 		}
 	}
 
@@ -399,13 +399,13 @@ chat_send(char const *str)
                         unsigned char ch = (unsigned char)*str++;
 
                         if (alarmed)
-        			r = 3;
+				r = 3;
                         else if (ch == PAUSE_CH)
 				usleep(500000); /* 1/2 second */
 			else  {
 				usleep(10000);	/* be kind to modem */
                                 if (write(STDOUT_FILENO, &ch, 1) != 1)
-        		  		r = alarmed ? 3 : 2;
+					r = alarmed ? 3 : 2;
                         }
                 }
                 alarm(0);
@@ -414,7 +414,7 @@ chat_send(char const *str)
 	}
 
         if (chat_debug & CHATDEBUG_SEND)
-          syslog(LOG_DEBUG, "chat_send %s", result(r));
+		syslog(LOG_DEBUG, "chat_send %s", result(r));
 
         return r;
 }
@@ -481,7 +481,7 @@ getty_chat(char *scrstr, int timeout, int debug)
                 }
 
                 if (chat_debug & CHATDEBUG_MISC)
-                  syslog(LOG_DEBUG, "getty_chat %s", result(r));
+			syslog(LOG_DEBUG, "getty_chat %s", result(r));
 
         }
         return r;
diff --git a/libexec/getty/extern.h b/libexec/getty/extern.h
index fbee272d109c..6872acb4f6f7 100644
--- a/libexec/getty/extern.h
+++ b/libexec/getty/extern.h
@@ -48,7 +48,7 @@ const char *autobaud(void);
 int	 delaybits(void);
 void	 edithost(const char *);
 void	 gendefaults(void);
-void	 gettable(const char *, char *);
+void	 gettable(const char *);
 void	 makeenv(char *[]);
 const char *portselector(void);
 void	 set_ttydefaults(int);
diff --git a/libexec/getty/init.c b/libexec/getty/init.c
index ec911ca7223f..79b9601a2be1 100644
--- a/libexec/getty/init.c
+++ b/libexec/getty/init.c
@@ -34,7 +34,7 @@
 static char sccsid[] = "@(#)from: init.c	8.1 (Berkeley) 6/4/93";
 #endif
 static const char rcsid[] =
-  "$FreeBSD$";
+    "$FreeBSD$";
 #endif /* not lint */
 
 /*
@@ -53,7 +53,7 @@ static char nullstr[] = "";
 static char loginprg[] = _PATH_LOGIN;
 static char datefmt[] = "%+";
 
-#define M(a) (&omode.c_cc[a])
+#define M(a) (char *)(&omode.c_cc[a])
 
 struct	gettystrs gettystrs[] = {
 	{ "nx", NULL, NULL },		/* next table */
@@ -123,7 +123,7 @@ struct	gettynums gettynums[] = {
 	{ "dc", 0, 0, 0 },		/* debug chat script value */
 	{ NULL, 0, 0, 0 }
 };
-  
+
 
 struct	gettyflags gettyflags[] = {
 	{ "ht",	0, 0, 0, 0 },		/* has tabs */
diff --git a/libexec/getty/main.c b/libexec/getty/main.c
index 743a0508c561..fad3811adcf6 100644
--- a/libexec/getty/main.c
+++ b/libexec/getty/main.c
@@ -58,6 +58,7 @@ __FBSDID("$FreeBSD$");
 #include <libutil.h>
 #include <setjmp.h>
 #include <signal.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <syslog.h>
@@ -100,10 +101,7 @@ static char	name[MAXLOGNAME*3];
 static char	ttyn[32];
 
 #define	OBUFSIZ		128
-#define	TABBUFSIZ	512
 
-static char	defent[TABBUFSIZ];
-static char	tabent[TABBUFSIZ];
 static const char	*tname;
 
 static char	*env[128];
@@ -191,7 +189,7 @@ main(int argc, char *argv[])
 	gethostname(hostname, sizeof(hostname) - 1);
 	hostname[sizeof(hostname) - 1] = '\0';
 	if (hostname[0] == '\0')
-		strcpy(hostname, "Amnesiac");
+		snprintf(hostname, sizeof(hostname), "Amnesiac");
 
 	/*
 	 * Limit running time to deal with broken or dead lines.
@@ -201,7 +199,7 @@ main(int argc, char *argv[])
 	limit.rlim_cur = GETTY_TIMEOUT;
 	(void)setrlimit(RLIMIT_CPU, &limit);
 
-	gettable("default", defent);
+	gettable("default");
 	gendefaults();
 	tname = "default";
 	if (argc > 1)
@@ -214,83 +212,87 @@ main(int argc, char *argv[])
 	 * that the file descriptors are already set up for us.
 	 * J. Gettys - MIT Project Athena.
 	 */
-	if (argc <= 2 || strcmp(argv[2], "-") == 0)
-	    strcpy(ttyn, ttyname(STDIN_FILENO));
-	else {
-	    strcpy(ttyn, _PATH_DEV);
-	    strlcat(ttyn, argv[2], sizeof(ttyn));
-	    if (strcmp(argv[0], "+") != 0) {
-		chown(ttyn, 0, 0);
-		chmod(ttyn, 0600);
-		revoke(ttyn);
-
-		/*
-		 * Do the first scan through gettytab.
-		 * Terminal mode parameters will be wrong until
-		 * defttymode() called, but they're irrelevant for
-		 * the initial setup of the terminal device.
-		 */
-		dogettytab();
-
-		/*
-		 * Init or answer modem sequence has been specified.
-		 */
-		if (IC || AC) {
-			if (!opentty(ttyn, O_RDWR|O_NONBLOCK))
-				exit(1);
-			defttymode();
-			setttymode(1);
+	if (argc <= 2 || strcmp(argv[2], "-") == 0) {
+		char *n = ttyname(STDIN_FILENO);
+		if (n == NULL) {
+			syslog(LOG_ERR, "ttyname: %m");
+			exit(1);
 		}
+		snprintf(ttyn, sizeof(ttyn), "%s", n);
+	} else {
+		snprintf(ttyn, sizeof(ttyn), "%s%s", _PATH_DEV, argv[2]);
+		if (strcmp(argv[0], "+") != 0) {
+			chown(ttyn, 0, 0);
+			chmod(ttyn, 0600);
+			revoke(ttyn);
+
+			/*
+			 * Do the first scan through gettytab.
+			 * Terminal mode parameters will be wrong until
+			 * defttymode() called, but they're irrelevant for
+			 * the initial setup of the terminal device.
+			 */
+			dogettytab();
 
-		if (IC) {
-			if (getty_chat(IC, CT, DC) > 0) {
-				syslog(LOG_ERR, "modem init problem on %s", ttyn);
-				(void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
-				exit(1);
+			/*
+			 * Init or answer modem sequence has been specified.
+			 */
+			if (IC || AC) {
+				if (!opentty(ttyn, O_RDWR|O_NONBLOCK))
+					exit(1);
+				defttymode();
+				setttymode(1);
 			}
-		}
-
-		if (AC) {
-			fd_set rfds;
-			struct timeval to;
-			int i;
 
-			FD_ZERO(&rfds);
-			FD_SET(0, &rfds);
-        		to.tv_sec = RT;
-        		to.tv_usec = 0;
-			i = select(32, &rfds, NULL, NULL, RT ? &to : NULL);
-        		if (i < 0) {
-				syslog(LOG_ERR, "select %s: %m", ttyn);
-			} else if (i == 0) {
-				syslog(LOG_NOTICE, "recycle tty %s", ttyn);
-				(void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
-				exit(0);  /* recycle for init */
+			if (IC) {
+				if (getty_chat(IC, CT, DC) > 0) {
+					syslog(LOG_ERR, "modem init problem on %s", ttyn);
+					(void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
+					exit(1);
+				}
 			}
-			i = getty_chat(AC, CT, DC);
-			if (i > 0) {
-				syslog(LOG_ERR, "modem answer problem on %s", ttyn);
-				(void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
-				exit(1);
+
+			if (AC) {
+				fd_set rfds;
+				struct timeval to;
+				int i;
+
+				FD_ZERO(&rfds);
+				FD_SET(0, &rfds);
+				to.tv_sec = RT;
+				to.tv_usec = 0;
+				i = select(32, &rfds, NULL, NULL, RT ? &to : NULL);
+				if (i < 0) {
+					syslog(LOG_ERR, "select %s: %m", ttyn);
+				} else if (i == 0) {
+					syslog(LOG_NOTICE, "recycle tty %s", ttyn);
+					(void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
+					exit(0);  /* recycle for init */
+				}
+				i = getty_chat(AC, CT, DC);
+				if (i > 0) {
+					syslog(LOG_ERR, "modem answer problem on %s", ttyn);
+					(void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
+					exit(1);
+				}
+			} else { /* maybe blocking open */
+				if (!opentty(ttyn, O_RDWR | (NC ? O_NONBLOCK : 0 )))
+					exit(1);
 			}
-		} else { /* maybe blocking open */
-			if (!opentty(ttyn, O_RDWR | (NC ? O_NONBLOCK : 0 )))
-				exit(1);
 		}
-	    }
 	}
 
 	defttymode();
 	for (;;) {
 
 		/*
-		 * if a delay was specified then sleep for that 
+		 * if a delay was specified then sleep for that
 		 * number of seconds before writing the initial prompt
 		 */
 		if (first_sleep && DE) {
-		    sleep(DE);
-		    /* remove any noise */
-		    (void)tcflush(STDIN_FILENO, TCIOFLUSH);
+			sleep(DE);
+			/* remove any noise */
+			(void)tcflush(STDIN_FILENO, TCIOFLUSH);
 		}
 		first_sleep = 0;
 
@@ -318,7 +320,7 @@ main(int argc, char *argv[])
 				char * cp;
 
 				while ((cp = get_line(fd)) != NULL) {
-					  putf(cp);
+					putf(cp);
 				}
 				close(fd);
 			}
@@ -439,7 +441,7 @@ opentty(const char *tty, int flags)
 			return 0;
 		sleep(60);
 	}
-	if (login_tty(i) < 0) { 
+	if (login_tty(i) < 0) {
 		if (daemon(0,0) < 0) {
 			syslog(LOG_ERR,"daemon: %m");
 			close(i);
@@ -555,7 +557,7 @@ getname(void)
 		   See RFC1662.
 		   Derived from code from Michael Hancock, <michaelh@cet.co.jp>
 		   and Erik 'PPP' Olson, <eriko@wrq.com>
-		 */
+		*/
 
 		if (PP && (cs == PPP_FRAME)) {
 			ppp_state = 1;
@@ -564,7 +566,7 @@ getname(void)
 		} else if (ppp_state == 2 && cs == PPP_ESCAPE) {
 			ppp_state = 3;
 		} else if ((ppp_state == 2 && cs == PPP_CONTROL)
-			|| (ppp_state == 3 && cs == PPP_CONTROL_ESCAPED)) {
+		    || (ppp_state == 3 && cs == PPP_CONTROL_ESCAPED)) {
 			ppp_state = 4;
 		} else if (ppp_state == 4 && cs == PPP_LCP_HI) {
 			ppp_state = 5;
@@ -761,7 +763,7 @@ putf(const char *cp)
 			puts(editedhost);
 			break;
 
-		case 'd': {
+		case 'd':
 			t = (time_t)0;
 			(void)time(&t);
 			if (Lo)
@@ -785,7 +787,6 @@ putf(const char *cp)
 		case 'v':
 			puts(kerninfo.version);
 			break;
-		}
 
 		case '%':
 			putchr('%');
@@ -801,9 +802,9 @@ putf(const char *cp)
 static void
 dogettytab(void)
 {
-	
+
 	/* Read the database entry. */
-	gettable(tname, tabent);
+	gettable(tname);
 
 	/*
 	 * Avoid inheriting the parity values from the default entry
diff --git a/libexec/getty/subr.c b/libexec/getty/subr.c
index 9958d193aa60..68682df6d5bd 100644
--- a/libexec/getty/subr.c
+++ b/libexec/getty/subr.c
@@ -34,7 +34,7 @@
 static char sccsid[] = "@(#)from: subr.c	8.1 (Berkeley) 6/4/93";
 #endif
 static const char rcsid[] =
-  "$FreeBSD$";
+    "$FreeBSD$";
 #endif /* not lint */
 
 /*
@@ -60,8 +60,9 @@ static const char rcsid[] =
  * Get a table entry.
  */
 void
-gettable(const char *name, char *buf)
+gettable(const char *name)
 {
+	char *buf = NULL;
 	struct gettystrs *sp;
 	struct gettynums *np;
 	struct gettyflags *fp;
@@ -155,6 +156,7 @@ gettable(const char *name, char *buf)
 			fp->value = 1 ^ fp->invrt;
 		}
 	}
+	free(buf);
 }
 
 void
@@ -186,8 +188,8 @@ setdefaults(void)
 
 	for (sp = gettystrs; sp->field; sp++)
 		if (!sp->value)
-			sp->value = !sp->defalt ? sp->defalt
-						: strdup(sp->defalt);
+			sp->value = !sp->defalt ?
+			    sp->defalt : strdup(sp->defalt);
 	for (np = gettynums; np->field; np++)
 		if (!np->set)
 			np->value = np->defalt;
@@ -202,13 +204,15 @@ charnames[] = {
 	&SU, &DS, &RP, &FL, &WE, &LN, 0
 };
 
+#define CV(a) (char *)(&tmode.c_cc[a])
+
 static char *
 charvars[] = {
-	&tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR],
-	&tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP],
-	&tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP],
-	&tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD],
-	&tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], 0
+	CV(VERASE), CV(VKILL), CV(VINTR),
+	CV(VQUIT), CV(VSTART), CV(VSTOP),
+	CV(VEOF), CV(VEOL), CV(VSUSP),
+	CV(VDSUSP), CV(VREPRINT), CV(VDISCARD),
+	CV(VWERASE), CV(VLNEXT), 0
 };
 
 void
@@ -505,7 +509,7 @@ edithost(const char *pattern)
 	 * In case of any errors, or if the pattern did not match, pass
 	 * the original hostname as is.
 	 */
- copyasis:
+copyasis:
 	strlcpy(editedhost, HN, sizeof(editedhost));
 }
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202211141123.2AEBN6ul040575>