Module Name:    src
Committed By:   christos
Date:           Sat Nov 21 14:59:51 UTC 2015

Modified Files:
        src/usr.bin/wall: Makefile wall.c

Log Message:
PR/50454: Timo Buhrmester: Fix wrong allocation for wall(1) -g
Modernize.


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/usr.bin/wall/Makefile
cvs rdiff -u -r1.29 -r1.30 src/usr.bin/wall/wall.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/wall/Makefile
diff -u src/usr.bin/wall/Makefile:1.10 src/usr.bin/wall/Makefile:1.11
--- src/usr.bin/wall/Makefile:1.10	Mon May 28 08:06:32 2007
+++ src/usr.bin/wall/Makefile	Sat Nov 21 09:59:51 2015
@@ -1,7 +1,8 @@
-#	$NetBSD: Makefile,v 1.10 2007/05/28 12:06:32 tls Exp $
+#	$NetBSD: Makefile,v 1.11 2015/11/21 14:59:51 christos Exp $
 #	@(#)Makefile	8.1 (Berkeley) 6/6/93
 
 .include <bsd.own.mk>
+WARNS=6
 
 USE_FORT?= yes	# setuid
 PROG=	wall

Index: src/usr.bin/wall/wall.c
diff -u src/usr.bin/wall/wall.c:1.29 src/usr.bin/wall/wall.c:1.30
--- src/usr.bin/wall/wall.c:1.29	Tue Sep  6 14:45:21 2011
+++ src/usr.bin/wall/wall.c	Sat Nov 21 09:59:51 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: wall.c,v 1.29 2011/09/06 18:45:21 joerg Exp $	*/
+/*	$NetBSD: wall.c,v 1.30 2015/11/21 14:59:51 christos Exp $	*/
 
 /*
  * Copyright (c) 1988, 1990, 1993
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 19
 #if 0
 static char sccsid[] = "@(#)wall.c	8.2 (Berkeley) 11/16/93";
 #endif
-__RCSID("$NetBSD: wall.c,v 1.29 2011/09/06 18:45:21 joerg Exp $");
+__RCSID("$NetBSD: wall.c,v 1.30 2015/11/21 14:59:51 christos Exp $");
 #endif /* not lint */
 
 /*
@@ -68,7 +68,7 @@ __RCSID("$NetBSD: wall.c,v 1.29 2011/09/
 #include "term_chk.h"
 
 static void	addgroup(char *);
-static void	makemsg(const char *);
+static void	makemsg(struct iovec *, const char *, int);
 __dead static void	usage(void);
 
 static struct wallgroup {
@@ -78,11 +78,6 @@ static struct wallgroup {
 	struct wallgroup *next;
 } *grouplist;
 
-static int nobanner;
-static size_t mbufsize;
-static char *mbuf;
-
-/* ARGSUSED */
 int
 main(int argc, char **argv)
 {
@@ -93,15 +88,19 @@ main(int argc, char **argv)
 	gid_t egid;
 	struct wallgroup *wg;
 	struct passwd *pw;
+	int nobanner;
 
 	setprogname(argv[0]);
 	egid = getegid();
 	if (setegid(getgid()) == -1)
-		err(1, "setegid");
+		err(EXIT_FAILURE, "setegid");
 	pw = getpwnam("nobody");
+	if (pw == NULL)
+		errx(EXIT_FAILURE, "Can't find passwd entry for `nobody'");
 
 	(void)check_sender(NULL, getuid(), egid);
 
+	nobanner = 0;
 	while ((ch = getopt(argc, argv, "g:n")) != -1)
 		switch (ch) {
 		case 'n':
@@ -121,10 +120,8 @@ main(int argc, char **argv)
 	if (argc > 1)
 		usage();
 
-	makemsg(*argv);
+	makemsg(&iov, *argv, nobanner);
 
-	iov.iov_base = mbuf;
-	iov.iov_len = mbufsize;
 	(void)getutentries(NULL, &ep);
 	(void)setegid(egid);
 	for (; ep; ep = ep->next) {
@@ -153,34 +150,34 @@ main(int argc, char **argv)
 		if ((p = ttymsg(&iov, 1, ep->line, 60*5)) != NULL)
 			warnx("%s", p);
 	}
-	exit(0);
+	return EXIT_SUCCESS;
 }
 
 static void
 addgroup(char *name)
 {
-	int i;
+	size_t i;
 	struct group *grp;
 	struct wallgroup *g;
 
 	grp = getgrnam(name);
 	if ((grp = getgrnam(name)) == NULL)
-		errx(1, "unknown group `%s'", name);
+		errx(EXIT_FAILURE, "unknown group `%s'", name);
 	for (i = 0; grp->gr_mem[i]; i++)
 		continue;
 
-	g = (struct wallgroup *)malloc(sizeof *g);
+	g = malloc(sizeof(*g));
 	if (g == NULL)
-		err(1, "malloc");
+		err(EXIT_FAILURE, "malloc");
 	g->gid = grp->gr_gid;
 	g->name = name;
-	g->mem = (char **)malloc(i + 1);
+	g->mem = calloc(i + 1, sizeof(*g->mem));
 	if (g->mem == NULL)
-		err(1, "malloc");
+		err(EXIT_FAILURE, "calloc");
 	for (i = 0; grp->gr_mem[i] != NULL; i++) {
 		g->mem[i] = strdup(grp->gr_mem[i]);
 		if (g->mem[i] == NULL)
-			err(1, "malloc");
+			err(EXIT_FAILURE, "strdup");
 	}
 	g->mem[i] = NULL;
 	g->next = grouplist;
@@ -188,57 +185,66 @@ addgroup(char *name)
 }
 
 static void
-makemsg(const char *fname)
+makebanner(FILE *fp)
 {
-	int ch, cnt;
+	const char *whom, *tty;
+	char hostname[MAXHOSTNAMELEN + 1], lbuf[100];
+	time_t now;
 	struct tm *lt;
 	struct passwd *pw;
+
+	if (!(whom = getlogin()))
+		whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
+	(void)gethostname(hostname, sizeof(hostname));
+	hostname[sizeof(hostname) - 1] = '\0';
+	(void)time(&now);
+	lt = localtime(&now);
+
+	/*
+	 * all this stuff is to blank out a square for the message;
+	 * we wrap message lines at column 79, not 80, because some
+	 * terminals wrap after 79, some do not, and we can't tell.
+	 * Which means that we may leave a non-blank character
+	 * in column 80, but that can't be helped.
+	 */
+	(void)fprintf(fp, "\r%79s\r\n", " ");
+	(void)snprintf(lbuf, sizeof lbuf,
+	    "Broadcast Message from %s@%s", whom, hostname);
+	(void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
+	tty = ttyname(STDERR_FILENO);
+	if (tty == NULL)
+		tty = "??";
+	(void)snprintf(lbuf, sizeof lbuf, "        (%s) at %d:%02d %s...", tty,
+	    lt->tm_hour, lt->tm_min, lt->tm_zone);
+	(void)fprintf(fp, "%-79.79s\r\n", lbuf);
+}
+
+static void
+makemsg(struct iovec *iov, const char *fname, int nobanner)
+{
+	int ch, cnt;
 	struct stat sbuf;
-	time_t now;
 	FILE *fp;
 	int fd;
-	const char *whom, *tty;
-	char *p, tmpname[MAXPATHLEN], lbuf[100],
-	    hostname[MAXHOSTNAMELEN+1];
+	char *p, tmpname[MAXPATHLEN], lbuf[100];
+	size_t mbufsize;
+	char *mbuf;
 
 	(void)snprintf(tmpname, sizeof tmpname, "%s/wall.XXXXXX", _PATH_TMP);
 	if ((fd = mkstemp(tmpname)) == -1)
-		err(1, "can't open temporary file");
+		err(EXIT_FAILURE, "can't open temporary file");
 	(void)unlink(tmpname);
 	if (!(fp = fdopen(fd, "r+")))
-		err(1, "can't open temporary file");
+		err(EXIT_FAILURE, "can't open temporary file");
+
+	if (!nobanner)
+		makebanner(fp);
 
-	if (!nobanner) {
-		if (!(whom = getlogin()))
-			whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
-		(void)gethostname(hostname, sizeof(hostname));
-		hostname[sizeof(hostname) - 1] = '\0';
-		(void)time(&now);
-		lt = localtime(&now);
-
-		/*
-		 * all this stuff is to blank out a square for the message;
-		 * we wrap message lines at column 79, not 80, because some
-		 * terminals wrap after 79, some do not, and we can't tell.
-		 * Which means that we may leave a non-blank character
-		 * in column 80, but that can't be helped.
-		 */
-		(void)fprintf(fp, "\r%79s\r\n", " ");
-		(void)snprintf(lbuf, sizeof lbuf,
-		    "Broadcast Message from %s@%s", whom, hostname);
-		(void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
-		tty = ttyname(STDERR_FILENO);
-		if (tty == NULL)
-			tty = "??";
-		(void)snprintf(lbuf, sizeof lbuf,
-		    "        (%s) at %d:%02d %s...", tty,
-		    lt->tm_hour, lt->tm_min, lt->tm_zone);
-		(void)fprintf(fp, "%-79.79s\r\n", lbuf);
-	}
 	(void)fprintf(fp, "%79s\r\n", " ");
 
 	if (fname && !(freopen(fname, "r", stdin)))
-		err(1, "can't read %s", fname);
+		err(EXIT_FAILURE, "can't read %s", fname);
+
 	while (fgets(lbuf, sizeof(lbuf), stdin))
 		for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
 			if (cnt == 79 || ch == '\n') {
@@ -254,22 +260,24 @@ makemsg(const char *fname)
 	(void)fprintf(fp, "%79s\r\n", " ");
 	rewind(fp);
 
-	if (fstat(fd, &sbuf))
-		err(1, "can't stat temporary file");
+	if (fstat(fd, &sbuf) == -1)
+		err(EXIT_FAILURE, "can't stat temporary file");
 	if ((uint64_t)sbuf.st_size > SIZE_T_MAX)
-		errx(1, "file too big");
-	mbufsize = sbuf.st_size;
+		errx(EXIT_FAILURE, "file too big");
+	mbufsize = (size_t)sbuf.st_size;
 	if (!(mbuf = malloc(mbufsize)))
-		err(1, "malloc");
+		err(EXIT_FAILURE, "malloc");
 	if (fread(mbuf, 1, mbufsize, fp) != mbufsize)
-		err(1, "can't read temporary file");
+		err(EXIT_FAILURE, "can't read temporary file");
 	(void)fclose(fp);
+	iov->iov_base = mbuf;
+	iov->iov_len = mbufsize;
 }
 
 static void
 usage(void)
 {
 
-	(void)fprintf(stderr, "usage: %s [-g group] [file]\n", getprogname());
-	exit(1);
+	(void)fprintf(stderr, "Usage: %s [-g group] [file]\n", getprogname());
+	exit(EXIT_FAILURE);
 }

Reply via email to