Bug 1271574 - Purposefully leak the XUL_APP_FILE string passed to putenv. r=bsmedberg
authorMike Hommey <mh+mozilla@glandium.org>
Thu, 02 Jun 2016 08:44:16 +0900
changeset 301141 fb084575df12b88ac718f023454f3f5d500ab388
parent 301140 302da31353a0a8ca6d65e01e869d98a589d34b62
child 301142 e0f9b54febc83597415fe8d056ef7b4e8939f03d
push id78237
push usermh@glandium.org
push dateWed, 08 Jun 2016 23:33:23 +0000
treeherdermozilla-inbound@fb084575df12 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs1271574, 552864
milestone50.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1271574 - Purposefully leak the XUL_APP_FILE string passed to putenv. r=bsmedberg Before bug 552864, the string was created with PR_smprintf, and PR_SetEnv'ed (which, under the hood, just calls putenv). PR_smprintf was allocating the string on the heap. Now, it's allocated on the stack, and still putenv'ed. putenv kind of takes ownership of the strings it's being passed, so stack allocated strings are dangerous to use. It looks like we've been fairly lucky that it worked, presumably because compilers would keep the stack frame with the variable, but that's not guaranteed to happen, and in some case, doesn't. So we strdup the string and purposefully leak it instead, which matches what happened before bug 552864, and is the only "sane" way to use putenv.
b2g/app/B2GLoader.cpp
b2g/app/nsBrowserApp.cpp
browser/app/nsBrowserApp.cpp
--- a/b2g/app/B2GLoader.cpp
+++ b/b2g/app/B2GLoader.cpp
@@ -133,17 +133,17 @@ GetAppIni(int argc, const char *argv[])
       return nullptr;
     }
 
     rv = XRE_GetFileFromPath(argv[2], getter_AddRefs(appini));
     NS_ENSURE_SUCCESS(rv, nullptr);
 
     char appEnv[MAXPATHLEN];
     snprintf(appEnv, MAXPATHLEN, "XUL_APP_FILE=%s", argv[2]);
-    if (putenv(appEnv)) {
+    if (putenv(strdup(appEnv))) {
       return nullptr;
     }
   }
 
   return appini.forget();
 }
 
 static bool
--- a/b2g/app/nsBrowserApp.cpp
+++ b/b2g/app/nsBrowserApp.cpp
@@ -132,17 +132,17 @@ static int do_main(int argc, char* argv[
     rv = XRE_GetFileFromPath(argv[2], getter_AddRefs(appini));
     if (NS_FAILED(rv)) {
       Output("application.ini path not recognized: '%s'", argv[2]);
       return 255;
     }
 
     char appEnv[MAXPATHLEN];
     snprintf(appEnv, MAXPATHLEN, "XUL_APP_FILE=%s", argv[2]);
-    if (putenv(appEnv)) {
+    if (putenv(strdup(appEnv))) {
       Output("Couldn't set %s.\n", appEnv);
       return 255;
     }
     argv[2] = argv[0];
     argv += 2;
     argc -= 2;
   }
 
--- a/browser/app/nsBrowserApp.cpp
+++ b/browser/app/nsBrowserApp.cpp
@@ -178,17 +178,17 @@ static int do_main(int argc, char* argv[
     rv = XRE_GetFileFromPath(argv[2], getter_AddRefs(appini));
     if (NS_FAILED(rv)) {
       Output("application.ini path not recognized: '%s'", argv[2]);
       return 255;
     }
 
     char appEnv[MAXPATHLEN];
     snprintf(appEnv, MAXPATHLEN, "XUL_APP_FILE=%s", argv[2]);
-    if (putenv(appEnv)) {
+    if (putenv(strdup(appEnv))) {
       Output("Couldn't set %s.\n", appEnv);
       return 255;
     }
     argv[2] = argv[0];
     argv += 2;
     argc -= 2;
   } else if (argc > 1 && IsArg(argv[1], "xpcshell")) {
     for (int i = 1; i < argc; i++) {