Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 23 Feb 2005 10:05:58 +0100
From:      des@des.no (=?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?=)
To:        =?iso-8859-1?q?Se=E1n_C=2E_Farley?= <sean-freebsd@farley.org>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: setenv/unsetenv's known memory leak
Message-ID:  <863bvnmyrt.fsf@xps.des.no>
In-Reply-To: <20050222173013.B26342@thor.farley.org>  22:01:12 -0600 (CST)")
References:  <20050222173013.B26342@thor.farley.org>

next in thread | previous in thread | raw e-mail | index | archive | help
Se=E1n C. Farley <sean-freebsd@farley.org> writes:
> While playing around with setenv(), I noticed that it can leak memory
> when a program overwrites a variable with a larger value.  unsetenv()
> will just leak memory.  All of this is documented in their man pages.
>
> The latest PR on this (two PR's mentioned in it are closed):
> http://www.freebsd.org/cgi/query-pr.cgi?pr=3Dmisc/19406

They were closed for a reason.  Read their audit trails.

> I could find no apparent reason for continuing to allow for the memory
> leak.  The only reason given to allow it was to permit programs to
> continue to use the environment variable retrieved by setenv() after the
> program had reset or deleted it.

Wrong.  The reason for the leak is that the initial environment array
is not malloc()ed and therefore cannot be free()d or realloc()ed.  To
work around this requires a lot of bookkeeping.

BTW, SUSv3 explicitly states that the application can not expect the
pointer returned by getenv() to remain meaningful after a subsequent
call to getenv(), setenv(), unsetenv() or (on XSI-conformant systems)
putenv(), so that argument falls flat on its face.

> Here is a test program along with a patch to stop the leak:
> http://www.farley.org/freebsd/tmp/setenv.tar.bz2

You can't possibly have tested it very thoroughly.  Try running your
test program with MALLOC_OPTIONS=3DAX in your environment:

des@xps ~/src/setenv% MALLOC_OPTIONS=3DAX ./testenv
testenv in realloc(): error: junk pointer, too high to make sense
zsh: abort (core dumped)  MALLOC_OPTIONS=3DAX ./testenv

DES
--=20
Dag-Erling Sm=F8rgrav - des@des.no



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?863bvnmyrt.fsf>