Bug 1341238 - Don't hang on to pointer from temporary NS_LossyConvertUTF16toASCII. r=padenot draft
authorMatthew Gregan <kinetik@flim.org>
Tue, 14 Mar 2017 17:36:12 +0100
changeset 498422 94bcfb9914152e43e8bcc31210d7175af8bcaf53
parent 498421 c97bc807031f522e94d41b203e2b5f376e7c240e
child 498423 c510ba8512ba805c434c51010b69077902f17de1
child 498424 06898437a219db7b5145784569a4ed8777b9fec7
push id49176
push userpaul@paul.cx
push dateTue, 14 Mar 2017 17:54:33 +0000
reviewerspadenot
bugs1341238
milestone55.0a1
Bug 1341238 - Don't hang on to pointer from temporary NS_LossyConvertUTF16toASCII. r=padenot MozReview-Commit-ID: J4qA1PNPyma
dom/media/CubebUtils.cpp
--- a/dom/media/CubebUtils.cpp
+++ b/dom/media/CubebUtils.cpp
@@ -170,21 +170,20 @@ void PrefChanged(const char* aPref, void
       cubeb_set_log_callback(CUBEB_LOG_DISABLED, nullptr);
       cubebLog->SetLevel(LogLevel::Disabled);
     }
   } else if (strcmp(aPref, PREF_CUBEB_BACKEND) == 0) {
     nsAdoptingString value = Preferences::GetString(aPref);
     if (value.IsEmpty()) {
       sCubebBackendName = nullptr;
     } else {
-      /* cubeb expects a c-string. */
-      const char* ascii = NS_LossyConvertUTF16toASCII(value).get();
-      sCubebBackendName = new char[value.Length() + 1];
-      PodCopy(sCubebBackendName.get(), ascii, value.Length());
-      sCubebBackendName[value.Length()] = 0;
+      NS_LossyConvertUTF16toASCII ascii(value);
+      sCubebBackendName = new char[ascii.Length() + 1];
+      PodCopy(sCubebBackendName.get(), ascii.get(), ascii.Length());
+      sCubebBackendName[ascii.Length()] = 0;
     }
   }
 }
 
 bool GetFirstStream()
 {
   static bool sFirstStream = true;
 
@@ -313,21 +312,20 @@ void InitBrandName()
                                            getter_AddRefs(brandBundle));
     if (NS_SUCCEEDED(rv)) {
       rv = brandBundle->GetStringFromName(u"brandShortName",
                                           getter_Copies(brandName));
       NS_WARNING_ASSERTION(
         NS_SUCCEEDED(rv), "Could not get the program name for a cubeb stream.");
     }
   }
-  /* cubeb expects a c-string. */
-  const char* ascii = NS_LossyConvertUTF16toASCII(brandName).get();
-  sBrandName = new char[brandName.Length() + 1];
-  PodCopy(sBrandName.get(), ascii, brandName.Length());
-  sBrandName[brandName.Length()] = 0;
+  NS_LossyConvertUTF16toASCII ascii(brandName);
+  sBrandName = new char[ascii.Length() + 1];
+  PodCopy(sBrandName.get(), ascii.get(), ascii.Length());
+  sBrandName[ascii.Length()] = 0;
 }
 
 cubeb* GetCubebContextUnlocked()
 {
   sMutex.AssertCurrentThreadOwns();
   if (sCubebState != CubebState::Uninitialized) {
     // If we have already passed the initialization point (below), just return
     // the current context, which may be null (e.g., after error or shutdown.)