diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2010-10-12 17:23:22 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2010-10-12 17:23:22 +0000 |
commit | caebab4963480a26b31eae028efc040871c99305 (patch) | |
tree | f1ef489f3a8c874765d8c6e3c58f37a2e1524b98 /usr.bin/hexdump/hexdump.c | |
parent | 27f134ab20d82f966e3a59953db8729e4dc9d3bb (diff) |
If the input length was specified, use it to set the stdio buffer
size for fread(). Otherwise, stdio will read as much as it can
(using a very large buffer) which may cause a hang if the input
comes from a blocking device such as /dev/srandom. OK deraadt@
Diffstat (limited to 'usr.bin/hexdump/hexdump.c')
-rw-r--r-- | usr.bin/hexdump/hexdump.c | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/usr.bin/hexdump/hexdump.c b/usr.bin/hexdump/hexdump.c index 5a50100f987..9ef015db76c 100644 --- a/usr.bin/hexdump/hexdump.c +++ b/usr.bin/hexdump/hexdump.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hexdump.c,v 1.13 2009/10/27 23:59:39 deraadt Exp $ */ +/* $OpenBSD: hexdump.c,v 1.14 2010/10/12 17:23:21 millert Exp $ */ /* $NetBSD: hexdump.c,v 1.7 1997/10/19 02:34:06 lukem Exp $ */ /* @@ -30,7 +30,8 @@ * SUCH DAMAGE. */ -#include <sys/types.h> +#include <sys/param.h> +#include <err.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -40,6 +41,8 @@ FS *fshead; /* head of format strings */ int blocksize; /* data block size */ int exitval; /* final exit value */ long length = -1; /* max bytes to read */ +char *iobuf; /* stdio I/O buffer */ +size_t iobufsiz; /* size of stdio I/O buffer */ int main(int, char **); @@ -60,6 +63,11 @@ main(int argc, char *argv[]) if (blocksize < tfs->bcnt) blocksize = tfs->bcnt; } + if (length != -1) { + iobufsiz = MIN(length, blocksize); + if ((iobuf = malloc(iobufsiz)) == NULL) + err(1, NULL); + } /* rewrite the rules, do syntax checking */ for (tfs = fshead; tfs; tfs = tfs->nextfs) rewrite(tfs); |