clearenv for newlib

Jeff Johnston
Fri Sep 14 18:51:00 GMT 2007

Joel Sherrill wrote:
> Hi,
> I am porting a library to RTEMS that is calling clearenv().
> I am willing to implement one for newlib.  Is this a
> method OK for newlib to have?
> I was looking at the current environ code to gauge the
> difficulty and noticed that unsetenv() does not free
> any memory.  Since putenv calls strdup(), I wonder
> if this is right.
> grep free libc/stdlib/*env*
> libc/stdlib/putenv_r.c:      (void) _free_r (reent_ptr, p);
> libc/stdlib/putenv_r.c:  (void) _free_r (reent_ptr, p);
> Those are error paths.  Is this a leak?
> --joel
Hi Joel,

  There isn't an issue with putenv because you'll notice that the free 
of the strdup'd string is done after the call to _setenv_r (success or 

  There is a problem because of the environ pointer.  We don't know what 
each item points to.  It may be an individually allocated string or it 
might not.  When we add new strings and we are forced to allocate, we 
could keep track of those so that if these new items that are allocated 
get removed, then we can handle it.

-- Jeff J.

More information about the Newlib mailing list