On Sun, Feb 20, 2005 at 10:45:37PM +0100, Sam Lauber wrote:

> A good replacement for a broken putenv() is (assuming that `putenv'
> is defined as `rpl_putenv'):
> 
> #undef putenv
> int 
> rpl_putenv(s)
>   char *s;
> {
>   char *t;
>   strcpy(t, s);
>   return putenv(t);
> }

The strcpy() call has an undefined effect as it dereferences an
uninitialised pointer.  Perhaps you meant to put a call to xmalloc()
or strdup() in there.

If so, what about those callers who already carefully did this: -

   /* Assume no other thread will modify var or val; also assume
    * we already hold a mutex controlling access to putenv().
    */
   size_t len = strlen(var) + 1u + strlen(val);
   char *s = xmalloc(len + 1u);
   snprintf(s, len+1u, "%s=%s", var, val);
   rpl_putenv(s);

... because then you would have a memory leak.

James.



Reply via email to