Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.bin/wall PR/50454: Timo Buhrmester: Fix wrong allocation...
details: https://anonhg.NetBSD.org/src/rev/1ea7d1a727d5
branches: trunk
changeset: 811905:1ea7d1a727d5
user: christos <christos%NetBSD.org@localhost>
date: Sat Nov 21 14:59:51 2015 +0000
description:
PR/50454: Timo Buhrmester: Fix wrong allocation for wall(1) -g
Modernize.
diffstat:
usr.bin/wall/Makefile | 3 +-
usr.bin/wall/wall.c | 136 ++++++++++++++++++++++++++-----------------------
2 files changed, 74 insertions(+), 65 deletions(-)
diffs (264 lines):
diff -r f0fa242f9339 -r 1ea7d1a727d5 usr.bin/wall/Makefile
--- a/usr.bin/wall/Makefile Sat Nov 21 12:34:48 2015 +0000
+++ b/usr.bin/wall/Makefile Sat Nov 21 14:59:51 2015 +0000
@@ -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
diff -r f0fa242f9339 -r 1ea7d1a727d5 usr.bin/wall/wall.c
--- a/usr.bin/wall/wall.c Sat Nov 21 12:34:48 2015 +0000
+++ b/usr.bin/wall/wall.c Sat Nov 21 14:59:51 2015 +0000
@@ -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 @@
#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 @@
#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 @@
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 @@
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 @@
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 @@
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 @@
}
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");
-
- 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);
+ err(EXIT_FAILURE, "can't open temporary file");
- /*
- * 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);
- }
+ if (!nobanner)
+ makebanner(fp);
+
(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 @@
(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);
}
Home |
Main Index |
Thread Index |
Old Index