[gnome-control-center/mcatanzaro/uselocale] Replace unsafe setlocale() use with uselocale()



commit f2a95824d603150a8951cf86787c9cf6281571b6
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Wed Feb 20 23:42:38 2019 -0600

    Replace unsafe setlocale() use with uselocale()
    
    setlocale() is not threadsafe except when used only to query the locale
    without changing it. Let's use uselocale() instead, which changes the
    locale only on the calling thread. Much better.

 panels/datetime/date-endian.c        | 11 +++++++---
 panels/region/cc-format-chooser.c    | 42 +++++++++++++++++++-----------------
 panels/region/cc-region-panel.c      | 13 ++++++-----
 panels/user-accounts/cc-user-panel.c | 11 +++++-----
 4 files changed, 44 insertions(+), 33 deletions(-)
---
diff --git a/panels/datetime/date-endian.c b/panels/datetime/date-endian.c
index 34f1d3893..3cc4c1b31 100644
--- a/panels/datetime/date-endian.c
+++ b/panels/datetime/date-endian.c
@@ -149,12 +149,17 @@ DateEndianess
 date_endian_get_for_lang (const char *lang,
                          gboolean    verbose)
 {
-       const char *old_lang;
+       locale_t locale;
+       locale_t old_locale;
        DateEndianess endian;
 
-       old_lang = setlocale (LC_TIME, lang);
+       locale = newlocale (LC_TIME_MASK, lang, (locale_t) 0);
+       old_locale = uselocale (locale);
+
        endian = date_endian_get_default (verbose);
-       setlocale (LC_TIME, old_lang);
+
+       uselocale (old_locale);
+       freelocale (locale);
 
        return endian;
 }
diff --git a/panels/region/cc-format-chooser.c b/panels/region/cc-format-chooser.c
index 56ba6e16d..39073f156 100644
--- a/panels/region/cc-format-chooser.c
+++ b/panels/region/cc-format-chooser.c
@@ -66,48 +66,48 @@ display_date (GtkWidget *label, GDateTime *dt, const gchar *format)
 static void
 update_format_examples (CcFormatChooser *chooser)
 {
-        g_autofree gchar *time_locale = NULL;
-        g_autofree gchar *numeric_locale = NULL;
-        g_autofree gchar *monetary_locale = NULL;
-        g_autofree gchar *measurement_locale = NULL;
-        g_autofree gchar *paper_locale = NULL;
+        locale_t locale;
+       locale_t old_locale;
         g_autoptr(GDateTime) dt = NULL;
         g_autofree gchar *s = NULL;
         const gchar *fmt;
         g_autoptr(GtkPaperSize) paper = NULL;
 
-        time_locale = g_strdup (setlocale (LC_TIME, NULL));
-        setlocale (LC_TIME, chooser->region);
+       locale = newlocale (LC_TIME_MASK, chooser->region, (locale_t) 0);
+       old_locale = uselocale (locale);
 
         dt = g_date_time_new_now_local ();
         display_date (chooser->date_format_label, dt, "%x");
         display_date (chooser->time_format_label, dt, "%X");
         display_date (chooser->date_time_format_label, dt, "%c");
 
-        setlocale (LC_TIME, time_locale);
+        uselocale (old_locale);
+       freelocale (locale);
 
-        numeric_locale = g_strdup (setlocale (LC_NUMERIC, NULL));
-        setlocale (LC_NUMERIC, chooser->region);
+       locale = newlocale (LC_NUMERIC_MASK, chooser->region, (locale_t) 0);
+       old_locale = uselocale (locale);
 
         s = g_strdup_printf ("%'.2f", 123456789.00);
         gtk_label_set_text (GTK_LABEL (chooser->number_format_label), s);
 
-        setlocale (LC_NUMERIC, numeric_locale);
+        uselocale (old_locale);
+       freelocale (locale);
 
 #if 0
-        monetary_locale = g_strdup (setlocale (LC_MONETARY, NULL));
-        setlocale (LC_MONETARY, chooser->region);
+       locale = newlocale (LC_MONETARY_MASK, chooser->region, (locale_t) 0);
+       old_locale = uselocale (locale);
 
         num_info = localeconv ();
         if (num_info != NULL)
                 gtk_label_set_text (GTK_LABEL (chooser->currency_format_label), num_info->currency_symbol);
 
-        setlocale (LC_MONETARY, monetary_locale);
+        uselocale (old_locale);
+       freelocale (locale);
 #endif
 
 #ifdef LC_MEASUREMENT
-        measurement_locale = g_strdup (setlocale (LC_MEASUREMENT, NULL));
-        setlocale (LC_MEASUREMENT, chooser->region);
+       locale = newlocale (LC_MEASUREMENT_MASK, chooser->region, (locale_t) 0);
+       old_locale = uselocale (locale);
 
         fmt = nl_langinfo (_NL_MEASUREMENT_MEASUREMENT);
         if (fmt && *fmt == 2)
@@ -115,17 +115,19 @@ update_format_examples (CcFormatChooser *chooser)
         else
                 gtk_label_set_text (GTK_LABEL (chooser->measurement_format_label), C_("measurement format", 
"Metric"));
 
-        setlocale (LC_MEASUREMENT, measurement_locale);
+        uselocale (old_locale);
+       freelocale (locale);
 #endif
 
 #ifdef LC_PAPER
-        paper_locale = g_strdup (setlocale (LC_PAPER, NULL));
-        setlocale (LC_PAPER, chooser->region);
+       locale = newlocale (LC_PAPER_MASK, chooser->region, (locale_t) 0);
+       old_locale = uselocale (locale);
 
         paper = gtk_paper_size_new (gtk_paper_size_get_default ());
         gtk_label_set_text (GTK_LABEL (chooser->paper_format_label), gtk_paper_size_get_display_name 
(paper));
 
-        setlocale (LC_PAPER, paper_locale);
+        uselocale (old_locale);
+       freelocale (locale);
 #endif
 }
 
diff --git a/panels/region/cc-region-panel.c b/panels/region/cc-region-panel.c
index 35859526d..798b6033b 100644
--- a/panels/region/cc-region-panel.c
+++ b/panels/region/cc-region-panel.c
@@ -227,20 +227,23 @@ set_restart_notification_visible (CcRegionPanel *self,
                                   const gchar   *locale,
                                   gboolean       visible)
 {
-        g_autofree gchar *current_locale = NULL;
+       locale_t new_locale;
+        locale_t current_locale;
         g_autoptr(GFile) file = NULL;
         g_autoptr(GFileOutputStream) output_stream = NULL;
         g_autoptr(GError) error = NULL;
 
         if (locale) {
-                current_locale = g_strdup (setlocale (LC_MESSAGES, NULL));
-                setlocale (LC_MESSAGES, locale);
+               new_locale = newlocale (LC_MESSAGES_MASK, locale, (locale_t) 0);
+               current_locale = uselocale (new_locale);
         }
 
         gtk_revealer_set_reveal_child (self->restart_revealer, visible);
 
-        if (locale)
-                setlocale (LC_MESSAGES, current_locale);
+        if (locale) {
+                uselocale (current_locale);
+               freelocale (new_locale);
+       }
 
         file = get_needs_restart_file ();
 
diff --git a/panels/user-accounts/cc-user-panel.c b/panels/user-accounts/cc-user-panel.c
index 07a12dda9..9aa73e4de 100644
--- a/panels/user-accounts/cc-user-panel.c
+++ b/panels/user-accounts/cc-user-panel.c
@@ -954,18 +954,19 @@ restart_now (CcUserPanel *self)
 static void
 show_restart_notification (CcUserPanel *self, const gchar *locale)
 {
-        gchar *current_locale;
+        locale_t current_locale;
+        locale_t new_locale;
 
         if (locale) {
-                current_locale = g_strdup (setlocale (LC_MESSAGES, NULL));
-                setlocale (LC_MESSAGES, locale);
+                new_locale = newlocale (LC_MESSAGES_MASK, locale, (locale_t) 0);
+                current_locale = uselocale (new_locale);
         }
 
         gtk_revealer_set_reveal_child (self->notification_revealer, TRUE);
 
         if (locale) {
-                setlocale (LC_MESSAGES, current_locale);
-                g_free (current_locale);
+                uselocale (current_locale);
+                freelocale (new_locale);
         }
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]