Commit 1f33beb6 authored by Michael Catanzaro's avatar Michael Catanzaro Committed by Michael Catanzaro

gnome-languages: Use uselocale to avoid threadsafety issues

setlocale is MT-Unsafe!
parent 5804fa7c
Pipeline #62742 passed with stage
in 2 minutes and 31 seconds
...@@ -286,15 +286,15 @@ gnome_normalize_locale (const char *locale) ...@@ -286,15 +286,15 @@ gnome_normalize_locale (const char *locale)
static gboolean static gboolean
language_name_is_valid (const char *language_name) language_name_is_valid (const char *language_name)
{ {
gboolean is_valid; locale_t locale;
int lc_type_id = LC_MESSAGES;
g_autofree char *old_locale = NULL;
old_locale = g_strdup (setlocale (lc_type_id, NULL)); locale = newlocale (LC_MESSAGES_MASK, language_name, (locale_t) 0);
is_valid = setlocale (lc_type_id, language_name) != NULL; if (locale != (locale_t) 0) {
setlocale (lc_type_id, old_locale); freelocale (locale);
return TRUE;
}
return is_valid; return FALSE;
} }
static void static void
...@@ -302,14 +302,16 @@ language_name_get_codeset_details (const char *language_name, ...@@ -302,14 +302,16 @@ language_name_get_codeset_details (const char *language_name,
char **pcodeset, char **pcodeset,
gboolean *is_utf8) gboolean *is_utf8)
{ {
g_autofree char *old_locale = NULL; locale_t locale;
locale_t old_locale;
const char *codeset = NULL; const char *codeset = NULL;
old_locale = g_strdup (setlocale (LC_CTYPE, NULL)); locale = newlocale (LC_CTYPE_MASK, language_name, (locale_t) 0);
if (locale == (locale_t) 0)
if (setlocale (LC_CTYPE, language_name) == NULL)
return; return;
old_locale = uselocale (locale);
codeset = nl_langinfo (CODESET); codeset = nl_langinfo (CODESET);
if (pcodeset != NULL) { if (pcodeset != NULL) {
...@@ -322,7 +324,8 @@ language_name_get_codeset_details (const char *language_name, ...@@ -322,7 +324,8 @@ language_name_get_codeset_details (const char *language_name,
*is_utf8 = strcmp (normalized_codeset, "UTF-8") == 0; *is_utf8 = strcmp (normalized_codeset, "UTF-8") == 0;
} }
setlocale (LC_CTYPE, old_locale); uselocale (old_locale);
freelocale (locale);
} }
/** /**
...@@ -699,12 +702,13 @@ get_translated_language (const char *code, ...@@ -699,12 +702,13 @@ get_translated_language (const char *code,
name = NULL; name = NULL;
if (language != NULL) { if (language != NULL) {
const char *translated_name; const char *translated_name;
g_autofree char *old_locale = NULL; locale_t loc;
locale_t old_locale;
if (locale != NULL) { if (locale != NULL) {
old_locale = g_strdup (setlocale (LC_MESSAGES, NULL)); loc = newlocale (LC_MESSAGES_MASK, locale, (locale_t) 0);
setlocale (LC_MESSAGES, locale); old_locale = uselocale (loc);
} }
if (is_fallback_language (code)) { if (is_fallback_language (code)) {
...@@ -717,7 +721,8 @@ get_translated_language (const char *code, ...@@ -717,7 +721,8 @@ get_translated_language (const char *code,
} }
if (locale != NULL) { if (locale != NULL) {
setlocale (LC_MESSAGES, old_locale); uselocale (old_locale);
freelocale (loc);
} }
} }
...@@ -754,12 +759,13 @@ get_translated_territory (const char *code, ...@@ -754,12 +759,13 @@ get_translated_territory (const char *code,
name = NULL; name = NULL;
if (territory != NULL) { if (territory != NULL) {
const char *translated_territory; const char *translated_territory;
g_autofree char *old_locale = NULL; locale_t loc;
locale_t old_locale;
g_autofree char *tmp = NULL; g_autofree char *tmp = NULL;
if (locale != NULL) { if (locale != NULL) {
old_locale = g_strdup (setlocale (LC_MESSAGES, NULL)); loc = newlocale (LC_MESSAGES_MASK, locale, (locale_t) 0);
setlocale (LC_MESSAGES, locale); old_locale = uselocale (loc);
} }
translated_territory = dgettext ("iso_3166", territory); translated_territory = dgettext ("iso_3166", territory);
...@@ -767,7 +773,8 @@ get_translated_territory (const char *code, ...@@ -767,7 +773,8 @@ get_translated_territory (const char *code,
name = capitalize_utf8_string (tmp); name = capitalize_utf8_string (tmp);
if (locale != NULL) { if (locale != NULL) {
setlocale (LC_MESSAGES, old_locale); uselocale (old_locale);
freelocale (loc);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment