Bug 1271574 - Purposefully leak the XUL_APP_FILE string passed to putenv. r?bsmedberg draft
authorMike Hommey <mh+mozilla@glandium.org>
Thu, 02 Jun 2016 08:44:16 +0900
changeset 374200 7f779e2bf9c7cca2c412b30f857507c5db4f5644
parent 374199 2b74c4db83cc5eadf6cd41679572185c35d61bf4
child 522568 f5fe764413a8c3eb627c199ae16e3436ed277c09
push id19947
push userbmo:mh+mozilla@glandium.org
push dateWed, 01 Jun 2016 23:56:41 +0000
reviewersbsmedberg
bugs1271574, 552864
milestone49.0a1
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
@@ -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++) {