Bug 1448786 - Avoid UTF8ToUnicodeBuffer in OSPreferences_win.cpp. draft
authorHenri Sivonen <hsivonen@hsivonen.fi>
Mon, 26 Mar 2018 15:42:27 +0300
changeset 773652 a4da4d10be08a745347d7a08ddf43d469883a122
parent 773651 f227c286cf735e1c6b4ba6523f62e19798517a54
child 773653 8e864a8f82a690c5a09cf9371deb8b7d7ecf7c09
push id104266
push userbmo:hsivonen@hsivonen.fi
push dateWed, 28 Mar 2018 07:33:03 +0000
bugs1448786
milestone61.0a1
Bug 1448786 - Avoid UTF8ToUnicodeBuffer in OSPreferences_win.cpp. MozReview-Commit-ID: 7a11ogGXo8E
intl/locale/windows/OSPreferences_win.cpp
--- a/intl/locale/windows/OSPreferences_win.cpp
+++ b/intl/locale/windows/OSPreferences_win.cpp
@@ -128,55 +128,58 @@ ToTimeLCType(OSPreferences::DateTimeForm
  * for combined date/time string, since Windows API does not provide an
  * option for this.
  */
 bool
 OSPreferences::ReadDateTimePattern(DateTimeFormatStyle aDateStyle,
                                    DateTimeFormatStyle aTimeStyle,
                                    const nsACString& aLocale, nsAString& aRetVal)
 {
-  WCHAR localeName[LOCALE_NAME_MAX_LENGTH];
-  UTF8ToUnicodeBuffer(aLocale, (char16_t*)localeName);
+  nsAutoString localeName;
+  CopyASCIItoUTF16(aLocale, localeName);
+
+  char16_t* localeNamePtr = localeName;
+  WCHAR* localeNameWcharPtr = reinterpret_cast<WCHAR*>(localeNamePtr);
 
   bool isDate = aDateStyle != DateTimeFormatStyle::None &&
                 aDateStyle != DateTimeFormatStyle::Invalid;
   bool isTime = aTimeStyle != DateTimeFormatStyle::None &&
                 aTimeStyle != DateTimeFormatStyle::Invalid;
 
   // If both date and time are wanted, we'll initially read them into a
   // local string, and then insert them into the overall date+time pattern;
   // but if only one is needed we'll work directly with the return value.
   // Set 'str' to point to the string we will use to retrieve patterns
   // from Windows.
   nsAutoString tmpStr;
   nsAString* str;
   if (isDate && isTime) {
-    if (!GetDateTimeConnectorPattern(NS_ConvertUTF16toUTF8(localeName), aRetVal)) {
+    if (!GetDateTimeConnectorPattern(aLocale, aRetVal)) {
       NS_WARNING("failed to get date/time connector");
       aRetVal.AssignLiteral(u"{1} {0}");
     }
     str = &tmpStr;
   } else if (isDate || isTime) {
     str = &aRetVal;
   } else {
     aRetVal.Truncate(0);
     return true;
   }
 
   if (isDate) {
     LCTYPE lcType = ToDateLCType(aDateStyle);
-    size_t len = GetLocaleInfoEx(localeName, lcType, nullptr, 0);
+    size_t len = GetLocaleInfoEx(reinterpret_cast<WCHAR*>(localeName.get()), lcType, nullptr, 0);
     if (len == 0) {
       return false;
     }
 
     // We're doing it to ensure the terminator will fit when Windows writes the data
     // to its output buffer. See bug 1358159 for details.
     str->SetLength(len);
-    GetLocaleInfoEx(localeName, lcType, (WCHAR*)str->BeginWriting(), len);
+    GetLocaleInfoEx(reinterpret_cast<WCHAR*>(localeName.get()), lcType, (WCHAR*)str->BeginWriting(), len);
     str->SetLength(len - 1); // -1 because len counts the null terminator
 
     // Windows uses "ddd" and "dddd" for abbreviated and full day names respectively,
     //   https://msdn.microsoft.com/en-us/library/windows/desktop/dd317787(v=vs.85).aspx
     // but in a CLDR/ICU-style pattern these should be "EEE" and "EEEE".
     //   http://userguide.icu-project.org/formatparse/datetime
     // So we fix that up here.
     nsAString::const_iterator start, pos, end;