From fe330dcff7ffc91d1a9270ba8cdcc126ef9c1f7f Mon Sep 17 00:00:00 2001 From: Ray Lai Date: Wed, 23 Aug 2006 03:13:10 +0000 Subject: Remove line length limit when reading format strings from a file. Fixes PR 5208. Problem reported and fix tested by jared rr spiegel . OK weingart@ and millert@. --- usr.bin/hexdump/parse.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/usr.bin/hexdump/parse.c b/usr.bin/hexdump/parse.c index a4ec066befd..20b65b52908 100644 --- a/usr.bin/hexdump/parse.c +++ b/usr.bin/hexdump/parse.c @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.c,v 1.14 2004/11/21 19:57:16 otto Exp $ */ +/* $OpenBSD: parse.c,v 1.15 2006/08/23 03:13:09 ray Exp $ */ /* $NetBSD: parse.c,v 1.12 2001/12/07 13:37:39 bjh21 Exp $ */ /* @@ -32,7 +32,7 @@ #ifndef lint /*static char sccsid[] = "from: @(#)parse.c 5.6 (Berkeley) 3/9/91";*/ -static char rcsid[] = "$OpenBSD: parse.c,v 1.14 2004/11/21 19:57:16 otto Exp $"; +static char rcsid[] = "$OpenBSD: parse.c,v 1.15 2006/08/23 03:13:09 ray Exp $"; #endif /* not lint */ #include @@ -53,25 +53,31 @@ FU *endfu; /* format at end-of-data */ void addfile(char *name) { - char *p; FILE *fp; - int ch; - char buf[2048 + 1]; + size_t len; + char *buf, *lbuf, *p; if ((fp = fopen(name, "r")) == NULL) err(1, "fopen %s", name); - while (fgets(buf, sizeof(buf), fp)) { - if (!(p = strchr(buf, '\n'))) { - warnx("line too long."); - while ((ch = getchar()) != '\n' && ch != EOF); - continue; + + lbuf = NULL; + while ((buf = fgetln(fp, &len))) { + if (buf[len - 1] == '\n') + buf[len - 1] = '\0'; + else { + /* EOF without EOL, copy and add the NUL */ + if ((lbuf = malloc(len + 1)) == NULL) + err(1, NULL); + memcpy(lbuf, buf, len); + lbuf[len] = '\0'; + buf = lbuf; } - *p = '\0'; for (p = buf; *p && isspace((unsigned char)*p); ++p); if (!*p || *p == '#') continue; add(p); } + free(lbuf); (void)fclose(fp); } -- cgit v1.2.3