diff options
author | Charles Longeau <chl@cvs.openbsd.org> | 2007-09-09 12:36:39 +0000 |
---|---|---|
committer | Charles Longeau <chl@cvs.openbsd.org> | 2007-09-09 12:36:39 +0000 |
commit | c59518e8bf05d8494a56e3664893de452a616f72 (patch) | |
tree | 1ddd805741584fbfb51f8394a80edbc75dcc625f /usr.bin/msgs | |
parent | 1d1dc042dbf29e6c033bd8b38bf65fe37f95b621 (diff) |
check fgets return value
use strcspn to properly overwrite '\n' in fgets returned buffer
with help and ok from moritz@ and ray@
Diffstat (limited to 'usr.bin/msgs')
-rw-r--r-- | usr.bin/msgs/msgs.c | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/usr.bin/msgs/msgs.c b/usr.bin/msgs/msgs.c index 24085ee1f98..201de211f19 100644 --- a/usr.bin/msgs/msgs.c +++ b/usr.bin/msgs/msgs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: msgs.c,v 1.31 2007/05/17 10:59:26 moritz Exp $ */ +/* $OpenBSD: msgs.c,v 1.32 2007/09/09 12:36:38 chl Exp $ */ /* $NetBSD: msgs.c,v 1.7 1995/09/28 06:57:40 tls Exp $ */ /*- @@ -40,7 +40,7 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)msgs.c 8.2 (Berkeley) 4/28/95"; #else -static char rcsid[] = "$OpenBSD: msgs.c,v 1.31 2007/05/17 10:59:26 moritz Exp $"; +static char rcsid[] = "$OpenBSD: msgs.c,v 1.32 2007/09/09 12:36:38 chl Exp $"; #endif #endif /* not lint */ @@ -75,6 +75,7 @@ static char rcsid[] = "$OpenBSD: msgs.c,v 1.31 2007/05/17 10:59:26 moritz Exp $" #include <sys/stat.h> #include <dirent.h> #include <ctype.h> +#include <err.h> #include <errno.h> #include <fcntl.h> #include <pwd.h> @@ -371,7 +372,8 @@ main(int argc, char *argv[]) printf("Message %d:\nFrom %s %sSubject: ", nextmsg, pw->pw_name, ctime(&t)); fflush(stdout); - fgets(inbuf, sizeof inbuf, stdin); + if (fgets(inbuf, sizeof inbuf, stdin) == NULL) + errx(1, "could not read input"); putchar('\n'); fflush(stdout); fprintf(newmsg, "From %s %sSubject: %s\n", @@ -380,8 +382,7 @@ main(int argc, char *argv[]) } else blankline = seensubj = NO; for (;;) { - fgets(inbuf, sizeof inbuf, stdin); - if (feof(stdin) || ferror(stdin)) + if (fgets(inbuf, sizeof inbuf, stdin) == NULL) break; blankline = (blankline || (inbuf[0] == '\n')); seensubj = (seensubj || @@ -752,9 +753,9 @@ ask(char *prompt) printf("%s ", prompt); fflush(stdout); intrpflg = NO; - (void) fgets(inbuf, sizeof inbuf, stdin); - if ((n = strlen(inbuf)) > 0 && inbuf[n - 1] == '\n') - inbuf[n - 1] = '\0'; + if (fgets(inbuf, sizeof inbuf, stdin) == NULL) + errx(1, "could not read input"); + inbuf[strcspn(inbuf, "\n")] = '\0'; if (intrpflg) inbuf[0] = 'x'; |