diff options
author | Ray Lai <ray@cvs.openbsd.org> | 2009-06-01 22:57:15 +0000 |
---|---|---|
committer | Ray Lai <ray@cvs.openbsd.org> | 2009-06-01 22:57:15 +0000 |
commit | ed03ace39d655a13790c49088bc74e6da44e4da4 (patch) | |
tree | 101917ce733e52755a0584f0fcf37f779b043dce | |
parent | 66c29c8aaa9bd842aae524dd24125940522ed538 (diff) |
Fix fgets handling.
1. Skip blank lines instead of dereferencing NULL-1.
2. If bufr is a blank line don't pass bufr+1 as a pointer.
OK millert
-rw-r--r-- | games/hack/hack.pager.c | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/games/hack/hack.pager.c b/games/hack/hack.pager.c index 507371d2c93..553a8ad9e45 100644 --- a/games/hack/hack.pager.c +++ b/games/hack/hack.pager.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hack.pager.c,v 1.15 2009/06/01 09:07:56 ray Exp $ */ +/* $OpenBSD: hack.pager.c,v 1.16 2009/06/01 22:57:14 ray 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.15 2009/06/01 09:07:56 ray Exp $"; +static const char rcsid[] = "$OpenBSD: hack.pager.c,v 1.16 2009/06/01 22:57:14 ray Exp $"; #endif /* not lint */ /* This file contains the command routine dowhatis() and a pager. */ @@ -87,6 +87,7 @@ dowhatis() FILE *fp; char bufr[BUFSZ+6]; char *buf = &bufr[6], *ep, q; + size_t len; extern char readchar(); if (!(fp = fopen(DATAFILE, "r"))) @@ -97,18 +98,20 @@ dowhatis() if (q != '\t') while (fgets(buf,BUFSZ,fp)) if (*buf == q) { - ep = strchr(buf, '\n'); - if (ep) - *ep = 0; - /* else: bad data file */ + len = strcspn(buf, "\n"); + /* bad data file */ + if (len == 0) + continue; + buf[len] = '\0'; /* Expand tab 'by hand' */ if (buf[1] == '\t'){ buf = bufr; buf[0] = q; (void) strncpy(buf+1, " ", 7); + len = strlen(buf); } pline(buf); - if (ep[-1] == ';') { + if (buf[len - 1] == ';') { pline("More info? "); if (readchar() == 'y') { page_more(fp,1); /* does fclose() */ @@ -146,7 +149,7 @@ page_more(FILE *fp, int strip) while (fgets(bufr, CO, fp) && (!strip || *bufr == '\t') && !got_intrup) { bufr[strcspn(bufr, "\n")] = '\0'; - if(page_line(bufr+strip)) { + if (*bufr == '\0' || page_line(bufr+strip)) { set_pager(2); goto ret; } |