[gtk+/gtk-3-16] cssstyle: Don't crash if properties are added at runtime



commit 334893c6c6a37dc5061377b1c25140b221d04a61
Author: Benjamin Otte <otte redhat com>
Date:   Fri Nov 20 20:54:21 2015 +0100

    cssstyle: Don't crash if properties are added at runtime
    
    Just return the default value for those properties.
    
    Testcase included.
    
    https://bugzilla.redhat.com/show_bug.cgi?id=1281234

 gtk/gtkcssstaticstyle.c      |    9 ++++++---
 testsuite/gtk/stylecontext.c |   24 ++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 3 deletions(-)
---
diff --git a/gtk/gtkcssstaticstyle.c b/gtk/gtkcssstaticstyle.c
index 9b0e3d4..97b4ab3 100644
--- a/gtk/gtkcssstaticstyle.c
+++ b/gtk/gtkcssstaticstyle.c
@@ -46,9 +46,12 @@ gtk_css_static_style_get_value (GtkCssStyle *style,
 {
   GtkCssStaticStyle *sstyle = GTK_CSS_STATIC_STYLE (style);
 
-  if (sstyle->values == NULL ||
-      id >= sstyle->values->len)
-    return NULL;
+  if (G_UNLIKELY (id >= GTK_CSS_PROPERTY_N_PROPERTIES))
+    {
+      GtkCssStyleProperty *prop = _gtk_css_style_property_lookup_by_id (id);
+
+      return _gtk_css_style_property_get_initial_value (prop);
+    }
 
   return g_ptr_array_index (sstyle->values, id);
 }
diff --git a/testsuite/gtk/stylecontext.c b/testsuite/gtk/stylecontext.c
index 9ed3b58..9436217 100644
--- a/testsuite/gtk/stylecontext.c
+++ b/testsuite/gtk/stylecontext.c
@@ -307,6 +307,29 @@ G_GNUC_END_IGNORE_DEPRECATIONS
   gtk_widget_destroy (window);
 }
 
+void
+test_new_css_property (void)
+{
+  GtkWidget *widget;
+  GtkStyleContext *context;
+  GtkBorder padding;
+
+  widget = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+  gtk_widget_realize (widget);
+  context = gtk_widget_get_style_context (widget);
+
+  gtk_style_context_get_padding (context, gtk_style_context_get_state (context), &padding);
+
+G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
+  gtk_style_properties_register_property (NULL,
+                                          g_param_spec_int ("test", "test", "test",
+                                                            0, G_MAXINT, 42, G_PARAM_READWRITE));
+G_GNUC_END_IGNORE_DEPRECATIONS;
+
+  gtk_style_context_add_class (context, "nonexisting");
+  gtk_style_context_get_padding (context, gtk_style_context_get_state (context), &padding);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -318,6 +341,7 @@ main (int argc, char *argv[])
   g_test_add_func ("/style/match", test_match);
   g_test_add_func ("/style/basic", test_basic_properties);
   g_test_add_func ("/style/invalidate-saved", test_invalidate_saved);
+  g_test_add_func ("/style/new-css-property", test_new_css_property);
 
   return g_test_run ();
 }


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