Fix for memory leak in setenv/unsetenv (take 2)

Sean C. Farley sean-freebsd at
Fri Apr 20 01:26:40 UTC 2007

On Thu, 19 Apr 2007, Julian Elischer wrote:

> Sean C. Farley wrote:
>> I have a new patch that fixes memory leaks caused by repeated calls
>> to setenv() with varying-sized values or unsetenv().  The web page
>> has a better description about it.
> I vaguely remember that several people have tried to fix this before,
> but that fixing it actually breaks some ABI..

I have been trying to fix it off and on since 2005[1].  :)  I believe I
have a change that does break the ABI.

unsetenv(3) does not currently follow the standard by returning an int.
I kept the void but recommend switching to an int.

> I may be wrong but maybe others remember better what the issue was.
> I believe that the end result was that it was considered better to leak
> memory than break the posix specified ABI in some way.

This patch still leaks memory in the sense that all calls to getenv()
have valid pointers (non-freed) throughout the run of the program.  It
only cleans up when the library exits.

sean-freebsd at

More information about the freebsd-current mailing list