From 1f6db2a5c5822a55a83bfdc830f1548908b3c75c Mon Sep 17 00:00:00 2001
From: Mikhail Fludkov <fludkov.me@gmail.com>
Date: Thu, 20 Sep 2018 16:08:19 +0200
Subject: [PATCH 079/682] glib/gcharset: fix leaking
 g_get_language_names_with_category

---
 glib/gcharset.c      |  7 ++++---
 glib/tests/charset.c | 13 +++++++++++++
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/glib/gcharset.c b/glib/gcharset.c
index bfcd12590..a97b33a03 100644
--- a/glib/gcharset.c
+++ b/glib/gcharset.c
@@ -576,15 +576,16 @@ g_get_language_names (void)
  *
  * g_get_language_names() returns g_get_language_names_with_category("LC_MESSAGES").
  *
- * Returns: (array zero-terminated=1) (transfer none): a %NULL-terminated array of strings owned by GLib
- *    that must not be modified or freed.
+ * Returns: (array zero-terminated=1) (transfer none): a %NULL-terminated array of strings owned by
+ *    the thread g_get_language_names_with_category was called from.
+ *    It must not be modified or freed. It must be copied if planned to be used in another thread.
  *
  * Since: 2.58
  */
 const gchar * const *
 g_get_language_names_with_category (const gchar *category_name)
 {
-  static GPrivate cache_private = G_PRIVATE_INIT ((void (*)(gpointer)) g_hash_table_remove_all);
+  static GPrivate cache_private = G_PRIVATE_INIT ((void (*)(gpointer)) g_hash_table_unref);
   GHashTable *cache = g_private_get (&cache_private);
   const gchar *languages;
   GLanguageNamesCache *name_cache;
diff --git a/glib/tests/charset.c b/glib/tests/charset.c
index 0a1c8ce3f..363eedfd1 100644
--- a/glib/tests/charset.c
+++ b/glib/tests/charset.c
@@ -59,6 +59,18 @@ test_language_names_with_category (void)
     }
 }
 
+static void
+test_language_names_with_category_async (void)
+{
+  g_thread_join (g_thread_new (
+      NULL, (GThreadFunc)g_get_language_names_with_category, "LC_CTYPE"));
+
+  /* g_get_language_names_with_category returns a pointer to a memory
+     which is owned by a thread it has been called from. The thread is dead now,
+     therefore returned pointer can't be used at this stage.
+  */
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -67,6 +79,7 @@ main (int argc, char *argv[])
   g_test_bug_base ("http://bugs.gnome.org/");
 
   g_test_add_func ("/charset/language_names_with_category", test_language_names_with_category);
+  g_test_add_func ("/charset/language_names_with_category_async", test_language_names_with_category_async);
 
   return g_test_run ();
 }
-- 
2.19.1

