setenv memory leak fix (take 3)

Sean C. Farley sean-freebsd at
Fri Apr 27 01:10:35 UTC 2007

I have a new version[1] of my fix for the memory leak in setenv(),
unsetenv() and putenv().

1. Fix for the rebuilding of environ after a new variable slot is added.
    I noticed this when Java was stuck in an endless loop.  It was not
    rebuilding environ when it should have.  Java needs environ to
    execve() itself with LD_LIBRARY_PATH defined with its library
    directories prefixed to the existing value.  It never saw the change,
    so it tried continuously.
2. Speed tweaks.  inline'ing helped a bit.

Here are some timings I took with the current code in FreeBSD and my
last two versions.  Each call is executed 1000000 times and the results
are in seconds.  The latest code is comparable for getenv() to the
current code base but setenv() is much faster.  This test does not
perform alternating between different size values.

                           Current     Take 2      Take 3
                           --------    --------    --------
getenv(name):             0.112457    0.216450    0.070750
setenv(name, value1, 1):  0.542081    0.491348    0.256943
getenv(name):             0.107742    0.256953    0.095008
getenv(name2):            0.157418    0.342412    0.122382
setenv(name, value2, 1):  0.389988    0.411441    0.169207
getenv(name):             0.156040    0.347334    0.121014
getenv(name2):            0.145866    0.345817    0.119156

Are there any opinions on having unsetenv() return a value to match
unsetenv() in IEEE Std 1003.1?

sean-freebsd at

More information about the freebsd-current mailing list