From 20ac70e76fb71cf775203a042b90c20ec019b6dc Mon Sep 17 00:00:00 2001
From: Michael Natterer <mitch@gimp.org>
Date: Mon, 22 Nov 2004 15:25:50 +0000
Subject: [PATCH] use g_strndup() instead of g_strdup() if a length was passed.

2004-11-22  Michael Natterer  <mitch@gimp.org>

	* libgimpbase/gimputils.c (gimp_any_to_utf8): use g_strndup()
	instead of g_strdup() if a length was passed.

	* app/dialogs/info-window.c: g_strndup() the comment parasite's
	data and pass -1 as length to gimp_any_to_utf8() so we don't
	encounter the questionable (buggy?) behavior of g_utf8_validate()
	to fail upon finding '\0' within the "length" passed.
	Fixes bug #159051.
---
 ChangeLog                 | 11 +++++++++++
 app/dialogs/info-window.c | 40 +++++++++++++++++++++++++++++++--------
 libgimpbase/gimputils.c   | 11 +++++++++--
 3 files changed, 52 insertions(+), 10 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 88af826550..8fe8dd8ed7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2004-11-22  Michael Natterer  <mitch@gimp.org>
+
+	* libgimpbase/gimputils.c (gimp_any_to_utf8): use g_strndup()
+	instead of g_strdup() if a length was passed.
+
+	* app/dialogs/info-window.c: g_strndup() the comment parasite's
+	data and pass -1 as length to gimp_any_to_utf8() so we don't
+	encounter the questionable (buggy?) behavior of g_utf8_validate()
+	to fail upon finding '\0' within the "length" passed.
+	Fixes bug #159051.
+
 2004-11-22  Michael Natterer  <mitch@gimp.org>
 
 	* plug-ins/common/struc.c: applied patch from Wolfgang Hofer
diff --git a/app/dialogs/info-window.c b/app/dialogs/info-window.c
index 17349de03f..b889939780 100644
--- a/app/dialogs/info-window.c
+++ b/app/dialogs/info-window.c
@@ -220,6 +220,7 @@ info_window_create_comment (InfoDialog  *info_win,
   GtkWidget    *vbox;
   GtkWidget    *vbox2;
   GimpParasite *comment;
+  gchar        *utf8;
 
   vbox = gtk_vbox_new (FALSE, 12);
   gtk_container_set_border_width (GTK_CONTAINER (vbox), 6);
@@ -234,11 +235,22 @@ info_window_create_comment (InfoDialog  *info_win,
 
   /*  image comment  */
   comment = gimp_image_parasite_find (gdisp->gimage, "gimp-comment");
-  if (comment == NULL)
-    g_snprintf (iwd->comment_str, MAX_BUF, "%s", _("(none)"));
+
+  if (comment)
+    {
+      gchar *str = g_strndup (gimp_parasite_data (comment),
+                              gimp_parasite_data_size (comment));
+
+      utf8 = gimp_any_to_utf8 (str, -1, NULL);
+    }
   else
-    g_snprintf (iwd->comment_str, MAX_BUF, "%s",
-                gimp_any_to_utf8 (comment->data, comment->size, NULL));
+    {
+      utf8 = g_strdup (_("(none)"));
+    }
+
+  g_snprintf (iwd->comment_str, MAX_BUF, "%s", utf8);
+
+  g_free (utf8);
 
   label = gtk_label_new (iwd->comment_str);
   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
@@ -606,13 +618,25 @@ info_window_update (GimpDisplay *gdisp)
   /*  image comment  */
   {
     GimpParasite *comment;
+    gchar        *utf8;
 
     comment = gimp_image_parasite_find (gdisp->gimage, "gimp-comment");
-    if (comment == NULL)
-      g_snprintf (iwd->comment_str, MAX_BUF, "%s", _("(none)"));
+
+    if (comment)
+      {
+        gchar *str = g_strndup (gimp_parasite_data (comment),
+                                gimp_parasite_data_size (comment));
+
+        utf8 = gimp_any_to_utf8 (str, -1, NULL);
+      }
     else
-      g_snprintf (iwd->comment_str, MAX_BUF, "%s",
-                  gimp_any_to_utf8 (comment->data, comment->size, NULL));
+      {
+        utf8 = g_strdup (_("(none)"));
+      }
+
+    g_snprintf (iwd->comment_str, MAX_BUF, "%s", utf8);
+
+    g_free (utf8);
   }
 
   {
diff --git a/libgimpbase/gimputils.c b/libgimpbase/gimputils.c
index e85364c975..9dbce800ad 100644
--- a/libgimpbase/gimputils.c
+++ b/libgimpbase/gimputils.c
@@ -136,9 +136,16 @@ gimp_any_to_utf8 (const gchar  *str,
   g_return_val_if_fail (str != NULL, NULL);
 
   if (g_utf8_validate (str, len, &start_invalid))
-    utf8 = g_strdup (str);
+    {
+      if (len < 0)
+        utf8 = g_strdup (str);
+      else
+        utf8 = g_strndup (str, len);
+    }
   else
-    utf8 = g_locale_to_utf8 (str, len, NULL, NULL, NULL);
+    {
+      utf8 = g_locale_to_utf8 (str, len, NULL, NULL, NULL);
+    }
 
   if (! utf8)
     {