Bug 19406

Summary: [libc] setenv(3) allocates memory which is not freed by unsetenv()
Product: Base System Reporter: Phil Pennock <phil.pennock>
Component: kernAssignee: Sean Farley <scf>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 3.4-STABLE   
Hardware: Any   
OS: Any   

Description Phil Pennock 2000-06-20 19:30:00 UTC
setenv() allocates memory for variable.  unsetenv() does not free that memory.

Fix: 

Anyone want to think how many old programs depend on some buggy aspect of the
memory allocation involved here?
How-To-Repeat: Compile the code below, run, find pid and use "ps up <pid>" to see the RSS
grow without apparent bound.

#include <stdlib.h>
#include <unistd.h>

#define Name "testing"
#define Value "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"

int
main (int argc, char *argv[])
{
	int i;

	while(1) {
		for (i=0; i<1000; ++i) {
			setenv(Name, Value, 1);
			unsetenv(Name);
		}
		sleep(1);
	}
	return 0;
}
Comment 1 dwmalone 2000-06-20 19:36:25 UTC
On Tue, Jun 20, 2000 at 08:28:13PM +0200, Phil Pennock wrote:

> Anyone want to think how many old programs depend on some buggy aspect of the
> memory allocation involved here?

I believe this is required by some spec (probably POSIX), and has been
discussed several times on the FreeBSD lists. I think the conclusion was
that the current implimentation was correct. See:

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=5604
http://www.FreeBSD.org/cgi/query-pr.cgi?pr=10341

	David.
Comment 2 Phil Pennock 2000-06-20 19:51:16 UTC
On Tue 20 Jun 2000 (19:36 +0100), David Malone wrote:
> On Tue, Jun 20, 2000 at 08:28:13PM +0200, Phil Pennock wrote:
> > Anyone want to think how many old programs depend on some buggy aspect of the
> > memory allocation involved here?
> 
> I believe this is required by some spec (probably POSIX), and has been
> discussed several times on the FreeBSD lists. I think the conclusion was
> that the current implimentation was correct. See:

Not sure about that.  But going to <http://www.opengroup.org/> and
finding the Single UNIX Specification online and searching for relevant
manual pages, putenv() is there whilst setenv() isn't.  Sorry, their
copyright prevents me posting a full URL which includes some form of
session-ID.

The implementation of putenv() on setenv() appears to not comply with
this spec.  This is what led me to find this in the first place.

*sighs*
-- 
Phil Pennock                        <pdp@nl.demon.net> <Phil.Pennock@thus.net>
Demon Internet Nederland -- Network Operations Centre -- Systems Administrator
Libertes philosophica.
Sales: +31 20 422 20 00                                Support: 0800 33 6666 8
Comment 3 Sean Farley freebsd_committer freebsd_triage 2007-08-04 19:58:57 UTC
Responsible Changed
From-To: freebsd-bugs->scf

Take ownership of this PR.  I believe I fixed it in PR kern/99826.
Comment 4 Sean Farley freebsd_committer freebsd_triage 2007-08-05 03:05:58 UTC
State Changed
From-To: open->feedback

Would you let me know if the change in kern/99826 is satisfactory to 
close this PR?  The memory leak shown in your example program should be 
fixed by this change (in 7-CURRENT). 

kern/99826:  http://www.freebsd.org/cgi/query-pr.cgi?pr=99826&cat=kern
Comment 5 Mark Linimon freebsd_committer freebsd_triage 2008-01-27 08:47:08 UTC
State Changed
From-To: feedback->closed

Feedback timeout (> 4 months).