Using environment variables without leaking memory?

Rob Landley rob at
Thu Nov 2 21:38:26 UTC 2006

On Thursday 02 November 2006 3:14 pm, Rich Felker wrote:
> I would prefer the former because, while it could break broken
> applications, it also eliminates the existing memory leak in existing
> correct applications.

Existing "correct" applications, if there are any (and you argued earlier that 
there aren't), leak memory like a sieve and don't care because they're 
following the standard and it's the universe's problem if the standard is 
crap and nobody anywhere implements it usefully.

I really don't want to use software written like that, for anything.  They 
probably use CORBA on Linux Standard Base 2.0 systems (with the patched 
version of gcc 3.3 for the C++ stuff).  Because it's STANDARD.  And ISO-9002.

(One of my favorite old dilbert strips, only time the PHB was actually the 
good guy...  PHB: "You want ISO 9000 certification?"  "Customer: Of course."  
PHB: "You don't care how bad our processes are as long as they're documented 
and we follow them?"  "Customer: Yes."  PHB: slaps a piece of paper down on 
the table.  "Our documented procedure is that I must laugh in your face and 
double our price.")

> BTW it should already be possible to avoid memleaks if you just use
> putenv and not setenv,

You have to track which variables you set and which ones you inherited from 
the environment.  It's _possible_, it's just slightly more work than not 
using the library functions at _all_ and just playing with "environ" 

> but maybe glibc/uclibc strdup putenv entries or 
> something..? :(

Nope, from the putenv man page:

       The  putenv()  function  adds or changes the value of environment vari‐
       ables.  The argument string is of the form name=value.   If  name  does
       not already exist in the environment, then string is added to the envi‐
       ronment.  If name does exist, then the value of name in the environment
       is  changed  to value.  The string pointed to by string becomes part of
       the environment, so altering the string changes the environment.

If you did a strdup, then altering the string wouldn't change the environment.  
Real systems care what the man page says way more than they care about what 
some spec says.  If it doesn't match the man page, people _notice_.  (And 
often, update the man page to match reality.)

"Perfection is reached, not when there is no longer anything to add, but
when there is no longer anything to take away." - Antoine de Saint-Exupery

More information about the uClibc mailing list