From ed03ace39d655a13790c49088bc74e6da44e4da4 Mon Sep 17 00:00:00 2001 From: Ray Lai Date: Mon, 1 Jun 2009 22:57:15 +0000 Subject: 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 --- games/hack/hack.pager.c | 19 +++++++++++-------- 1 file 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; } -- cgit v1.2.3