From d4a522277bcd1e172be57f3525c2d24831bb3cc5 Mon Sep 17 00:00:00 2001 From: Alynx Zhou Date: Thu, 4 Jan 2024 10:29:49 +0800 Subject: [PATCH] system/region: Prevent preview crash from accessing invalid pointer In !2051, we switch back to real locale before setting the label text, however, according to nl_langinfo's manpage, the returned pointer could be invalid after switching locale or creating new locale, so the program may crash. To fix this, we save the result before switching locale, so we won't access the invalid pointer after switch locale. --- panels/region/cc-format-preview.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/panels/region/cc-format-preview.c b/panels/region/cc-format-preview.c index 7aea45511..e4f4afa32 100644 --- a/panels/region/cc-format-preview.c +++ b/panels/region/cc-format-preview.c @@ -69,6 +69,7 @@ update_format_examples (CcFormatPreview *self) g_autofree gchar *s = NULL; #ifdef LC_MEASUREMENT const gchar *fmt; + gboolean is_imperial = FALSE; #endif g_autoptr(GtkPaperSize) paper = NULL; @@ -133,6 +134,9 @@ update_format_examples (CcFormatPreview *self) old_locale = uselocale (locale); fmt = nl_langinfo (_NL_MEASUREMENT_MEASUREMENT); + /* The returned pointer of nl_langinfo could be invalid after switching + locale, so we must use it here. */ + is_imperial = fmt && *fmt == 2; if (locale != (locale_t) 0) { @@ -140,7 +144,7 @@ update_format_examples (CcFormatPreview *self) freelocale (locale); } - if (fmt && *fmt == 2) + if (is_imperial) gtk_label_set_text (GTK_LABEL (self->measurement_format_label), C_("measurement format", "Imperial")); else gtk_label_set_text (GTK_LABEL (self->measurement_format_label), C_("measurement format", "Metric")); -- 2.43.0