summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRay Lai <ray@cvs.openbsd.org>2006-08-23 03:13:10 +0000
committerRay Lai <ray@cvs.openbsd.org>2006-08-23 03:13:10 +0000
commitfe330dcff7ffc91d1a9270ba8cdcc126ef9c1f7f (patch)
treedad41800d4195b0a5ea420a0dfef544f58ca7974
parent8f9be8dee4a2d5c0f11e699829f0949feb69cb8e (diff)
Remove line length limit when reading format strings from a file.
Fixes PR 5208. Problem reported and fix tested by jared rr spiegel <jrrs at hklocal dot nodeless dot net>. OK weingart@ and millert@.
-rw-r--r--usr.bin/hexdump/parse.c28
1 files 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 <sys/types.h>
@@ -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);
}