Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e8f82b4

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 fa042ab commit e8f82b4

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

@@ -122,31 +123,31 @@ main(int argc, char *argv[])
122123
char *env_locale;
123124

124125
if ((env_locale = getenv("LC_COLLATE")) != NULL)
125-
pg_perm_setlocale(LC_COLLATE, env_locale);
126+
init_locale(LC_COLLATE, env_locale);
126127
else
127-
pg_perm_setlocale(LC_COLLATE, "");
128+
init_locale(LC_COLLATE, "");
128129

129130
if ((env_locale = getenv("LC_CTYPE")) != NULL)
130-
pg_perm_setlocale(LC_CTYPE, env_locale);
131+
init_locale(LC_CTYPE, env_locale);
131132
else
132-
pg_perm_setlocale(LC_CTYPE, "");
133+
init_locale(LC_CTYPE, "");
133134
}
134135
#else
135-
pg_perm_setlocale(LC_COLLATE, "");
136-
pg_perm_setlocale(LC_CTYPE, "");
136+
init_locale(LC_COLLATE, "");
137+
init_locale(LC_CTYPE, "");
137138
#endif
138139

139140
#ifdef LC_MESSAGES
140-
pg_perm_setlocale(LC_MESSAGES, "");
141+
init_locale(LC_MESSAGES, "");
141142
#endif
142143

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

151152
/*
152153
* Now that we have absorbed as much as we wish to from the locale
@@ -299,6 +300,23 @@ startup_hacks(const char *progname)
299300
}
300301

301302

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

0 commit comments

Comments
 (0)