From owner-freebsd-audit Wed Dec 20 3:46:10 2000 From owner-freebsd-audit@FreeBSD.ORG Wed Dec 20 03:46:08 2000 Return-Path: Delivered-To: freebsd-audit@freebsd.org Received: from scientia.demon.co.uk (scientia.demon.co.uk [212.228.14.13]) by hub.freebsd.org (Postfix) with ESMTP id 8852C37B400 for ; Wed, 20 Dec 2000 03:46:06 -0800 (PST) Received: from strontium.scientia.demon.co.uk ([192.168.91.36] ident=root) by scientia.demon.co.uk with esmtp (Exim 3.169 #1) id 148guG-0005sC-00 for audit@FreeBSD.ORG; Wed, 20 Dec 2000 10:55:36 +0000 Received: (from ben@localhost) by strontium.scientia.demon.co.uk (8.11.1/8.11.1) id eBKAtaw50726 for audit@FreeBSD.ORG; Wed, 20 Dec 2000 10:55:36 GMT (envelope-from ben) Date: Wed, 20 Dec 2000 10:55:36 +0000 From: Ben Smithurst To: audit@FreeBSD.ORG Subject: Re: printf(1) broken for some long format strings Message-ID: <20001220105536.A8350@strontium.scientia.demon.co.uk> References: <20001219143506.C78749@strontium.scientia.demon.co.uk> <200012191729.eBJHTps36903@billy-club.village.org> <20001219215413.H78749@strontium.scientia.demon.co.uk> <20001220124232.I54775@gsmx07.alcatel.com.au> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <20001220124232.I54775@gsmx07.alcatel.com.au> Sender: ben@scientia.demon.co.uk Sender: owner-freebsd-audit@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Peter Jeremy wrote: > One problem with your patch is that it just ensures that there is > sufficient space for the current copy. This means that realloc() is > likely to be called a number of times with slightly larger lengths > each time. This sort of behaviour is likely to lead to memory > fragmentation, which may be significant in long running processes > (like interactive shells). Over-estimating the amount of memory > needed when actually allocating memory will reduce the number of > realloc()s and hence the amount of fragmentation. ok, but I don't think realloc() will be needed many times if we just round to the nearest k boundary. I think the buffer in mklong() is only needed when there's a long string with no format strings in it followed by a format like "%d", and it's probably fairly rare for printf(1) to be used with a string like that over 1k in length. Unless anyone knows otherwise. So, my final proposal. :-) I stopped using reallocf() even though Bruce wanted it, because we use ckrealloc() in the shell, the overall code is actually a bit simpler without reallocf(). Index: printf.c =================================================================== RCS file: /usr/cvs/src/usr.bin/printf/printf.c,v retrieving revision 1.15 diff -u -r1.15 printf.c --- printf.c 2000/09/04 06:11:25 1.15 +++ printf.c 2000/12/19 22:15:31 @@ -60,6 +60,7 @@ #ifdef SHELL #define main printfcmd #include "bltin/bltin.h" +#include "memalloc.h" #else #define warnx1(a, b, c) warnx(a) #define warnx2(a, b, c) warnx(a, b) @@ -247,12 +248,23 @@ char *str; int ch; { - static char copy[64]; - int len; + static char *copy; + static size_t copy_size; + size_t len, newlen; + char *newcopy; len = strlen(str) + 2; - if (len > sizeof copy) - return NULL; + if (len > copy_size) { + newlen = ((len + 1023) >> 10) << 10; +#ifdef SHELL + if ((newcopy = ckrealloc(copy, newlen)) == NULL) +#else + if ((newcopy = realloc(copy, newlen)) == NULL) +#endif + return (NULL); + copy = newcopy; + copy_size = newlen; + } memmove(copy, str, len - 3); copy[len - 3] = 'q'; -- Ben Smithurst / ben@FreeBSD.org / PGP: 0x99392F7D To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-audit" in the body of the message