mirror of
https://github.com/postgres/postgres.git
synced 2025-08-27 07:42:10 +03:00
setlocale() on Windows doesn't work correctly if the locale name contains
dots. I previously worked around this in initdb, mapping the known problematic locale names to aliases that work, but Hiroshi Inoue pointed out that that's not enough because even if you use one of the aliases, like "Chinese_HKG", setlocale(LC_CTYPE, NULL) returns back the long form, ie. "Chinese_Hong Kong S.A.R.". When we try to restore an old locale value by passing that value back to setlocale(), it fails. Note that you are affected by this bug also if you use one of those short-form names manually, so just reverting the hack in initdb won't fix it. To work around that, move the locale name mapping from initdb to a wrapper around setlocale(), so that the mapping is invoked on every setlocale() call. Also, add a few checks for failed setlocale() calls in the backend. These calls shouldn't fail, and if they do there isn't much we can do about it, but at least you'll get a warning. Backpatch to 9.1, where the initdb hack was introduced. The Windows bug affects older versions too if you set locale manually to one of the aliases, but given the lack of complaints from the field, I'm hesitent to backpatch.
This commit is contained in:
@@ -356,3 +356,109 @@ pg_get_encoding_from_locale(const char *ctype, bool write_message)
|
||||
}
|
||||
|
||||
#endif /* (HAVE_LANGINFO_H && CODESET) || WIN32 */
|
||||
|
||||
#ifdef WIN32
|
||||
/*
|
||||
* Windows has a problem with locale names that have a dot in the country
|
||||
* name. For example:
|
||||
*
|
||||
* "Chinese (Traditional)_Hong Kong S.A.R..950"
|
||||
*
|
||||
* For some reason, setlocale() doesn't accept that. Fortunately, Windows'
|
||||
* setlocale() accepts various alternative names for such countries, so we
|
||||
* provide a wrapper setlocale() function that maps the troublemaking locale
|
||||
* names to accepted aliases.
|
||||
*/
|
||||
|
||||
#undef setlocale
|
||||
|
||||
struct locale_map
|
||||
{
|
||||
const char *locale_name_part; /* string in locale name to replace */
|
||||
const char *replacement; /* string to replace it with */
|
||||
};
|
||||
|
||||
static const struct locale_map locale_map_list[] = {
|
||||
|
||||
/*
|
||||
* "HKG" is listed here:
|
||||
* http://msdn.microsoft.com/en-us/library/cdax410z%28v=vs.71%29.aspx
|
||||
* (Country/Region Strings).
|
||||
*
|
||||
* "ARE" is the ISO-3166 three-letter code for U.A.E. It is not on the
|
||||
* above list, but seems to work anyway.
|
||||
*/
|
||||
{ "Hong Kong S.A.R.", "HKG" },
|
||||
{ "U.A.E.", "ARE" },
|
||||
|
||||
/*
|
||||
* The ISO-3166 country code for Macau S.A.R. is MAC, but Windows doesn't
|
||||
* seem to recognize that. And Macau isn't listed in the table of
|
||||
* accepted abbreviations linked above. Fortunately, "ZHM" seems to be
|
||||
* accepted as an alias for "Chinese (Traditional)_Macau S.A.R..950". I'm
|
||||
* not sure where "ZHM" comes from, must be some legacy naming scheme. But
|
||||
* hey, it works.
|
||||
*
|
||||
* Note that unlike HKG and ARE, ZHM is an alias for the *whole* locale
|
||||
* name, not just the country part.
|
||||
*
|
||||
* Some versions of Windows spell it "Macau", others "Macao".
|
||||
*/
|
||||
{ "Chinese (Traditional)_Macau S.A.R..950", "ZHM" },
|
||||
{ "Chinese_Macau S.A.R..950", "ZHM" },
|
||||
{ "Chinese (Traditional)_Macao S.A.R..950", "ZHM" },
|
||||
{ "Chinese_Macao S.A.R..950", "ZHM" }
|
||||
};
|
||||
|
||||
char *
|
||||
pgwin32_setlocale(int category, const char *locale)
|
||||
{
|
||||
char *result;
|
||||
char *alias;
|
||||
int i;
|
||||
|
||||
if (locale == NULL)
|
||||
return setlocale(category, locale);
|
||||
|
||||
/* Check if the locale name matches any of the problematic ones. */
|
||||
alias = NULL;
|
||||
for (i = 0; i < lengthof(locale_map_list); i++)
|
||||
{
|
||||
const char *needle = locale_map_list[i].locale_name_part;
|
||||
const char *replacement = locale_map_list[i].replacement;
|
||||
char *match;
|
||||
|
||||
match = strstr(locale, needle);
|
||||
if (match != NULL)
|
||||
{
|
||||
/* Found a match. Replace the matched string. */
|
||||
int matchpos = match - locale;
|
||||
int replacementlen = strlen(replacement);
|
||||
char *rest = match + strlen(needle);
|
||||
int restlen = strlen(rest);
|
||||
|
||||
alias = malloc(matchpos + replacementlen + restlen + 1);
|
||||
if (!alias)
|
||||
return NULL;
|
||||
|
||||
memcpy(&alias[0], &locale[0], matchpos);
|
||||
memcpy(&alias[matchpos], replacement, replacementlen);
|
||||
memcpy(&alias[matchpos + replacementlen], rest, restlen + 1); /* includes null terminator */
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Call the real setlocale() function */
|
||||
if (alias)
|
||||
{
|
||||
result = setlocale(category, alias);
|
||||
free(alias);
|
||||
}
|
||||
else
|
||||
result = setlocale(category, locale);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif /* WIN32 */
|
||||
|
Reference in New Issue
Block a user