Re: clearing the environment

Laurent Bercot <[email protected]> Sun, 3 Jun 2012 11:24:18 +0200
Newsgroups gmane.linux.lib.dietlibc
Message-ID <[email protected]>
> So, if I understood right, we shouldn't use clearenv() or equivalent and then
> setup a few variables, but rather build from scratch an array an pass it to
> execve. Is there some issue with clearenv, e.g. related with memory leaking?

 putenv() and friends were made to modify the environment within the same
process, without execve'ing. It requires some hidden memory allocations,
it's inefficient, and unless you're coding a shell, it's mostly useless.

 Since you can afford to execve(), it's much simpler to just setup a new
environment array and pass it to execve(). You *can* use putenv(), or
heap memory allocation, and just not care about memory leaks, since
execve() will clean up everything anyway; but you don't need to - and you
certainly don't need to if all you want is clear the environment.

 Btw, don't pass NULL to execve(). Pass char const *envp[1] = { NULL }.

-- 
 Laurent