[patch] serious problem in pango_layout_set_text()



Hi,

attached is a patch that fixes a problem with pango_layout_set_text():
If an invalid UTF8 string is set, this function used to spit out a 
warning and returned in an inconsistent state (layout->text g_free'd)
which causes the function to crash when being called the next time.

The patch adds a gboolean return value that indicates success of the
operation to give the application programmer a chance to react to this
problem (for example by displaying "[ Invalid UTF8 string ]" instead).


Salut, Sven
 
Index: pango/pango-layout.c
===================================================================
RCS file: /cvs/gnome/pango/pango/pango-layout.c,v
retrieving revision 1.68
diff -u -r1.68 pango-layout.c
--- pango/pango-layout.c	2001/08/09 07:29:41	1.68
+++ pango/pango-layout.c	2001/08/22 20:45:45
@@ -714,14 +714,17 @@
  *          calculated.
  * 
  * Set the text of the layout.
+ *
+ * Returns: %FALSE if the @text could not be set. This can only happen if
+ * @text is an invalid UTF8 string.
  **/
-void
+gboolean
 pango_layout_set_text (PangoLayout *layout,
 		       const char  *text,
 		       int          length)
 {
-  g_return_if_fail (layout != NULL);
-  g_return_if_fail (length == 0 || text != NULL);
+  g_return_val_if_fail (layout != NULL, FALSE);
+  g_return_val_if_fail (length == 0 || text != NULL, FALSE);
   
   if (layout->text)
     g_free (layout->text);
@@ -741,7 +744,11 @@
 	  if (g_utf8_get_char (p) == (gunichar)-1)
 	    {
 	      g_warning ("Invalid UTF8 string passed to pango_layout_set_text()");
-	      return;
+              layout->text = g_strdup ("");
+              layout->n_chars = 0;
+              pango_layout_clear_lines (layout);
+              
+	      return FALSE;
 	    }
 	  p = g_utf8_next_char (p);
 	  n_chars++;
@@ -768,6 +775,8 @@
   layout->length = length;
 
   pango_layout_clear_lines (layout);
+
+  return TRUE;
 }
 
 /**
Index: pango/pango-layout.h
===================================================================
RCS file: /cvs/gnome/pango/pango/pango-layout.h,v
retrieving revision 1.26
diff -u -r1.26 pango-layout.h
--- pango/pango-layout.h	2001/06/08 16:02:59	1.26
+++ pango/pango-layout.h	2001/08/22 20:45:45
@@ -83,7 +83,7 @@
 					    PangoAttrList  *attrs);
 PangoAttrList *pango_layout_get_attributes (PangoLayout    *layout);
 
-void           pango_layout_set_text       (PangoLayout    *layout,
+gboolean       pango_layout_set_text       (PangoLayout    *layout,
 					    const char     *text,
 					    int             length);
 const char    *pango_layout_get_text       (PangoLayout    *layout);




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]