[gtksourceview/wip/chergert/gsv-gtk4: 155/259] renderer: robustness fixes for gutter renderers




commit 900123caadf1b3e11b2842b5153e0ee2c070452a
Author: Christian Hergert <chergert redhat com>
Date:   Fri Mar 27 14:04:35 2020 -0700

    renderer: robustness fixes for gutter renderers

 gtksourceview/gtksourcegutterrendererlines.c | 27 +++++++++++++++++++++++----
 gtksourceview/gtksourcegutterrenderertext.c  | 20 ++++++++++++++++++++
 2 files changed, 43 insertions(+), 4 deletions(-)
---
diff --git a/gtksourceview/gtksourcegutterrendererlines.c b/gtksourceview/gtksourcegutterrendererlines.c
index 67072c49..b5ef2f24 100644
--- a/gtksourceview/gtksourcegutterrendererlines.c
+++ b/gtksourceview/gtksourcegutterrendererlines.c
@@ -67,13 +67,23 @@ static void
 recalculate_size (GtkSourceGutterRendererLines *renderer)
 {
        GtkSourceBuffer *buffer;
-       gint num_lines;
+       gint num_lines = 1;
        gint num_digits;
 
        buffer = gtk_source_gutter_renderer_get_buffer (GTK_SOURCE_GUTTER_RENDERER (renderer));
-       num_lines = gtk_text_buffer_get_line_count (GTK_TEXT_BUFFER (buffer));
+
+       if (buffer != NULL)
+       {
+               num_lines = gtk_text_buffer_get_line_count (GTK_TEXT_BUFFER (buffer));
+       }
+
        num_digits = count_num_digits (num_lines);
 
+       if (num_digits < 2)
+       {
+               num_digits = 2;
+       }
+
        if (num_digits != renderer->num_line_digits)
        {
                renderer->num_line_digits = num_digits;
@@ -326,12 +336,21 @@ gtk_source_gutter_renderer_lines_measure (GtkWidget      *widget,
        {
                GtkSourceBuffer *buffer;
                gchar markup[32];
-               guint num_lines;
+               guint num_lines = 0;
                gint size;
                gint xpad;
 
                buffer = gtk_source_gutter_renderer_get_buffer (GTK_SOURCE_GUTTER_RENDERER (renderer));
-               num_lines = MAX (99, gtk_text_buffer_get_line_count (GTK_TEXT_BUFFER (buffer)));
+
+               if (buffer != NULL)
+               {
+                       num_lines = gtk_text_buffer_get_line_count (GTK_TEXT_BUFFER (buffer));
+               }
+
+               if (num_lines < 99)
+               {
+                       num_lines = 99;
+               }
 
                g_snprintf (markup, sizeof markup, "<b>%u</b>", num_lines);
                gtk_source_gutter_renderer_text_measure_markup (GTK_SOURCE_GUTTER_RENDERER_TEXT (renderer),
diff --git a/gtksourceview/gtksourcegutterrenderertext.c b/gtksourceview/gtksourcegutterrenderertext.c
index 4aa3eeaa..14c8d8f9 100644
--- a/gtksourceview/gtksourcegutterrenderertext.c
+++ b/gtksourceview/gtksourcegutterrenderertext.c
@@ -141,10 +141,30 @@ measure_text (GtkSourceGutterRendererText *renderer,
        GtkSourceView *view;
        PangoLayout *layout;
 
+       if (width != NULL)
+       {
+               *width = 0;
+       }
+
+       if (height != NULL)
+       {
+               *height = 0;
+       }
+
        view = gtk_source_gutter_renderer_get_view (GTK_SOURCE_GUTTER_RENDERER (renderer));
 
+       if (view == NULL)
+       {
+               return;
+       }
+
        layout = gtk_widget_create_pango_layout (GTK_WIDGET (view), NULL);
 
+       if (layout == NULL)
+       {
+               return;
+       }
+
        if (markup != NULL)
        {
                pango_layout_set_markup (layout, markup, -1);


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