Subject: bin/35408: bin/cat wasting memory (more or less)
To: None <gnats-admin@netbsd.org, netbsd-bugs@netbsd.org>
From: Andreas Wiese <andreas.wiese@instandbesetzt.net>
List: netbsd-bugs
Date: 01/11/2007 21:30:00
>Number: 35408
>Category: bin
>Synopsis: bin/cat »wastes« memory when output blocksize < BUFSIZ
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: bin-bug-people
>State: open
>Class: change-request
>Submitter-Id: net
>Arrival-Date: Thu Jan 11 21:30:00 +0000 2007
>Originator: Andreas Wiese
>Release: NetBSD 4.99.8
>Organization:
BSD-Crew Dresden
>Environment:
System: NetBSD schroeder.lan.instandbesetzt.net 4.99.8 NetBSD 4.99.8 (SCHROEDER) #0: Thu Jan 11 21:37:15 CET 2007 root@schroeder.lan.instandbesetzt.net:/usr/obj/sys/arch/i386/compile/SCHROEDER i386
Architecture: i386
Machine: i386
>Description:
I was bored and read some sources. If in raw-mode, cat(1) does check
the blocksize of it's output, and allocates a buffer via malloc(3) with
at least BUFSIZ bytes. But for the case malloc(3) fails, we also have a
static buffer 'static char *fb_buf[BUFSIZ]'. Why not using this one if
we otherwise would only allocate the same size (when blocksize is not
greater than BUFSIZE)?
Yes, this are only 1024 bytes but I don't see the sense in
allocating this memory twice. And both fp_buf and buf should be on the
heap, so where's the difference (is there any)?
>How-To-Repeat:
n/a
>Fix:
diff -ur cat.o/cat.c cat/cat.c
--- cat.o/cat.c 2007-01-09 22:24:52.000000000 +0100
+++ cat/cat.c 2007-01-09 22:32:56.000000000 +0100
@@ -295,14 +295,13 @@
wfd = fileno(stdout);
if (buf == NULL) {
- if (fstat(wfd, &sbuf) == 0) {
- bsize = sbuf.st_blksize > BUFSIZ ?
- sbuf.st_blksize : BUFSIZ;
+ if (fstat(wfd, &sbuf) == 0 && sbuf.st_blksize > BUFSIZ) {
+ bsize = sbuf.st_blksize;
buf = malloc(bsize);
}
if (buf == NULL) {
- buf = fb_buf;
bsize = BUFSIZ;
+ buf = fb_buf;
}
}
while ((nr = read(rfd, buf, bsize)) > 0)
HAND & LG -- aw
np: Kaizers Orchestra (Ompa til du dør) -- 04. 170
--
Nicht die Schönheit entscheidet, wen wir lieben, sondern
die Liebe entscheidet, wen wir schön finden.