[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Portability issue with `putenv'

From: Sam Lauber
Subject: Portability issue with `putenv'
Date: Sun, 20 Feb 2005 22:45:37 +0100

I have found a new function to add to the list of 
nonportable functions: `putenv'.  The DJGPP C Library 
refrence says under `putenv': 

 ... that most implementations will not allow you to free the string you pass 

Most of the time, `putenv' essently boils down to

 environ[free_position] = str

Which that note makes perfect sense: `environ' is declared 

extern char **environ;

and `putenv' is declared as

int putenv(const char *str)

Which is why they invented strcpy().  `free'ing the string 
would make the next access probably cause the `malloc' 
arena to be corrupted, causing a spectacular and gratitous 
segmentation fault.  If the string is not `strcpy'd into 
a safe location, than said call to `putenv' is 
nonportable.  A good replacement for a broken putenv() is 
(assuming that `putenv' is defined as `rpl_putenv'):

#undef putenv
  char *s;
  char *t;
  strcpy(t, s);
  return putenv(t);

Samuel Lauber

Web-based SMS services available at http://www.operamail.com.
From your mailbox to local or overseas cell phones.

Powered by Outblaze

reply via email to

[Prev in Thread] Current Thread [Next in Thread]