Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/cat Fix buffer allocation in function `raw_cat'. It was ...



details:   https://anonhg.NetBSD.org/src/rev/ce3d35b492b6
branches:  trunk
changeset: 472267:ce3d35b492b6
user:      hannken <hannken%NetBSD.org@localhost>
date:      Mon Apr 26 09:15:59 1999 +0000

description:
Fix buffer allocation in function `raw_cat'. It was possible to get a
buffer size of zero if stdout has a block size of 0.
The command `rsh <host> cat <files>' gives no output and no error.

- Allocate a minimum of BUFSIZ bytes for buffer.
- Use a static buffer if either `fstat' or `malloc' fails.
- Do the allocation once since stdout will not change block size.

diffstat:

 bin/cat/cat.c |  26 ++++++++++++--------------
 1 files changed, 12 insertions(+), 14 deletions(-)

diffs (54 lines):

diff -r aaa81fc79abc -r ce3d35b492b6 bin/cat/cat.c
--- a/bin/cat/cat.c     Mon Apr 26 07:04:08 1999 +0000
+++ b/bin/cat/cat.c     Mon Apr 26 09:15:59 1999 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cat.c,v 1.19 1999/03/11 12:04:18 fair Exp $    */
+/*     $NetBSD: cat.c,v 1.20 1999/04/26 09:15:59 hannken Exp $ */
 
 /*
  * Copyright (c) 1989, 1993
@@ -47,7 +47,7 @@
 #if 0
 static char sccsid[] = "@(#)cat.c      8.2 (Berkeley) 4/27/95";
 #else
-__RCSID("$NetBSD: cat.c,v 1.19 1999/03/11 12:04:18 fair Exp $");
+__RCSID("$NetBSD: cat.c,v 1.20 1999/04/26 09:15:59 hannken Exp $");
 #endif
 #endif /* not lint */
 
@@ -254,25 +254,23 @@
 raw_cat(rfd)
        int rfd;
 {
-       int wfd = 0;
+       int wfd;
        static char *buf;
+       static char fb_buf[BUFSIZ];
        struct stat sbuf;
        static size_t bsize;
        ssize_t nr, nw, off;
 
        wfd = fileno(stdout);
-       if (fstat(wfd, &sbuf))
-               err(1, "%s", filename);
-       if (bsize < sbuf.st_blksize) {
-               bsize = MIN(sbuf.st_blksize, SSIZE_MAX);
-               if (bsize != sbuf.st_blksize) {
-                       bsize = 16384;
+       if (buf == NULL) {
+               if (fstat(wfd, &sbuf) == 0) {
+                       bsize = MIN(sbuf.st_blksize, SSIZE_MAX);
+                       bsize = MAX(bsize, BUFSIZ);
+                       buf = malloc(bsize);
                }
-               if(buf != NULL)
-                       free(buf);
-               if ((buf = malloc(bsize)) == NULL) {
-                       bsize = 0;
-                       err(1, "malloc failed: cannot allocate buffer");
+               if (buf == NULL) {
+                       buf = fb_buf;
+                       bsize = BUFSIZ;
                }
        }
        while ((nr = read(rfd, buf, bsize)) > 0)



Home | Main Index | Thread Index | Old Index