Bug 1271574 - Purposefully leak the XUL_APP_FILE string passed to putenv. r=bsmedberg, a=sylvestre
authorMike Hommey <mh+mozilla@glandium.org>
Thu, 02 Jun 2016 08:44:16 +0900
changeset 339644 a7e4bbe5ec84cc2bb4e45ecd62de26292aa3adf6
parent 339643 d7a911ef57ad6b836046532794014cc06d24c6f2
child 339645 395d166833b05fa1db781843da89f28b8b122b54
push id6249
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 13:59:36 +0000
treeherdermozilla-beta@bad9d4f5bf7e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg, sylvestre
bugs1271574, 552864
milestone49.0a2
Bug 1271574 - Purposefully leak the XUL_APP_FILE string passed to putenv. r=bsmedberg, a=sylvestre 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
@@ -166,17 +166,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++) {