Changeset 206295 in webkit


Ignore:
Timestamp:
Sep 23, 2016 1:20:06 AM (8 years ago)
Author:
Carlos Garcia Campos
Message:

REGRESSION(r194387): Crash on github.com in IntlDateTimeFormat::resolvedOptions in C locale
https://bugs.webkit.org/show_bug.cgi?id=162139

Reviewed by Michael Catanzaro.

Source/JavaScriptCore:

The crash happens in unix ports because the resolved locale is empty when system locale is "C". IntlObject
considers any language tag with a size < 2 to be an invalid language, so "C" is not a valid language to resolve
the locale. We should ensure that WTF::platformUserPreferredLanguages() never returns invalid languages, but
that's not enough, because languages can be overriden from the public API, so we need to handle those cases and
throw exceptions instead of crashing.

  • runtime/IntlCollator.cpp:

(JSC::IntlCollator::initializeCollator): Throw a exception when we fail to resolve the locale.

  • runtime/IntlDateTimeFormat.cpp:

(JSC::IntlDateTimeFormat::initializeDateTimeFormat): Ditto.

  • runtime/IntlNumberFormat.cpp:

(JSC::IntlNumberFormat::initializeNumberFormat): Ditto.

Source/WebKit2:

Handle the case of "C" locale passed by the user using "en-US" as default to match what
WTF::platformUserPreferredLanguages() does.

  • UIProcess/API/gtk/WebKitWebContext.cpp:

(webkit_web_context_set_preferred_languages): Remove the call to languageDidChange() because
overrideUserPreferredLanguages() already calls it, so we were actually notifying the observers twice.

Source/WTF:

Handle the case of "C" or "POSIX" locale and use "en-US" as default. That matches what ICU and other ports do,
as well as what layout tests expect (some tests like js/intl-collator.html pass in the bots only because we use
en-US as system locale in those bots).

  • wtf/PlatformUserPreferredLanguagesUnix.cpp:

(WTF::platformLanguage):

Tools:

Add test cases to check the behavior when using the C locale and an invalid locale.

  • TestWebKitAPI/Tests/WebKit2Gtk/TestWebKitWebContext.cpp:

(testWebContextLanguages):

LayoutTests:

  • js/intl-invalid-locale-crash-expected.txt: Added.
  • js/intl-invalid-locale-crash.html: Added.
Location:
trunk
Files:
2 added
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r206282 r206295  
     12016-09-23  Carlos Garcia Campos  <cgarcia@igalia.com>
     2
     3        REGRESSION(r194387): Crash on github.com in IntlDateTimeFormat::resolvedOptions in C locale
     4        https://bugs.webkit.org/show_bug.cgi?id=162139
     5
     6        Reviewed by Michael Catanzaro.
     7
     8        * js/intl-invalid-locale-crash-expected.txt: Added.
     9        * js/intl-invalid-locale-crash.html: Added.
     10
    1112016-09-22  Megan Gardner  <megan_gardner@apple.com>
    212
  • trunk/Source/JavaScriptCore/ChangeLog

    r206289 r206295  
     12016-09-23  Carlos Garcia Campos  <cgarcia@igalia.com>
     2
     3        REGRESSION(r194387): Crash on github.com in IntlDateTimeFormat::resolvedOptions in C locale
     4        https://bugs.webkit.org/show_bug.cgi?id=162139
     5
     6        Reviewed by Michael Catanzaro.
     7
     8        The crash happens in unix ports because the resolved locale is empty when system locale is "C". IntlObject
     9        considers any language tag with a size < 2 to be an invalid language, so "C" is not a valid language to resolve
     10        the locale. We should ensure that WTF::platformUserPreferredLanguages() never returns invalid languages, but
     11        that's not enough, because languages can be overriden from the public API, so we need to handle those cases and
     12        throw exceptions instead of crashing.
     13
     14        * runtime/IntlCollator.cpp:
     15        (JSC::IntlCollator::initializeCollator): Throw a exception when we fail to resolve the locale.
     16        * runtime/IntlDateTimeFormat.cpp:
     17        (JSC::IntlDateTimeFormat::initializeDateTimeFormat): Ditto.
     18        * runtime/IntlNumberFormat.cpp:
     19        (JSC::IntlNumberFormat::initializeNumberFormat): Ditto.
     20
    1212016-09-22  Benjamin Poulain  <bpoulain@apple.com>
    222
  • trunk/Source/JavaScriptCore/runtime/IntlCollator.cpp

    r205569 r206295  
    258258    // 19. Set collator.[[locale]] to the value of r.[[locale]].
    259259    m_locale = result.get(ASCIILiteral("locale"));
     260    if (m_locale.isEmpty()) {
     261        throwTypeError(&state, scope, ASCIILiteral("failed to initialize Collator due to invalid locale"));
     262        return;
     263    }
    260264
    261265    // 20. Let k be 0.
  • trunk/Source/JavaScriptCore/runtime/IntlDateTimeFormat.cpp

    r205569 r206295  
    457457    // 13. Set dateTimeFormat.[[locale]] to the value of r.[[locale]].
    458458    m_locale = resolved.get(vm.propertyNames->locale.string());
     459    if (m_locale.isEmpty()) {
     460        throwTypeError(&exec, scope, ASCIILiteral("failed to initialize DateTimeFormat due to invalid locale"));
     461        return;
     462    }
    459463    // 14. Set dateTimeFormat.[[calendar]] to the value of r.[[ca]].
    460464    m_calendar = resolved.get(ASCIILiteral("ca"));
  • trunk/Source/JavaScriptCore/runtime/IntlNumberFormat.cpp

    r205569 r206295  
    199199    // 13. Set numberFormat.[[locale]] to the value of r.[[locale]].
    200200    m_locale = result.get(ASCIILiteral("locale"));
     201    if (m_locale.isEmpty()) {
     202        throwTypeError(&state, scope, ASCIILiteral("failed to initialize NumberFormat due to invalid locale"));
     203        return;
     204    }
    201205
    202206    // 14. Set numberFormat.[[numberingSystem]] to the value of r.[[nu]].
  • trunk/Source/WTF/ChangeLog

    r206274 r206295  
     12016-09-23  Carlos Garcia Campos  <cgarcia@igalia.com>
     2
     3        REGRESSION(r194387): Crash on github.com in IntlDateTimeFormat::resolvedOptions in C locale
     4        https://bugs.webkit.org/show_bug.cgi?id=162139
     5
     6        Reviewed by Michael Catanzaro.
     7
     8        Handle the case of "C" or "POSIX" locale and use "en-US" as default. That matches what ICU and other ports do,
     9        as well as what layout tests expect (some tests like js/intl-collator.html pass in the bots only because we use
     10        en-US as system locale in those bots).
     11
     12        * wtf/PlatformUserPreferredLanguagesUnix.cpp:
     13        (WTF::platformLanguage):
     14
    1152016-09-22  Filip Pizlo  <fpizlo@apple.com>
    216
  • trunk/Source/WTF/wtf/PlatformUserPreferredLanguagesUnix.cpp

    r201038 r206295  
    3636{
    3737    String localeDefault(setlocale(LC_CTYPE, nullptr));
    38     if (localeDefault.isEmpty())
    39         return String("c");
     38    if (localeDefault.isEmpty() || equalIgnoringASCIICase(localeDefault, "C") || equalIgnoringASCIICase(localeDefault, "POSIX"))
     39        return ASCIILiteral("en-us");
    4040
    4141    String normalizedDefault = localeDefault.convertToASCIILowercase();
  • trunk/Source/WebKit2/ChangeLog

    r206294 r206295  
     12016-09-23  Carlos Garcia Campos  <cgarcia@igalia.com>
     2
     3        REGRESSION(r194387): Crash on github.com in IntlDateTimeFormat::resolvedOptions in C locale
     4        https://bugs.webkit.org/show_bug.cgi?id=162139
     5
     6        Reviewed by Michael Catanzaro.
     7
     8        Handle the case of "C" locale passed by the user using "en-US" as default to match what
     9        WTF::platformUserPreferredLanguages() does.
     10
     11        * UIProcess/API/gtk/WebKitWebContext.cpp:
     12        (webkit_web_context_set_preferred_languages): Remove the call to languageDidChange() because
     13        overrideUserPreferredLanguages() already calls it, so we were actually notifying the observers twice.
     14
    1152016-09-23  Carlos Garcia Campos  <cgarcia@igalia.com>
    216
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebContext.cpp

    r201267 r206295  
    973973
    974974    Vector<String> languages;
    975     for (size_t i = 0; languageList[i]; ++i)
    976         languages.append(String::fromUTF8(languageList[i]).convertToASCIILowercase().replace("_", "-"));
    977 
     975    for (size_t i = 0; languageList[i]; ++i) {
     976        // Do not propagate the C locale to WebCore.
     977        if (!g_ascii_strcasecmp(languageList[i], "C") || !g_ascii_strcasecmp(languageList[i], "POSIX"))
     978            languages.append(ASCIILiteral("en-us"));
     979        else
     980            languages.append(String::fromUTF8(languageList[i]).convertToASCIILowercase().replace("_", "-"));
     981    }
    978982    WebCore::overrideUserPreferredLanguages(languages);
    979     WebCore::languageDidChange();
    980983}
    981984
  • trunk/Tools/ChangeLog

    r206290 r206295  
     12016-09-23  Carlos Garcia Campos  <cgarcia@igalia.com>
     2
     3        REGRESSION(r194387): Crash on github.com in IntlDateTimeFormat::resolvedOptions in C locale
     4        https://bugs.webkit.org/show_bug.cgi?id=162139
     5
     6        Reviewed by Michael Catanzaro.
     7
     8        Add test cases to check the behavior when using the C locale and an invalid locale.
     9
     10        * TestWebKitAPI/Tests/WebKit2Gtk/TestWebKitWebContext.cpp:
     11        (testWebContextLanguages):
     12
    1132016-09-22  Hunseop Jeong  <hs85.jeong@samsung.com>
    214
  • trunk/Tools/TestWebKitAPI/Tests/WebKit2Gtk/TestWebKitWebContext.cpp

    r197563 r206295  
    466466static void testWebContextLanguages(WebViewTest* test, gconstpointer)
    467467{
    468     static const char* expectedDefaultLanguage = "en";
     468    static const char* expectedDefaultLanguage = "en-us";
    469469    test->loadURI(kServer->getURIForPath("/").data());
    470470    test->waitUntilLoadFinished();
     
    488488    g_assert_cmpuint(mainResourceDataSize, ==, strlen(expectedLanguages));
    489489    g_assert(!strncmp(mainResourceData, expectedLanguages, mainResourceDataSize));
     490
     491    // When using the C locale, en-US should be used as default.
     492    const char* cLanguage[] = { "C", nullptr };
     493    webkit_web_context_set_preferred_languages(test->m_webContext.get(), cLanguage);
     494    GUniqueOutPtr<GError> error;
     495    WebKitJavascriptResult* javascriptResult = test->runJavaScriptAndWaitUntilFinished("Intl.DateTimeFormat().resolvedOptions().locale", &error.outPtr());
     496    g_assert(javascriptResult);
     497    g_assert(!error);
     498    GUniquePtr<char> locale(WebViewTest::javascriptResultToCString(javascriptResult));
     499    g_assert_cmpstr(locale.get(), ==, "en-US");
     500
     501    // When using the POSIX locale, en-US should be used as default.
     502    const char* posixLanguage[] = { "POSIX", nullptr };
     503    webkit_web_context_set_preferred_languages(test->m_webContext.get(), posixLanguage);
     504    javascriptResult = test->runJavaScriptAndWaitUntilFinished("Intl.DateTimeFormat().resolvedOptions().locale", &error.outPtr());
     505    g_assert(javascriptResult);
     506    g_assert(!error);
     507    locale.reset(WebViewTest::javascriptResultToCString(javascriptResult));
     508    g_assert_cmpstr(locale.get(), ==, "en-US");
     509
     510    // An invalid locale should throw an exception.
     511    const char* invalidLanguage[] = { "A", nullptr };
     512    webkit_web_context_set_preferred_languages(test->m_webContext.get(), invalidLanguage);
     513    javascriptResult = test->runJavaScriptAndWaitUntilFinished("Intl.DateTimeFormat().resolvedOptions().locale", &error.outPtr());
     514    g_assert(!javascriptResult);
     515    g_assert_error(error.get(), WEBKIT_JAVASCRIPT_ERROR, WEBKIT_JAVASCRIPT_ERROR_SCRIPT_FAILED);
    490516}
    491517
Note: See TracChangeset for help on using the changeset viewer.