From 183a60a7bc560091ab03e83e09c0d635c09af615 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Thu, 29 May 2025 17:10:47 -0400 Subject: [PATCH] ICULocaleBackend: Validate that the created locale really exists. Locale::createCanonical will happily accept all kinds of strings that don't actually indicate valid locales, but down the line ICU will behave strangely or even crash with these in certain methods. So, use getISO3Language(), which will return nothing if the language is unknown to ICU, to validate that the locale exists, or bail if it doesn't. Fixes #19576. --- src/system/libroot/add-ons/icu/ICULocaleBackend.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/system/libroot/add-ons/icu/ICULocaleBackend.cpp b/src/system/libroot/add-ons/icu/ICULocaleBackend.cpp index 7c9657a496..44986a0c83 100644 --- a/src/system/libroot/add-ons/icu/ICULocaleBackend.cpp +++ b/src/system/libroot/add-ons/icu/ICULocaleBackend.cpp @@ -88,6 +88,13 @@ ICULocaleBackend::SetLocale(int category, const char* posixLocaleName) return _SetPosixLocale(category); Locale locale = Locale::createCanonical(posixLocaleName); + + // Locale::create doesn't check validity, so make sure the locale + // has a real ISO-3166 language code to validate that it actually exists. + const char* iso3166 = locale.getISO3Language(); + if (iso3166 == NULL || iso3166[0] == '\0') + return NULL; + switch (category) { case LC_ALL: if (fCollateData.SetTo(locale, posixLocaleName) != B_OK