Bug 1382840 - Making the nsRFPService::UpdatePref() to copy the string which been passed to PR_SetEnv(). r?ehsan draft
authorTim Huang <tihuang@mozilla.com>
Fri, 21 Jul 2017 11:49:21 +0800
changeset 614081 beac2137945e370919f9dae359137531dc988e2f
parent 611714 eb1d92b2b6a4161492561250f51bae5bafeda68a
child 638772 42118c5365c34c3327dbb6b46c4015fc916c519e
push id69909
push userbmo:tihuang@mozilla.com
push dateMon, 24 Jul 2017 02:16:52 +0000
reviewersehsan
bugs1382840
milestone56.0a1
Bug 1382840 - Making the nsRFPService::UpdatePref() to copy the string which been passed to PR_SetEnv(). r?ehsan The PR_SetEnv() needs the passing string been intentionally leaked. However, nsRFPService::UpdatePref() uses PR_SetEnv() without leaking the input string. This patch is going to fix this by using ToNewCString(). MozReview-Commit-ID: 97QQUk2sjM9
toolkit/components/resistfingerprinting/nsRFPService.cpp
--- a/toolkit/components/resistfingerprinting/nsRFPService.cpp
+++ b/toolkit/components/resistfingerprinting/nsRFPService.cpp
@@ -143,17 +143,29 @@ nsRFPService::UpdatePref()
     PR_SetEnv("TZ=UTC");
     JS::SetTimeResolutionUsec(kResolutionUSec);
   } else if (sInitialized) {
     JS::SetTimeResolutionUsec(0);
     // We will not touch the TZ value if 'privacy.resistFingerprinting' is false during
     // the time of initialization.
     if (!mInitialTZValue.IsEmpty()) {
       nsAutoCString tzValue = NS_LITERAL_CSTRING("TZ=") + mInitialTZValue;
-      PR_SetEnv(tzValue.get());
+      static char* tz = nullptr;
+
+      // If the tz has been set before, we free it first since it will be allocated
+      // a new value later.
+      if (tz) {
+        free(tz);
+      }
+      // PR_SetEnv() needs the input string been leaked intentionally, so
+      // we copy it here.
+      tz = ToNewCString(tzValue);
+      if (tz) {
+        PR_SetEnv(tz);
+      }
     } else {
 #if defined(XP_LINUX) || defined (XP_MACOSX)
       // For POSIX like system, we reset the TZ to the /etc/localtime, which is the
       // system timezone.
       PR_SetEnv("TZ=:/etc/localtime");
 #else
       // For Windows, we reset the TZ to an empty string. This will make Windows to use
       // its system timezone.