Skip to content

Commit 603eb79

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 e045052 commit 603eb79

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
@@ -51,6 +51,7 @@ const char *progname;
5151

5252

5353
static void startup_hacks(const char *progname);
54+
static void init_locale(int category, const char *locale);
5455
static void help(const char *progname);
5556
static void check_root(const char *progname);
5657
static char *get_current_username(const char *progname);
@@ -124,31 +125,31 @@ main(int argc, char *argv[])
124125
char *env_locale;
125126

126127
if ((env_locale = getenv("LC_COLLATE")) != NULL)
127-
pg_perm_setlocale(LC_COLLATE, env_locale);
128+
init_locale(LC_COLLATE, env_locale);
128129
else
129-
pg_perm_setlocale(LC_COLLATE, "");
130+
init_locale(LC_COLLATE, "");
130131

131132
if ((env_locale = getenv("LC_CTYPE")) != NULL)
132-
pg_perm_setlocale(LC_CTYPE, env_locale);
133+
init_locale(LC_CTYPE, env_locale);
133134
else
134-
pg_perm_setlocale(LC_CTYPE, "");
135+
init_locale(LC_CTYPE, "");
135136
}
136137
#else
137-
pg_perm_setlocale(LC_COLLATE, "");
138-
pg_perm_setlocale(LC_CTYPE, "");
138+
init_locale(LC_COLLATE, "");
139+
init_locale(LC_CTYPE, "");
139140
#endif
140141

141142
#ifdef LC_MESSAGES
142-
pg_perm_setlocale(LC_MESSAGES, "");
143+
init_locale(LC_MESSAGES, "");
143144
#endif
144145

145146
/*
146147
* We keep these set to "C" always, except transiently in pg_locale.c; see
147148
* that file for explanations.
148149
*/
149-
pg_perm_setlocale(LC_MONETARY, "C");
150-
pg_perm_setlocale(LC_NUMERIC, "C");
151-
pg_perm_setlocale(LC_TIME, "C");
150+
init_locale(LC_MONETARY, "C");
151+
init_locale(LC_NUMERIC, "C");
152+
init_locale(LC_TIME, "C");
152153

153154
/*
154155
* Now that we have absorbed as much as we wish to from the locale
@@ -294,6 +295,23 @@ startup_hacks(const char *progname)
294295
}
295296

296297

298+
/*
299+
* Make the initial permanent setting for a locale category. If that fails,
300+
* perhaps due to LC_foo=invalid in the environment, use locale C. If even
301+
* that fails, perhaps due to out-of-memory, the entire startup fails with it.
302+
* When this returns, we are guaranteed to have a setting for the given
303+
* category's environment variable.
304+
*/
305+
static void
306+
init_locale(int category, const char *locale)
307+
{
308+
if (pg_perm_setlocale(category, locale) == NULL &&
309+
pg_perm_setlocale(category, "C") == NULL)
310+
elog(FATAL, "could not adopt C locale");
311+
}
312+
313+
314+
297315
/*
298316
* Help display should match the options accepted by PostmasterMain()
299317
* and PostgresMain().

0 commit comments

Comments
 (0)