]> git.lyx.org Git - lyx.git/blobdiff - src/support/environment.cpp
Fix bugs #6078 and #9364
[lyx.git] / src / support / environment.cpp
index dfdc2d4867c804d63ff1b4fa2dc65b9bc680ac18..ceaca60f547d86aebc21b295a424b4f08395fb13 100644 (file)
@@ -71,6 +71,11 @@ bool setEnv(string const & name, string const & value)
 #if defined (HAVE_SETENV)
        return ::setenv(name.c_str(), encoded.c_str(), 1) == 0;
 #elif defined (HAVE_PUTENV)
+       // According to http://pubs.opengroup.org/onlinepubs/9699919799/functions/putenv.html
+       // the argument of putenv() needs to be static, because changing its
+       // value will change the environment. Therefore we need a different static
+       // storage for each variable.
+       // FIXME THREAD
        static map<string, string> varmap;
        varmap[name] = name + '=' + encoded;
        return ::putenv(const_cast<char*>(varmap[name].c_str())) == 0;
@@ -129,10 +134,12 @@ bool unsetEnv(string const & name)
 {
 #if defined(HAVE_UNSETENV)
        // FIXME: does it leak?
-       return unsetenv(name.c_str()) == 0;
+       return ::unsetenv(name.c_str()) == 0;
 #elif defined(HAVE_PUTENV)
        // This is OK with MSVC and MinGW at least.
-       return putenv((name + "=").c_str()) == 0;
+       // The argument of putenv() does not need to be a static variable in this
+       // case, since the variable is removed from the environment.
+       return ::putenv(const_cast<char*>((name + "=").c_str())) == 0;
 #else
 #error No environment-unsetting function has been defined.
 #endif