fix putenv memory leak

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8037 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Alfredo Braunstein 2003-11-05 08:22:32 +00:00
parent db78dbfa04
commit dffbeaf18d
3 changed files with 29 additions and 4 deletions

View File

@ -1,3 +1,10 @@
2003-11-05 João Luis M. Assirati <assirati@fma.if.usp.br>
* putenv.C: allocate the string before putting it into the
environment.
* lyxlib.h: adjust.
2003-11-03 Lars Gullik Bjønnes <larsbj@gullik.net> 2003-11-03 Lars Gullik Bjønnes <larsbj@gullik.net>
* tempname.C (tempName): use scoped_array for exception safety * tempname.C (tempName): use scoped_array for exception safety

View File

@ -40,8 +40,8 @@ int kill(int pid, int sig);
void abort(); void abort();
/// create the given directory with the given mode /// create the given directory with the given mode
int mkdir(std::string const & pathname, unsigned long int mode); int mkdir(std::string const & pathname, unsigned long int mode);
/// put a C std::string into the environment /// put variable=value as a C std::string into the environment
int putenv(char const * str); bool putenv(std::string const & varname, std::string const & value);
/// unlink the given file /// unlink the given file
int unlink(std::string const & file); int unlink(std::string const & file);
/// remove the given directory /// remove the given directory

View File

@ -4,6 +4,7 @@
* Licence details can be found in the file COPYING. * Licence details can be found in the file COPYING.
* *
* \author Lars Gullik Bjønnes * \author Lars Gullik Bjønnes
* \author João Luis M. Assirati
* *
* Full author contact details are available in file CREDITS. * Full author contact details are available in file CREDITS.
*/ */
@ -13,8 +14,25 @@
#include "lyxlib.h" #include "lyxlib.h"
#include <cstdlib> #include <cstdlib>
#include <string>
#include <map>
int lyx::support::putenv(char const * str) using std::string;
using std::map;
bool lyx::support::putenv(string const & varname, string const & value)
{ {
return ::putenv(const_cast<char*>(str)); static map<string, char *> varmap;
string str = varname + '=' + value;
char * newptr = new char[str.size() + 1];
newptr[str.copy(newptr, string::npos)] = '\0';
bool status = (::putenv(newptr) == 0);
char * oldptr = varmap[varname];
if (oldptr)
delete oldptr;
varmap[varname] = newptr;
return status;
} }