From f8ca22295e994338259174e5c66b6b23e5d9b55f Mon Sep 17 00:00:00 2001 From: Jeff Davis Date: Tue, 28 Mar 2023 07:55:45 -0700 Subject: [PATCH] initdb: replace check_icu_locale() with default_icu_locale(). The extra checks done in check_icu_locale() are not necessary. An existing comment already pointed out that the checks would be done during post-bootstrap initialization, when the locale is opened by the backend. This was a mistake in commit 27b62377b4. This commit creates a simpler function default_icu_locale() to just return the locale of the default collator. Discussion: https://postgr.es/m/04182066-7655-344a-b8b7-040b1b2490fb%40enterprisedb.com Reviewed-by: Peter Eisentraut --- src/bin/initdb/initdb.c | 56 ++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index bae97539fc..0acfd0fea2 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -2243,46 +2243,44 @@ check_icu_locale_encoding(int user_enc) } /* - * Check that ICU accepts the locale name; or if not specified, retrieve the - * default ICU locale. + * Determine default ICU locale by opening the default collator and reading + * its locale. + * + * NB: The default collator (opened using NULL) is different from the collator + * for the root locale (opened with "", "und", or "root"). The former depends + * on the environment (useful at initdb time) and the latter does not. */ -static void -check_icu_locale(void) +static char * +default_icu_locale(void) { #ifdef USE_ICU UCollator *collator; UErrorCode status; + const char *valid_locale; + char *default_locale; status = U_ZERO_ERROR; - collator = ucol_open(icu_locale, &status); + collator = ucol_open(NULL, &status); + if (U_FAILURE(status)) + pg_fatal("could not open collator for default locale: %s", + u_errorName(status)); + + status = U_ZERO_ERROR; + valid_locale = ucol_getLocaleByType(collator, ULOC_VALID_LOCALE, + &status); if (U_FAILURE(status)) { - if (icu_locale) - pg_fatal("could not open collator for locale \"%s\": %s", - icu_locale, u_errorName(status)); - else - pg_fatal("could not open collator for default locale: %s", - u_errorName(status)); + ucol_close(collator); + pg_fatal("could not determine default ICU locale"); } - /* if not specified, get locale from default collator */ - if (icu_locale == NULL) - { - const char *default_locale; - - status = U_ZERO_ERROR; - default_locale = ucol_getLocaleByType(collator, ULOC_VALID_LOCALE, - &status); - if (U_FAILURE(status)) - { - ucol_close(collator); - pg_fatal("could not determine default ICU locale"); - } - - icu_locale = pg_strdup(default_locale); - } + default_locale = pg_strdup(valid_locale); ucol_close(collator); + + return default_locale; +#else + pg_fatal("ICU is not supported in this build"); #endif } @@ -2339,7 +2337,9 @@ setlocales(void) if (locale_provider == COLLPROVIDER_ICU) { - check_icu_locale(); + /* acquire default locale from the environment, if not specified */ + if (icu_locale == NULL) + icu_locale = default_icu_locale(); /* * In supported builds, the ICU locale ID will be checked by the