Skip to content

Commit 3580397

Browse files
committed
Always set the six locale category environment variables in main().
Typical server invocations already achieved that. Invalid locale settings in the initial postmaster environment interfered, as could malloc() failure. Setting "LC_MESSAGES=pt_BR.utf8 LC_ALL=invalid" in the postmaster environment will now choose C-locale messages, not Brazilian Portuguese messages. Most localized programs, including all PostgreSQL frontend executables, do likewise. Users are unlikely to observe changes involving locale categories other than LC_MESSAGES. CheckMyDatabase() ensures that we successfully set LC_COLLATE and LC_CTYPE; main() sets the remaining three categories to locale "C", which almost cannot fail. Back-patch to 9.0 (all supported versions).
1 parent aae12e4 commit 3580397

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

src/backend/main/main.c

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const char *progname;
5050

5151

5252
static void startup_hacks(const char *progname);
53+
static void init_locale(int category, const char *locale);
5354
static void help(const char *progname);
5455
static void check_root(const char *progname);
5556
static char *get_current_username(const char *progname);
@@ -111,31 +112,31 @@ main(int argc, char *argv[])
111112
char *env_locale;
112113

113114
if ((env_locale = getenv("LC_COLLATE")) != NULL)
114-
pg_perm_setlocale(LC_COLLATE, env_locale);
115+
init_locale(LC_COLLATE, env_locale);
115116
else
116-
pg_perm_setlocale(LC_COLLATE, "");
117+
init_locale(LC_COLLATE, "");
117118

118119
if ((env_locale = getenv("LC_CTYPE")) != NULL)
119-
pg_perm_setlocale(LC_CTYPE, env_locale);
120+
init_locale(LC_CTYPE, env_locale);
120121
else
121-
pg_perm_setlocale(LC_CTYPE, "");
122+
init_locale(LC_CTYPE, "");
122123
}
123124
#else
124-
pg_perm_setlocale(LC_COLLATE, "");
125-
pg_perm_setlocale(LC_CTYPE, "");
125+
init_locale(LC_COLLATE, "");
126+
init_locale(LC_CTYPE, "");
126127
#endif
127128

128129
#ifdef LC_MESSAGES
129-
pg_perm_setlocale(LC_MESSAGES, "");
130+
init_locale(LC_MESSAGES, "");
130131
#endif
131132

132133
/*
133134
* We keep these set to "C" always, except transiently in pg_locale.c; see
134135
* that file for explanations.
135136
*/
136-
pg_perm_setlocale(LC_MONETARY, "C");
137-
pg_perm_setlocale(LC_NUMERIC, "C");
138-
pg_perm_setlocale(LC_TIME, "C");
137+
init_locale(LC_MONETARY, "C");
138+
init_locale(LC_NUMERIC, "C");
139+
init_locale(LC_TIME, "C");
139140

140141
/*
141142
* Now that we have absorbed as much as we wish to from the locale
@@ -268,6 +269,23 @@ startup_hacks(const char *progname)
268269
}
269270

270271

272+
/*
273+
* Make the initial permanent setting for a locale category. If that fails,
274+
* perhaps due to LC_foo=invalid in the environment, use locale C. If even
275+
* that fails, perhaps due to out-of-memory, the entire startup fails with it.
276+
* When this returns, we are guaranteed to have a setting for the given
277+
* category's environment variable.
278+
*/
279+
static void
280+
init_locale(int category, const char *locale)
281+
{
282+
if (pg_perm_setlocale(category, locale) == NULL &&
283+
pg_perm_setlocale(category, "C") == NULL)
284+
elog(FATAL, "could not adopt C locale");
285+
}
286+
287+
288+
271289
/*
272290
* Help display should match the options accepted by PostmasterMain()
273291
* and PostgresMain().

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy