From dffbeaf18dbc69a99d9f94f5c7c745f032fd5f1e Mon Sep 17 00:00:00 2001 From: Alfredo Braunstein Date: Wed, 5 Nov 2003 08:22:32 +0000 Subject: [PATCH] fix putenv memory leak git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8037 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/support/ChangeLog | 7 +++++++ src/support/lyxlib.h | 4 ++-- src/support/putenv.C | 22 ++++++++++++++++++++-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/support/ChangeLog b/src/support/ChangeLog index df4afcd0f1..0d342db2e9 100644 --- a/src/support/ChangeLog +++ b/src/support/ChangeLog @@ -1,3 +1,10 @@ +2003-11-05 João Luis M. Assirati + + * putenv.C: allocate the string before putting it into the + environment. + + * lyxlib.h: adjust. + 2003-11-03 Lars Gullik Bjønnes * tempname.C (tempName): use scoped_array for exception safety diff --git a/src/support/lyxlib.h b/src/support/lyxlib.h index e76e14c956..0695269c90 100644 --- a/src/support/lyxlib.h +++ b/src/support/lyxlib.h @@ -40,8 +40,8 @@ int kill(int pid, int sig); void abort(); /// create the given directory with the given mode int mkdir(std::string const & pathname, unsigned long int mode); -/// put a C std::string into the environment -int putenv(char const * str); +/// put variable=value as a C std::string into the environment +bool putenv(std::string const & varname, std::string const & value); /// unlink the given file int unlink(std::string const & file); /// remove the given directory diff --git a/src/support/putenv.C b/src/support/putenv.C index af6e8ad56a..83e4e739df 100644 --- a/src/support/putenv.C +++ b/src/support/putenv.C @@ -4,6 +4,7 @@ * Licence details can be found in the file COPYING. * * \author Lars Gullik Bjønnes + * \author João Luis M. Assirati * * Full author contact details are available in file CREDITS. */ @@ -13,8 +14,25 @@ #include "lyxlib.h" #include +#include +#include -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(str)); + static map 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; }