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 | |
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')
-rw-r--r-- | usr.bin/hexdump/display.c | 7 | ||||
-rw-r--r-- | usr.bin/hexdump/hexdump.c | 12 | ||||
-rw-r--r-- | usr.bin/hexdump/hexdump.h | 4 |
3 files changed, 18 insertions, 5 deletions
diff --git a/usr.bin/hexdump/display.c b/usr.bin/hexdump/display.c index 75136f1185c..74b72e81503 100644 --- a/usr.bin/hexdump/display.c +++ b/usr.bin/hexdump/display.c @@ -1,4 +1,4 @@ -/* $OpenBSD: display.c,v 1.18 2009/10/27 23:59:39 deraadt Exp $ */ +/* $OpenBSD: display.c,v 1.19 2010/10/12 17:23:21 millert Exp $ */ /* $NetBSD: display.c,v 1.12 2001/12/07 15:14:29 bjh21 Exp $ */ /* @@ -310,8 +310,11 @@ next(char **argv) doskip(statok ? *_argv : "stdin", statok); if (*_argv) ++_argv; - if (!skip) + if (!skip) { + if (iobuf != NULL) + setvbuf(stdin, iobuf, _IOFBF, iobufsiz); return(1); + } } /* NOTREACHED */ } 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); diff --git a/usr.bin/hexdump/hexdump.h b/usr.bin/hexdump/hexdump.h index 440b94e8132..6d8f4a558c6 100644 --- a/usr.bin/hexdump/hexdump.h +++ b/usr.bin/hexdump/hexdump.h @@ -1,4 +1,4 @@ -/* $OpenBSD: hexdump.h,v 1.8 2007/01/28 16:37:09 miod Exp $ */ +/* $OpenBSD: hexdump.h,v 1.9 2010/10/12 17:23:21 millert Exp $ */ /* $NetBSD: hexdump.h,v 1.7 2001/12/07 15:14:29 bjh21 Exp $ */ /* @@ -78,6 +78,8 @@ extern int exitval; /* final exit value */ extern FS *fshead; /* head of format strings list */ extern long length; /* max bytes to read */ extern off_t skip; /* bytes to skip */ +extern char *iobuf; /* stdio I/O buffer */ +extern size_t iobufsiz; /* size of stdio I/O buffer */ extern enum _vflag vflag; void add(const char *); |