diff options
author | Gilles Chehade <gilles@cvs.openbsd.org> | 2007-09-11 15:21:06 +0000 |
---|---|---|
committer | Gilles Chehade <gilles@cvs.openbsd.org> | 2007-09-11 15:21:06 +0000 |
commit | 18590f6c720ae49a5bb4d811e6827d85c513fcfe (patch) | |
tree | faa7e813da1b9d96e659b374d353a543d00380bf | |
parent | a737f2f8b1f877858f2553f91964fd9c3ab037fc (diff) |
use strcspn to properly overwrite '\n' in fgets returned buffer
ok pyr@, ray@, millert@, moritz@, chl@
-rw-r--r-- | games/atc/main.c | 11 | ||||
-rw-r--r-- | games/bcd/bcd.c | 7 | ||||
-rw-r--r-- | games/hack/hack.pager.c | 11 | ||||
-rw-r--r-- | games/hack/hack.rumors.c | 7 | ||||
-rw-r--r-- | games/monop/monop.c | 8 |
5 files changed, 18 insertions, 26 deletions
diff --git a/games/atc/main.c b/games/atc/main.c index 35d7ae8fb01..ab15b9f3603 100644 --- a/games/atc/main.c +++ b/games/atc/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.17 2007/04/13 13:41:44 sobrado Exp $ */ +/* $OpenBSD: main.c,v 1.18 2007/09/11 15:21:05 gilles Exp $ */ /* $NetBSD: main.c,v 1.4 1995/04/27 21:22:25 mycroft Exp $ */ /*- @@ -52,7 +52,7 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 5/31/93"; #else -static char rcsid[] = "$OpenBSD: main.c,v 1.17 2007/04/13 13:41:44 sobrado Exp $"; +static char rcsid[] = "$OpenBSD: main.c,v 1.18 2007/09/11 15:21:05 gilles Exp $"; #endif #endif /* not lint */ @@ -272,8 +272,8 @@ default_game(void) return (NULL); } fclose(fp); - if ((p = strchr(line, '\n')) != NULL) - *p = '\0'; + + line[strcspn(line, "\n")] = '\0'; if (strlen(line) + strlen(_PATH_GAMES) >= sizeof(file)) { warnx("default game name too long"); return (NULL); @@ -301,8 +301,7 @@ okay_game(const char *s) while (fgets(line, sizeof(line), fp) != NULL) { char *p; - if ((p = strchr(line, '\n')) != NULL) - *p = '\0'; + line[strcspn(line, "\n")] = '\0'; if (strcmp(s, line) == 0) { if (strlen(line) + strlen(_PATH_GAMES) >= sizeof(file)) { warnx("game name too long"); diff --git a/games/bcd/bcd.c b/games/bcd/bcd.c index 063ddd940d7..f7e8cc3ec75 100644 --- a/games/bcd/bcd.c +++ b/games/bcd/bcd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bcd.c,v 1.11 2004/07/09 15:59:26 deraadt Exp $ */ +/* $OpenBSD: bcd.c,v 1.12 2007/09/11 15:21:05 gilles Exp $ */ /* $NetBSD: bcd.c,v 1.6 1995/04/24 12:22:23 cgd Exp $ */ /* @@ -43,7 +43,7 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)bcd.c 8.2 (Berkeley) 3/20/94"; #else -static char rcsid[] = "$OpenBSD: bcd.c,v 1.11 2004/07/09 15:59:26 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: bcd.c,v 1.12 2007/09/11 15:21:05 gilles Exp $"; #endif #endif /* not lint */ @@ -155,8 +155,7 @@ printcard(char *str) char *p; /* ruthlessly remove newlines and truncate at 48 characters. */ - if ((p = strchr(str, '\n'))) - *p = '\0'; + str[strcspn(str, "\n")] = '\0'; if (strlen(str) > COLUMNS) str[COLUMNS] = '\0'; diff --git a/games/hack/hack.pager.c b/games/hack/hack.pager.c index 74ce3e2d0ec..953ab7677d5 100644 --- a/games/hack/hack.pager.c +++ b/games/hack/hack.pager.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hack.pager.c,v 1.12 2007/02/21 03:53:32 ray Exp $ */ +/* $OpenBSD: hack.pager.c,v 1.13 2007/09/11 15:21:05 gilles Exp $ */ /* * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica, @@ -62,7 +62,7 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: hack.pager.c,v 1.12 2007/02/21 03:53:32 ray Exp $"; +static const char rcsid[] = "$OpenBSD: hack.pager.c,v 1.13 2007/09/11 15:21:05 gilles Exp $"; #endif /* not lint */ /* This file contains the command routine dowhatis() and a pager. */ @@ -97,8 +97,7 @@ dowhatis() if(q != '\t') while(fgets(buf,BUFSZ,fp)) if(*buf == q) { - ep = strchr(buf, '\n'); - if(ep) *ep = 0; + buf[strcspn(buf, "\n")] = '\0'; /* else: bad data file */ /* Expand tab 'by hand' */ if(buf[1] == '\t'){ @@ -144,9 +143,7 @@ page_more(FILE *fp, int strip) bufr = (char *) alloc((unsigned) CO); while (fgets(bufr, CO, fp) && (!strip || *bufr == '\t') && !got_intrup) { - ep = strchr(bufr, '\n'); - if(ep) - *ep = 0; + bufr[strcspn(bufr, "\n")] = '\0'; if(page_line(bufr+strip)) { set_pager(2); goto ret; diff --git a/games/hack/hack.rumors.c b/games/hack/hack.rumors.c index 392a030e600..4ccfab757a2 100644 --- a/games/hack/hack.rumors.c +++ b/games/hack/hack.rumors.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hack.rumors.c,v 1.5 2003/05/19 06:30:56 pjanzen Exp $ */ +/* $OpenBSD: hack.rumors.c,v 1.6 2007/09/11 15:21:05 gilles Exp $ */ /* * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica, @@ -62,7 +62,7 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: hack.rumors.c,v 1.5 2003/05/19 06:30:56 pjanzen Exp $"; +static const char rcsid[] = "$OpenBSD: hack.rumors.c,v 1.6 2007/09/11 15:21:05 gilles Exp $"; #endif /* not lint */ #include <stdio.h> @@ -105,10 +105,9 @@ static void outline(FILE *rumf) { char line[COLNO]; - char *ep; if(!fgets(line, sizeof(line), rumf)) return; - if((ep = strchr(line, '\n')) != 0) *ep = 0; + line[strcspn(line, "\n")] = '\0'; pline("This cookie has a scrap of paper inside! It reads: "); pline(line); } diff --git a/games/monop/monop.c b/games/monop/monop.c index 2392882f578..c249c94a01d 100644 --- a/games/monop/monop.c +++ b/games/monop/monop.c @@ -1,4 +1,4 @@ -/* $OpenBSD: monop.c,v 1.8 2007/03/02 04:32:32 ray Exp $ */ +/* $OpenBSD: monop.c,v 1.9 2007/09/11 15:21:05 gilles Exp $ */ /* $NetBSD: monop.c,v 1.3 1995/03/23 08:34:52 cgd Exp $ */ /* @@ -40,7 +40,7 @@ static const char copyright[] = #if 0 static char sccsid[] = "@(#)monop.c 8.1 (Berkeley) 5/31/93"; #else -static const char rcsid[] = "$OpenBSD: monop.c,v 1.8 2007/03/02 04:32:32 ray Exp $"; +static const char rcsid[] = "$OpenBSD: monop.c,v 1.9 2007/09/11 15:21:05 gilles Exp $"; #endif #endif /* not lint */ @@ -88,7 +88,6 @@ main(ac, av) static void getplayers() { - char *sp; int i, j; char buf[257]; @@ -111,8 +110,7 @@ blew_it: printf("user closed input stream, quitting...\n"); exit(0); } - if ((sp = strchr(buf, '\n')) != NULL) - *sp = '\0'; + buf[strcspn(buf, "\n")] = '\0'; } while (strlen(buf) == 0); if ((name_list[i] = play[i].name = strdup(buf)) == NULL) err(1, NULL); |