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



commit ba6934366d01d8713e8eb9ff4c5bdc46561c8a69
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 34563e0..5b5e409 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 f0381bb..971c1cb 100644
--- a/testsuite/gtk/stylecontext.c
+++ b/testsuite/gtk/stylecontext.c
@@ -406,6 +406,29 @@ test_style_classes (void)
   g_object_unref (context);
 }
 
+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[])
 {
@@ -420,6 +443,7 @@ main (int argc, char *argv[])
   g_test_add_func ("/style/set-widget-path-saved", test_set_widget_path_saved);
   g_test_add_func ("/style/widget-path-parent", test_widget_path_parent);
   g_test_add_func ("/style/classes", test_style_classes);
+  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]