git: 8ad7a14ab49b - main - getty: code cleanup, part 1

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Fri, 04 Nov 2022 14:21:13 UTC
The branch main has been updated by des:

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

commit 8ad7a14ab49b93240676e15f404354775be931f4
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-04 14:19:27 +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
---
 libexec/getty/chat.c   | 14 +++++++-------
 libexec/getty/extern.h |  2 +-
 libexec/getty/init.c   |  2 +-
 libexec/getty/main.c   | 18 +++++++-----------
 libexec/getty/subr.c   | 16 ++++++++++------
 5 files changed, 26 insertions(+), 26 deletions(-)

diff --git a/libexec/getty/chat.c b/libexec/getty/chat.c
index 0f2def37b028..980a144cdb1d 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 == '\\')
 					{
@@ -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)
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..4ff0291c6f46 100644
--- a/libexec/getty/init.c
+++ b/libexec/getty/init.c
@@ -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 */
diff --git a/libexec/getty/main.c b/libexec/getty/main.c
index e1420586679b..605be6c9bd56 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)
@@ -215,10 +213,9 @@ main(int argc, char *argv[])
 	 * J. Gettys - MIT Project Athena.
 	 */
 	if (argc <= 2 || strcmp(argv[2], "-") == 0)
-	    strcpy(ttyn, ttyname(STDIN_FILENO));
+	    snprintf(ttyn, sizeof(ttyn), "%s", ttyname(STDIN_FILENO));
 	else {
-	    strcpy(ttyn, _PATH_DEV);
-	    strlcat(ttyn, argv[2], sizeof(ttyn));
+	    snprintf(ttyn, sizeof(ttyn), "%s%s", _PATH_DEV, argv[2]);
 	    if (strcmp(argv[0], "+") != 0) {
 		chown(ttyn, 0, 0);
 		chmod(ttyn, 0600);
@@ -762,7 +759,7 @@ putf(const char *cp)
 			puts(editedhost);
 			break;
 
-		case 'd': {
+		case 'd':
 			t = (time_t)0;
 			(void)time(&t);
 			if (Lo)
@@ -786,7 +783,6 @@ putf(const char *cp)
 		case 'v':
 			puts(kerninfo.version);
 			break;
-		}
 
 		case '%':
 			putchr('%');
@@ -804,7 +800,7 @@ 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..f8637211d424 100644
--- a/libexec/getty/subr.c
+++ b/libexec/getty/subr.c
@@ -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
@@ -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