From 2b26c349c9146543e8813eba3c06f2d1a763e210 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=A0=D1=83=D1=81=D0=BB=D0=B0=D0=BD=20=D0=98=D0=B6=D0=B1=D1?=
 =?UTF-8?q?=83=D0=BB=D0=B0=D1=82=D0=BE=D0=B2?= <lrn1986@gmail.com>
Date: Fri, 16 Dec 2011 13:42:49 +0400
Subject: [PATCH 3/3] Be more careful with strings

TODO: validate EVERY UTF-8 string we get from the net! And convert C ones.
---
 src/fs/gnunet-fs-gtk-common.c |   25 ++++++++++++++++++++++---
 1 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/src/fs/gnunet-fs-gtk-common.c b/src/fs/gnunet-fs-gtk-common.c
index e7cb07f..1a250c6 100644
--- a/src/fs/gnunet-fs-gtk-common.c
+++ b/src/fs/gnunet-fs-gtk-common.c
@@ -48,13 +48,32 @@ GNUNET_FS_GTK_add_meta_data_to_list_store (void *cls, const char *plugin_name,
                                            const char *data_mime_type,
                                            const char *data, size_t data_len)
 {
+  gchar *data_to_insert = NULL;
+  gboolean freedata = FALSE;
   GtkListStore *ls = GTK_LIST_STORE (cls);
 
-  if ((format == EXTRACTOR_METAFORMAT_UTF8) ||
-      (format == EXTRACTOR_METAFORMAT_C_STRING))
+  if (format == EXTRACTOR_METAFORMAT_UTF8)
+  {
+    if (g_utf8_validate (data, data_len, NULL))
+      data_to_insert = data;
+    else
+      format = EXTRACTOR_METAFORMAT_C_STRING;
+  }
+  if (format == EXTRACTOR_METAFORMAT_C_STRING)
+  {
+    gsize rd, wr;
+    data_to_insert = g_locale_to_utf8 (data, data_len, &rd, &wr, NULL);
+    freedata = TRUE;
+  }
+
+  if (data_to_insert)
+  {
     gtk_list_store_insert_with_values (ls, NULL, G_MAXINT, 0, type, 1, format,
                                        2, EXTRACTOR_metatype_to_string (type),
-                                       3, data, -1);
+                                       3, data_to_insert, -1);
+    if (freedata)
+      g_free (data_to_insert);
+  }
   return 0;
 }
 
-- 
1.7.4

