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:57:03 +0000
changeset 499654 2637f6d8110045b500da1148afc3b8eeba3595eb
parent 499653 454c997fec10a7c7edda7fab0cd1a994b187f185
child 549404 f65f763c368937091bc5cc220c3adcf5cad5b2b1
push id49464
push userbmo:kinetik@flim.org
push dateThu, 16 Mar 2017 01:49:25 +0000
reviewerspadenot
bugs1341238
milestone55.0a1
Bug 1341238 - Don't hang on to pointer from temporary NS_LossyConvertUTF16toASCII. r=padenot
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.)