From owner-freebsd-hackers Fri Jan 30 14:27:48 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id OAA08676 for hackers-outgoing; Fri, 30 Jan 1998 14:27:48 -0800 (PST) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from whistle.com (s205m131.whistle.com [207.76.205.131]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id OAA08631 for ; Fri, 30 Jan 1998 14:27:41 -0800 (PST) (envelope-from archie@whistle.com) Received: (from smap@localhost) by whistle.com (8.7.5/8.6.12) id OAA04715 for ; Fri, 30 Jan 1998 14:27:09 -0800 (PST) Received: from bubba.whistle.com(207.76.205.7) by whistle.com via smap (V1.3) id sma004707; Fri Jan 30 14:26:52 1998 Received: (from archie@localhost) by bubba.whistle.com (8.8.7/8.6.12) id OAA17656 for freebsd-hackers@freebsd.org; Fri, 30 Jan 1998 14:26:52 -0800 (PST) From: Archie Cobbs Message-Id: <199801302226.OAA17656@bubba.whistle.com> Subject: setenv() bug.. how to fix? To: freebsd-hackers@FreeBSD.ORG Date: Fri, 30 Jan 1998 14:26:52 -0800 (PST) X-Mailer: ELM [version 2.4ME+ PL31 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG X-To-Unsubscribe: mail to majordomo@FreeBSD.org "unsubscribe hackers" I just submitted a bug (bin/5604) regarding setenv(). The main problem is that if you overwrite a variable with a value that is longer than the previous value, the memory malloc()'d for the previous value is never freed. Also, unsetenv() has the same problem -- it doesn't free the old value. I submitted a patch to fix this, but now I realize that the patch may not be entirely correct.. because the original contents of the environ[] array are not actually malloc()'d, rather they are set up by the loader (right??) So it's inappropriate to free() an "original" value.. but later values that get overwritten (or unsetenv()'d) DO need to be freed... What's the best way to properly fix this? The only way I can think of is to keep a bit array associated with the environ[] array, which specifies which entries are "original" and don't need to be free'd. Thanks, -Archie PS. This program demonstrates the memory leak bug: #include #define BSIZE 1024 char buf[BSIZE + 1]; int main(int ac, char *av[]) { int x; memset(buf, 'b', BSIZE); buf[BSIZE] = 0; for (x = 0; 1; x++) { buf[x % BSIZE] = 0; setenv("foo", buf, 1); buf[x % BSIZE] = 'b'; } return(0); } ___________________________________________________________________________ Archie Cobbs * Whistle Communications, Inc. * http://www.whistle.com