Bug 1497948: Ensure that enterprise policies are converted into a correct UTF-16 string that can be parsed by JS_ParseJSON on macOS. r=mstange
authorStephen A Pohl <spohl.mozilla.bugs@gmail.com>
Sat, 13 Oct 2018 18:52:35 -0400
changeset 489464 1f542a1a1b9892b3b963724e3252c74585107b92
parent 489463 410f1ce46d58faaa90983efe0805b798df57c43e
child 489465 c86bcd5caf4d56057444464298454ee861f14eaf
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewersmstange
bugs1497948
milestone64.0a1
Bug 1497948: Ensure that enterprise policies are converted into a correct UTF-16 string that can be parsed by JS_ParseJSON on macOS. r=mstange
xpcom/base/nsMacPreferencesReader.mm
--- a/xpcom/base/nsMacPreferencesReader.mm
+++ b/xpcom/base/nsMacPreferencesReader.mm
@@ -9,21 +9,24 @@
 #include "nsISupportsPrimitives.h"
 
 NS_IMPL_ISUPPORTS(nsMacPreferencesReader, nsIMacPreferencesReader)
 
 using namespace mozilla;
 
 struct StringWriteFunc : public JSONWriteFunc
 {
-  nsCString& mCString;
-  explicit StringWriteFunc(nsCString& aCString) : mCString(aCString)
+  nsAString& mString;
+  explicit StringWriteFunc(nsAString& aStr) : mString(aStr)
   {
   }
-  void Write(const char* aStr) override { mCString.Append(aStr); }
+  void Write(const char* aStr) override
+  {
+    mString.Append(NS_ConvertUTF8toUTF16(aStr));
+  }
 };
 
 static void
 EvaluateDict(JSONWriter* aWriter, NSDictionary<NSString*, id>* aDict);
 
 static void
 EvaluateArray(JSONWriter* aWriter, NSArray* aArray)
 {
@@ -74,24 +77,23 @@ nsMacPreferencesReader::PoliciesEnabled(
                          boolForKey:policiesEnabledStr] == YES;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsMacPreferencesReader::ReadPreferences(JSContext* aCx,
                                         JS::MutableHandle<JS::Value> aResult)
 {
-  nsAutoCString jsonStr;
+  nsAutoString jsonStr;
   JSONWriter w(MakeUnique<StringWriteFunc>(jsonStr));
   w.Start();
   EvaluateDict(&w, [[NSUserDefaults standardUserDefaults]
                      dictionaryRepresentation]);
   w.End();
 
-  NS_ConvertUTF8toUTF16 jsString(nsDependentCString(jsonStr.get()));
-  auto json = static_cast<const char16_t*>(jsString.get());
+  auto json = static_cast<const char16_t*>(jsonStr.get());
 
   JS::RootedValue val(aCx);
   MOZ_ALWAYS_TRUE(JS_ParseJSON(aCx, json, jsonStr.Length(), &val));
 
   aResult.set(val);
   return NS_OK;
 }