From owner-freebsd-stable Wed Sep 25 23:16:20 2002 Delivered-To: freebsd-stable@freebsd.org Received: from mx1.FreeBSD.org (mx1.FreeBSD.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id D249E37B401 for ; Wed, 25 Sep 2002 23:16:18 -0700 (PDT) Received: from tp.databus.com (p70-227.acedsl.com [66.114.70.227]) by mx1.FreeBSD.org (Postfix) with ESMTP id 3516E43E3B for ; Wed, 25 Sep 2002 23:16:18 -0700 (PDT) (envelope-from barney@tp.databus.com) Received: from tp.databus.com (localhost.databus.com [127.0.0.1]) by tp.databus.com (8.12.6/8.12.6) with ESMTP id g8Q6GHE0016866 for ; Thu, 26 Sep 2002 02:16:17 -0400 (EDT) (envelope-from barney@tp.databus.com) Received: (from barney@localhost) by tp.databus.com (8.12.6/8.12.6/Submit) id g8Q6GH9I016865 for stable@freebsd.org; Thu, 26 Sep 2002 02:16:17 -0400 (EDT) Date: Thu, 26 Sep 2002 02:16:17 -0400 From: Barney Wolff To: stable@freebsd.org Subject: Re: [v]asprintf leaks memory Message-ID: <20020926061617.GA16599@tp.databus.com> References: <20020925133219.GA59210@HAL9000.homeunix.com> <20020925155222.GA4874@tp.databus.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20020925155222.GA4874@tp.databus.com> User-Agent: Mutt/1.4i X-Scanned-By: MIMEDefang 2.21 (www . roaringpenguin . com / mimedefang) Sender: owner-freebsd-stable@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG There is possibly a more serious bug here. If vfprintf returns with f._bf._base NULL, it would seem that *f._p = '\0'; will store into freed memory. Perhaps harmless, but that assumes a particular malloc implementation. What's more, if the reallocf actually switches buffers, the 0 may not have been copied, and may have been beyond the end of the original buffer. If vfprintf always leaves room for the 0, the reallocf call is never necessary. I think safer code would read: ret = vfprintf(&f, fmt, ap); va_end(ap); if (f._bf._base != NULL) f._bf._base = reallocf(f._bf._base, ret + 1); if (f._bf._base != NULL) f._bf._base[ret] = '\0'; else { errno = ENOMEM; ret = -1; } *str = (char *)f._bf._base; return ret; } Barney On Wed, Sep 25, 2002 at 06:32:19AM -0700, David Schultz wrote: > Index: src/lib/libc/stdio/asprintf.c > =================================================================== > RCS file: /home/ncvs/src/lib/libc/stdio/asprintf.c,v > retrieving revision 1.6 > diff -u -u -r1.6 asprintf.c > --- src/lib/libc/stdio/asprintf.c 1999/08/28 00:00:55 1.6 > +++ src/lib/libc/stdio/asprintf.c 2002/09/25 13:08:48 > @@ -71,7 +71,8 @@ > ret = vfprintf(&f, fmt, ap); > *f._p = '\0'; > va_end(ap); > - f._bf._base = reallocf(f._bf._base, f._bf._size + 1); > + if (f._bf._base != NULL) > + f._bf._base = reallocf(f._bf._base, f._bf._size + 1); > if (f._bf._base == NULL) { > errno = ENOMEM; > ret = -1; -- Barney Wolff I'm available by contract or FT: http://www.databus.com/bwresume.pdf To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-stable" in the body of the message