# HG changeset patch
# User Darren Salt <linux@youmustbejoking.demon.co.uk>
# Date 1213804224 -3600
# Node ID 7f9c887bfda6de22070eb189ad0a3d9478b7bb29
# Parent  d92320ace965ba12bd40784c803791b9796663b4
Cope better with invalid UTF-8 when adding text to the log buffers.

diff --git a/ChangeLog b/ChangeLog
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,7 @@ 0.5.904: 2008/??/??
 0.5.904: 2008/??/??
 	* Add a lock to avoid a race which can cause GTK crashes when
 	  updating the console log window.
+	* Cope better with invalid UTF-8 when adding text to log buffers.
 	* Properly encode name & password for HTTP auth. (xine-lib 1.2)
 	* If the user cancels the HTTP auth dbox, don't re-ask if repeating.
 
diff --git a/src/log_window.c b/src/log_window.c
--- a/src/log_window.c
+++ b/src/log_window.c
@@ -83,6 +83,33 @@ static void log_clip (GtkTextBuffer *buf
   }
 }
 
+static void log_do_insert (GtkTextBuffer *buffer, GtkTextIter *pos,
+			   const char *text, ssize_t length, const char *tag)
+{
+  if (tag)
+    gtk_text_buffer_insert_with_tags_by_name (buffer, pos, text, length, tag, NULL);
+  else
+    gtk_text_buffer_insert (buffer, pos, text, length);
+}
+
+static void log_insert (GtkTextBuffer *buffer, GtkTextIter *pos,
+			const char *text, const char *tag)
+{
+  const gchar *end = text;
+  while (*end)
+  {
+    text = end;
+    g_utf8_validate (end, -1, &end);
+    log_do_insert (buffer, pos, text, end - text, tag);
+    if (*end)
+    {
+      log_do_insert (buffer, pos, "�", -1, tag);
+      while ((*++end & 0x7F) < 0x40)
+	/* skip 0x80..0xBF */;
+    }
+  }
+}
+
 static void add_log_line (GtkTextBuffer *buffer, GtkTextIter *pos,
 			  const char *text, gint length)
 {
@@ -92,7 +119,7 @@ static void add_log_line (GtkTextBuffer 
       (buffer, pos, text, ++colon - text, "datestamp", NULL);
   else
     colon = text;
-  gtk_text_buffer_insert (buffer, pos, colon, -1);
+  log_insert (buffer, pos, colon, NULL);
 }
 
 typedef struct {
@@ -217,10 +244,7 @@ static void log_console_text_internal (c
 
   gdk_threads_enter ();
   gtk_text_buffer_get_end_iter (buffer, &end);
-  if (err)
-    gtk_text_buffer_insert_with_tags_by_name (buffer, &end, line, -1, "error", NULL);
-  else
-    gtk_text_buffer_insert (buffer, &end, line, -1);
+  log_insert (buffer, &end, line, "error");
   log_clip (buffer);
   gdk_threads_leave ();
 }
