Skip to content

Commit 230865e

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 4dd51b3 commit 230865e

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

4949

5050
static void startup_hacks(const char *progname);
51+
static void init_locale(int category, const char *locale);
5152
static void help(const char *progname);
5253
static void check_root(const char *progname);
5354
static char *get_current_username(const char *progname);
@@ -121,31 +122,31 @@ main(int argc, char *argv[])
121122
char *env_locale;
122123

123124
if ((env_locale = getenv("LC_COLLATE")) != NULL)
124-
pg_perm_setlocale(LC_COLLATE, env_locale);
125+
init_locale(LC_COLLATE, env_locale);
125126
else
126-
pg_perm_setlocale(LC_COLLATE, "");
127+
init_locale(LC_COLLATE, "");
127128

128129
if ((env_locale = getenv("LC_CTYPE")) != NULL)
129-
pg_perm_setlocale(LC_CTYPE, env_locale);
130+
init_locale(LC_CTYPE, env_locale);
130131
else
131-
pg_perm_setlocale(LC_CTYPE, "");
132+
init_locale(LC_CTYPE, "");
132133
}
133134
#else
134-
pg_perm_setlocale(LC_COLLATE, "");
135-
pg_perm_setlocale(LC_CTYPE, "");
135+
init_locale(LC_COLLATE, "");
136+
init_locale(LC_CTYPE, "");
136137
#endif
137138

138139
#ifdef LC_MESSAGES
139-
pg_perm_setlocale(LC_MESSAGES, "");
140+
init_locale(LC_MESSAGES, "");
140141
#endif
141142

142143
/*
143144
* We keep these set to "C" always, except transiently in pg_locale.c; see
144145
* that file for explanations.
145146
*/
146-
pg_perm_setlocale(LC_MONETARY, "C");
147-
pg_perm_setlocale(LC_NUMERIC, "C");
148-
pg_perm_setlocale(LC_TIME, "C");
147+
init_locale(LC_MONETARY, "C");
148+
init_locale(LC_NUMERIC, "C");
149+
init_locale(LC_TIME, "C");
149150

150151
/*
151152
* Now that we have absorbed as much as we wish to from the locale
@@ -292,6 +293,23 @@ startup_hacks(const char *progname)
292293
}
293294

294295

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

0 commit comments

Comments
 (0)