From owner-freebsd-current@FreeBSD.ORG Tue Oct 10 14:05:57 2006 Return-Path: X-Original-To: freebsd-current@freebsd.org Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id C01DC16A407 for ; Tue, 10 Oct 2006 14:05:57 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from server.baldwin.cx (66-23-211-162.clients.speedfactory.net [66.23.211.162]) by mx1.FreeBSD.org (Postfix) with ESMTP id 3162C43D55 for ; Tue, 10 Oct 2006 14:05:56 +0000 (GMT) (envelope-from jhb@freebsd.org) Received: from zion.baldwin.cx (zion.baldwin.cx [192.168.0.7]) (authenticated bits=0) by server.baldwin.cx (8.13.6/8.13.6) with ESMTP id k9AE5hs1087406; Tue, 10 Oct 2006 10:05:43 -0400 (EDT) (envelope-from jhb@freebsd.org) From: John Baldwin To: freebsd-current@freebsd.org Date: Tue, 10 Oct 2006 10:01:03 -0400 User-Agent: KMail/1.9.1 References: <20061006200320.T1063@baba.farley.org> In-Reply-To: <20061006200320.T1063@baba.farley.org> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200610101001.04286.jhb@freebsd.org> X-Greylist: Sender succeeded SMTP AUTH authentication, not delayed by milter-greylist-2.0.2 (server.baldwin.cx [192.168.0.1]); Tue, 10 Oct 2006 10:05:43 -0400 (EDT) X-Virus-Scanned: ClamAV 0.88.3/2018/Tue Oct 10 08:04:40 2006 on server.baldwin.cx X-Virus-Status: Clean X-Spam-Status: No, score=-4.4 required=4.2 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.1.3 X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on server.baldwin.cx Cc: "Sean C. Farley" Subject: Re: Fix for memory leak in setenv/unsetenv X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 10 Oct 2006 14:05:57 -0000 On Friday 06 October 2006 21:13, Sean C. Farley wrote: > Many a moon ago[1], I put together a patch to fix the leak in setenv() > and unsetenv(). A few months ago, I submitted a PR (kern/99826[2]) for > the final fix. I was wondering if anyone would take a look at it to see > if any changes are still warranted. The PR contains information about > the patch and sample programs to test it out. > > Thank you. > > Sean > 1. http://lists.freebsd.org/pipermail/freebsd-hackers/2005-February/010463.html > 2. http://www.freebsd.org/cgi/query-pr.cgi?pr=kern/99826 This still won't work. The reason for the intentional leak is because of this code sequence: char *a; setenv("FOO", "0", 1); a = getenv("FOO"); setenv("FOO", "bar", 1); printf("FOO was %s\n", a); With the memory leak fixed this will use free'd memory. While this code may seem weird in a program, it actually is quite possible for a library to read and cache the value of an environment variable. If you didn't leave the leak around, the library could cause a crash if the main program (or another library) changed the environment variable the first library had a cached pointer to the value of. I know for one app at my last job we had a problem with this with TZ, and so we explicitly space padded the timezone name out to a fixed-size each time to avoid the leak. -- John Baldwin