[gtk+] inspector: fix a crash in the CSS editor



commit 6525a11b8d1fa6385a307f12df2eb321e7046564
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Aug 24 20:27:13 2014 +0200

    inspector: fix a crash in the CSS editor
    
    gtk_css_section_get_end_position() can return a position one byte beyond
    the end of the last line.
    
    gtk_text_buffer_get_iter_at_line_index() accepts only valid
    line_number/byte_index pairs. Another solution is to make the
    GtkTextBuffer function less strict, by returning a boolean if the exact
    position was found.
    
    The CSS parser should also be fixed to always return valid positions.
    But it's better to have a safety net in the CSS editor, just in case (a
    warning could be print).
    
    https://bugzilla.gnome.org/show_bug.cgi?id=735337

 gtk/inspector/css-editor.c |   37 +++++++++++++++++++++++++++++--------
 1 files changed, 29 insertions(+), 8 deletions(-)
---
diff --git a/gtk/inspector/css-editor.c b/gtk/inspector/css-editor.c
index 33c7877..d8f5181 100644
--- a/gtk/inspector/css-editor.c
+++ b/gtk/inspector/css-editor.c
@@ -259,6 +259,27 @@ text_changed (GtkTextBuffer         *buffer,
   ce->priv->timeout = g_timeout_add (100, update_timeout, ce); 
 }
 
+/* Safe version of gtk_text_buffer_get_iter_at_line_index(). */
+static void
+safe_get_iter_at_line_index (GtkTextBuffer *buffer,
+                             GtkTextIter   *iter,
+                             gint           line_number,
+                             gint           byte_index)
+{
+  if (line_number >= gtk_text_buffer_get_line_count (buffer))
+    {
+      gtk_text_buffer_get_end_iter (buffer, iter);
+      return;
+    }
+
+  gtk_text_buffer_get_iter_at_line (buffer, iter, line_number);
+
+  if (byte_index < gtk_text_iter_get_bytes_in_line (iter))
+    gtk_text_iter_set_line_index (iter, byte_index);
+  else
+    gtk_text_iter_forward_to_line_end (iter);
+}
+
 static void
 show_parsing_error (GtkCssProvider        *provider,
                     GtkCssSection         *section,
@@ -269,14 +290,14 @@ show_parsing_error (GtkCssProvider        *provider,
   const char *tag_name;
   GtkTextBuffer *buffer = GTK_TEXT_BUFFER (ce->priv->text);
 
-  gtk_text_buffer_get_iter_at_line_index (buffer,
-                                          &start,
-                                          gtk_css_section_get_start_line (section),
-                                          gtk_css_section_get_start_position (section));
-  gtk_text_buffer_get_iter_at_line_index (buffer,
-                                          &end,
-                                          gtk_css_section_get_end_line (section),
-                                          gtk_css_section_get_end_position (section));
+  safe_get_iter_at_line_index (buffer,
+                               &start,
+                               gtk_css_section_get_start_line (section),
+                               gtk_css_section_get_start_position (section));
+  safe_get_iter_at_line_index (buffer,
+                               &end,
+                               gtk_css_section_get_end_line (section),
+                               gtk_css_section_get_end_position (section));
 
   if (g_error_matches (error, GTK_CSS_PROVIDER_ERROR, GTK_CSS_PROVIDER_ERROR_DEPRECATED))
     tag_name = "warning";


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