From: Mike Hommey <glandium@debian.org>
Date: Fri, 27 Jun 2008 20:34:32 +0200
Subject: Don't use static strings when setting environment variables

There are situations where libxul.so gets dlclose()d, making static
strings unavailable and leading to strange segmentation faults.
---
 toolkit/xre/nsAppRunner.cpp |   40 +++++++++++++++++++++++++---------------
 1 files changed, 25 insertions(+), 15 deletions(-)

diff --git a/toolkit/xre/nsAppRunner.cpp b/toolkit/xre/nsAppRunner.cpp
index e6a3505..2d27266 100644
--- a/toolkit/xre/nsAppRunner.cpp
+++ b/toolkit/xre/nsAppRunner.cpp
@@ -292,6 +292,16 @@ static char **gRestartArgv;
 #include "nsGTKToolkit.h"
 #endif
 
+// Save literal putenv string to environment variable.
+static void
+SaveToEnv(const char *putenv)
+{
+  char *expr = strdup(putenv);
+  if (expr)
+    PR_SetEnv(expr);
+  // We intentionally leak |expr| here since it is required by PR_SetEnv.
+}
+
 // Save the given word to the specified environment variable.
 static void
 SaveWordToEnv(const char *name, const nsACString & word)
@@ -1605,7 +1615,7 @@ static nsresult LaunchChild(nsINativeAppSupport* aNative,
     gRestartArgv[gRestartArgc] = nsnull;
   }
 
-  PR_SetEnv("MOZ_LAUNCHED_CHILD=1");
+  SaveToEnv("MOZ_LAUNCHED_CHILD=1");
 
 #if defined(XP_MACOSX)
   SetupMacCommandLine(gRestartArgc, gRestartArgv);
@@ -1832,7 +1842,7 @@ ShowProfileManager(nsIToolkitProfileService* aProfileSvc,
   PRBool offline = PR_FALSE;
   aProfileSvc->GetStartOffline(&offline);
   if (offline) {
-    PR_SetEnv("XRE_START_OFFLINE=1");
+    SaveToEnv("XRE_START_OFFLINE=1");
   }
 
   return LaunchChild(aNative);
@@ -1844,7 +1854,7 @@ ImportProfiles(nsIToolkitProfileService* aPService,
 {
   nsresult rv;
 
-  PR_SetEnv("XRE_IMPORT_PROFILES=1");
+  SaveToEnv("XRE_IMPORT_PROFILES=1");
 
   // try to import old-style profiles
   { // scope XPCOM
@@ -2791,7 +2801,7 @@ XRE_main(int argc, char* argv[], const nsXREAppData* aAppData)
   }
 #endif
 
-  PR_SetEnv("MOZ_LAUNCHED_CHILD=");
+  SaveToEnv("MOZ_LAUNCHED_CHILD=");
 
   gRestartArgc = gArgc;
   gRestartArgv = (char**) malloc(sizeof(char*) * (gArgc + 1 + (override ? 2 : 0)));
@@ -2838,7 +2848,7 @@ XRE_main(int argc, char* argv[], const nsXREAppData* aAppData)
     PR_fprintf(PR_STDERR, "Error: argument -a requires an application name\n");
     return 1;
   } else if (ar == ARG_FOUND) {
-    PR_SetEnv("MOZ_NO_REMOTE=1");
+    SaveToEnv("MOZ_NO_REMOTE=1");
   }
 
   // Handle -help and -version command line arguments.
@@ -3258,14 +3268,14 @@ XRE_main(int argc, char* argv[], const nsXREAppData* aAppData)
 
           // clear out any environment variables which may have been set 
           // during the relaunch process now that we know we won't be relaunching.
-          PR_SetEnv("XRE_PROFILE_PATH=");
-          PR_SetEnv("XRE_PROFILE_LOCAL_PATH=");
-          PR_SetEnv("XRE_PROFILE_NAME=");
-          PR_SetEnv("XRE_START_OFFLINE=");
-          PR_SetEnv("XRE_IMPORT_PROFILES=");
-          PR_SetEnv("NO_EM_RESTART=");
-          PR_SetEnv("XUL_APP_FILE=");
-          PR_SetEnv("XRE_BINARY_PATH=");
+          SaveToEnv("XRE_PROFILE_PATH=");
+          SaveToEnv("XRE_PROFILE_LOCAL_PATH=");
+          SaveToEnv("XRE_PROFILE_NAME=");
+          SaveToEnv("XRE_START_OFFLINE=");
+          SaveToEnv("XRE_IMPORT_PROFILES=");
+          SaveToEnv("NO_EM_RESTART=");
+          SaveToEnv("XUL_APP_FILE=");
+          SaveToEnv("XRE_BINARY_PATH=");
 
           if (!shuttingDown) {
 #ifdef XP_MACOSX
@@ -3376,10 +3386,10 @@ XRE_main(int argc, char* argv[], const nsXREAppData* aAppData)
       else {
         char* noEMRestart = PR_GetEnv("NO_EM_RESTART");
         if (noEMRestart && *noEMRestart) {
-          PR_SetEnv("NO_EM_RESTART=1");
+          SaveToEnv("NO_EM_RESTART=1");
         }
         else {
-          PR_SetEnv("NO_EM_RESTART=0");
+          SaveToEnv("NO_EM_RESTART=0");
         }
       }
 
