diff options
author | Charles Longeau <chl@cvs.openbsd.org> | 2007-09-07 16:30:26 +0000 |
---|---|---|
committer | Charles Longeau <chl@cvs.openbsd.org> | 2007-09-07 16:30:26 +0000 |
commit | 7e8b13bd425975b8103e6f91764990f6cb898083 (patch) | |
tree | 437ff2adf85cac43b6c8bd87f5998e4f89a7410b /sbin/restore | |
parent | 187ba8e20ad6081514a8c14c78d662f5f54d8aa3 (diff) |
check fgets return value
use sizeof buf instead of TP_BSIZE
use strcspn to properly overwrite '\n' in fgets returned buffer
ok moritz@ ray@
Diffstat (limited to 'sbin/restore')
-rw-r--r-- | sbin/restore/tape.c | 18 |
1 files changed, 8 insertions, 10 deletions
diff --git a/sbin/restore/tape.c b/sbin/restore/tape.c index 0120d703290..9cc2afe1316 100644 --- a/sbin/restore/tape.c +++ b/sbin/restore/tape.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tape.c,v 1.33 2007/09/02 15:19:25 deraadt Exp $ */ +/* $OpenBSD: tape.c,v 1.34 2007/09/07 16:30:25 chl Exp $ */ /* $NetBSD: tape.c,v 1.26 1997/04/15 07:12:25 lukem Exp $ */ /* @@ -39,7 +39,7 @@ #if 0 static char sccsid[] = "@(#)tape.c 8.6 (Berkeley) 9/13/94"; #else -static const char rcsid[] = "$OpenBSD: tape.c,v 1.33 2007/09/02 15:19:25 deraadt Exp $"; +static const char rcsid[] = "$OpenBSD: tape.c,v 1.34 2007/09/07 16:30:25 chl Exp $"; #endif #endif /* not lint */ @@ -334,10 +334,10 @@ again: do { fprintf(stderr, "Specify next volume #: "); (void)fflush(stderr); - (void)fgets(buf, TP_BSIZE, terminal); - } while (!feof(terminal) && buf[0] == '\n'); - if (feof(terminal)) - exit(1); + if (fgets(buf, sizeof buf, terminal) == NULL || + feof(terminal)) + exit(1); + } while (buf[0] == '\n'); newvol = atoi(buf); if (newvol <= 0) { fprintf(stderr, @@ -353,11 +353,9 @@ again: fprintf(stderr, "Enter ``none'' if there are no more tapes\n"); fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape); (void)fflush(stderr); - if (fgets(buf, TP_BSIZE, terminal) == NULL || feof(terminal)) + if (fgets(buf, sizeof buf, terminal) == NULL || feof(terminal)) exit(1); - i = strlen(buf); - if (i > 0 && buf[--i] == '\n') - buf[i] = '\0'; + buf[strcspn(buf, "\n")] = '\0'; if (strcmp(buf, "none") == 0) { terminateinput(); return; |