[gnome-builder] style: various defensive cleanups



commit 280c51cc8bc6ee62538f35b6286552de4a5c748a
Author: Christian Hergert <chergert redhat com>
Date:   Sun Dec 17 01:54:20 2017 -0800

    style: various defensive cleanups
    
    This does some cleanup to clear variables when we free them to
    be less likely to have an invalid pointer use after free.

 src/gstyle/gstyle-color-scale.c                   |   14 ++++++--------
 src/gstyle/gstyle-color-widget.c                  |    7 ++++---
 src/gstyle/gstyle-css-provider.c                  |    9 ---------
 src/gstyle/gstyle-palette.c                       |   11 +++++------
 src/gstyle/gstyle-rename-popover.c                |    7 -------
 src/gstyle/gstyle-revealer.c                      |    9 ---------
 src/libide/debugger/ide-debugger-frame.c          |    2 ++
 src/libide/documentation/ide-documentation-info.c |    5 +++--
 src/libide/documentation/ide-documentation.c      |    4 ++--
 src/libide/highlighting/ide-highlight-index.c     |    8 ++++----
 src/libide/layout/ide-layout-grid-column.c        |    2 ++
 11 files changed, 28 insertions(+), 50 deletions(-)
---
diff --git a/src/gstyle/gstyle-color-scale.c b/src/gstyle/gstyle-color-scale.c
index 1f1f31d..dd78d0e 100644
--- a/src/gstyle/gstyle-color-scale.c
+++ b/src/gstyle/gstyle-color-scale.c
@@ -607,17 +607,15 @@ gstyle_color_scale_finalize (GObject *object)
 
   g_clear_object (&self->long_press_gesture);
   g_clear_object (&self->default_provider);
-  g_sequence_free (self->custom_color_stops);
 
-  cairo_pattern_destroy (self->checkered_pattern);
-  if (self->pattern != NULL)
-    cairo_pattern_destroy (self->pattern);
+  g_clear_pointer (&self->custom_color_stops, g_sequence_free);
+  g_clear_pointer (&self->checkered_pattern, cairo_pattern_destroy);
+  g_clear_pointer (&self->pattern, cairo_pattern_destroy);
+  g_clear_pointer (&self->data_surface, cairo_pattern_destroy);
 
-  if (self->data_surface != NULL)
-    cairo_surface_destroy (self->data_surface);
+  g_clear_pointer (&self->data_raw, g_free);
+  g_clear_pointer (&self->data_raw_filtered, g_free);
 
-  g_free (self->data_raw);
-  g_free (self->data_raw_filtered);
 }
 
 static void
diff --git a/src/gstyle/gstyle-color-widget.c b/src/gstyle/gstyle-color-widget.c
index fcce3e8..ec6d236 100644
--- a/src/gstyle/gstyle-color-widget.c
+++ b/src/gstyle/gstyle-color-widget.c
@@ -1340,7 +1340,7 @@ gstyle_color_widget_finalize (GObject *object)
 {
   GstyleColorWidget *self = GSTYLE_COLOR_WIDGET (object);
 
-  if (self->color)
+  if (self->color != NULL)
     gstyle_color_widget_disconnect_color (self);
 
   g_clear_object (&self->multipress_gesture);
@@ -1350,8 +1350,9 @@ gstyle_color_widget_finalize (GObject *object)
   g_clear_object (&self->color);
   g_clear_object (&self->filtered_color);
   g_clear_object (&self->default_provider);
-  cairo_pattern_destroy (self->checkered_pattern);
-  gtk_target_list_unref (self->target_list);
+
+  g_clear_pointer (&self->checkered_pattern, cairo_pattern_destroy);
+  g_clear_pointer (&self->target_list, gtk_target_list_unref);
 
   G_OBJECT_CLASS (gstyle_color_widget_parent_class)->finalize (object);
 }
diff --git a/src/gstyle/gstyle-css-provider.c b/src/gstyle/gstyle-css-provider.c
index 7c74de3..2994ed5 100644
--- a/src/gstyle/gstyle-css-provider.c
+++ b/src/gstyle/gstyle-css-provider.c
@@ -103,14 +103,6 @@ gstyle_css_provider_new (void)
 }
 
 static void
-gstyle_css_provider_finalize (GObject *object)
-{
-  /* GstyleCssProvider *self = (GstyleCssProvider *)object; */
-
-  G_OBJECT_CLASS (gstyle_css_provider_parent_class)->finalize (object);
-}
-
-static void
 gstyle_css_provider_get_property (GObject    *object,
                                   guint       prop_id,
                                   GValue     *value,
@@ -145,7 +137,6 @@ gstyle_css_provider_class_init (GstyleCssProviderClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
-  object_class->finalize = gstyle_css_provider_finalize;
   object_class->get_property = gstyle_css_provider_get_property;
   object_class->set_property = gstyle_css_provider_set_property;
 }
diff --git a/src/gstyle/gstyle-palette.c b/src/gstyle/gstyle-palette.c
index 9c0cfb1..e8de101 100644
--- a/src/gstyle/gstyle-palette.c
+++ b/src/gstyle/gstyle-palette.c
@@ -1105,12 +1105,11 @@ gstyle_palette_finalize (GObject *object)
 {
   GstylePalette *self = GSTYLE_PALETTE (object);
 
-  g_ptr_array_unref (self->colors);
-  g_hash_table_unref (self->color_names);
-
-  g_free (self->name);
-  g_free (self->id);
-  g_free (self->gettext_domain);
+  g_clear_pointer (&self->colors, g_ptr_array_unref);
+  g_clear_pointer (&self->color_names, g_hash_table_unref);
+  g_clear_pointer (&self->name, g_free);
+  g_clear_pointer (&self->id, g_free);
+  g_clear_pointer (&self->gettext_domain, g_free);
   g_clear_object (&self->file);
 
   G_OBJECT_CLASS (gstyle_palette_parent_class)->finalize (object);
diff --git a/src/gstyle/gstyle-rename-popover.c b/src/gstyle/gstyle-rename-popover.c
index 89c235f..fc98745 100644
--- a/src/gstyle/gstyle-rename-popover.c
+++ b/src/gstyle/gstyle-rename-popover.c
@@ -207,12 +207,6 @@ gstyle_rename_popover_new (void)
 }
 
 static void
-gstyle_rename_popover_finalize (GObject *object)
-{
-  G_OBJECT_CLASS (gstyle_rename_popover_parent_class)->finalize (object);
-}
-
-static void
 gstyle_rename_popover_get_property (GObject    *object,
                                     guint       prop_id,
                                     GValue     *value,
@@ -272,7 +266,6 @@ gstyle_rename_popover_class_init (GstyleRenamePopoverClass *klass)
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
 
-  object_class->finalize = gstyle_rename_popover_finalize;
   object_class->get_property = gstyle_rename_popover_get_property;
   object_class->set_property = gstyle_rename_popover_set_property;
 
diff --git a/src/gstyle/gstyle-revealer.c b/src/gstyle/gstyle-revealer.c
index 54e1e54..b69fc0d 100644
--- a/src/gstyle/gstyle-revealer.c
+++ b/src/gstyle/gstyle-revealer.c
@@ -222,12 +222,6 @@ gstyle_revealer_new (void)
 }
 
 static void
-gstyle_revealer_finalize (GObject *object)
-{
-  G_OBJECT_CLASS (gstyle_revealer_parent_class)->finalize (object);
-}
-
-static void
 gstyle_revealer_get_preferred_width (GtkWidget *widget,
                                      gint      *min_width,
                                      gint      *nat_width)
@@ -316,12 +310,9 @@ gstyle_revealer_add (GtkContainer *container,
 static void
 gstyle_revealer_class_init (GstyleRevealerClass *klass)
 {
-  GObjectClass *object_class = G_OBJECT_CLASS (klass);
   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
 
-  object_class->finalize = gstyle_revealer_finalize;
-
   container_class->add = gstyle_revealer_add;
 
   widget_class->size_allocate = gstyle_revealer_size_allocate;
diff --git a/src/libide/debugger/ide-debugger-frame.c b/src/libide/debugger/ide-debugger-frame.c
index e51f0fd..7e75c41 100644
--- a/src/libide/debugger/ide-debugger-frame.c
+++ b/src/libide/debugger/ide-debugger-frame.c
@@ -16,6 +16,8 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#define G_LOG_DOMAIN "ide-debugger-frame"
+
 #include "debugger/ide-debugger-frame.h"
 
 typedef struct
diff --git a/src/libide/documentation/ide-documentation-info.c 
b/src/libide/documentation/ide-documentation-info.c
index 8d686a1..d066c81 100644
--- a/src/libide/documentation/ide-documentation-info.c
+++ b/src/libide/documentation/ide-documentation-info.c
@@ -23,11 +23,12 @@
 
 struct _IdeDocumentationInfo
 {
-  GObject parent_instance;
+  GObject                  parent_instance;
 
   gchar                   *input;
-  IdeDocumentationContext  context;
   GPtrArray               *proposals;
+
+  IdeDocumentationContext  context;
 };
 
 G_DEFINE_TYPE (IdeDocumentationInfo, ide_documentation_info, G_TYPE_OBJECT)
diff --git a/src/libide/documentation/ide-documentation.c b/src/libide/documentation/ide-documentation.c
index 74127f9..e21c660 100644
--- a/src/libide/documentation/ide-documentation.c
+++ b/src/libide/documentation/ide-documentation.c
@@ -25,8 +25,8 @@
 
 struct _IdeDocumentation
 {
-  GObject                  parent_instance;
-  PeasExtensionSet        *extensions;
+  GObject           parent_instance;
+  PeasExtensionSet *extensions;
 };
 
 G_DEFINE_TYPE (IdeDocumentation, ide_documentation, IDE_TYPE_OBJECT)
diff --git a/src/libide/highlighting/ide-highlight-index.c b/src/libide/highlighting/ide-highlight-index.c
index 56b7519..68137ea 100644
--- a/src/libide/highlighting/ide-highlight-index.c
+++ b/src/libide/highlighting/ide-highlight-index.c
@@ -48,7 +48,7 @@ ide_highlight_index_new (void)
 {
   IdeHighlightIndex *ret;
 
-  ret = g_new0 (IdeHighlightIndex, 1);
+  ret = g_slice_new0 (IdeHighlightIndex);
   ret->ref_count = 1;
   ret->strings = g_string_chunk_new (ide_get_system_page_size ());
   ret->index = g_hash_table_new (g_str_hash, g_str_equal);
@@ -117,9 +117,9 @@ ide_highlight_index_finalize (IdeHighlightIndex *self)
 {
   IDE_ENTRY;
 
-  g_string_chunk_free (self->strings);
-  g_hash_table_unref (self->index);
-  g_free (self);
+  g_clear_pointer (&self->strings, g_string_chunk_free);
+  g_clear_pointer (&self->index, g_hash_table_unref);
+  g_slice_free (IdeHighlightIndex, self);
 
   DZL_COUNTER_DEC (instances);
 
diff --git a/src/libide/layout/ide-layout-grid-column.c b/src/libide/layout/ide-layout-grid-column.c
index 9fff7fb..c32cb30 100644
--- a/src/libide/layout/ide-layout-grid-column.c
+++ b/src/libide/layout/ide-layout-grid-column.c
@@ -116,11 +116,13 @@ ide_layout_grid_column_remove (GtkContainer *container,
 static void
 ide_layout_grid_column_finalize (GObject *object)
 {
+#ifndef G_DISABLE_ASSERT
   IdeLayoutGridColumn *self = (IdeLayoutGridColumn *)object;
 
   g_assert (self->focus_stack.head == NULL);
   g_assert (self->focus_stack.tail == NULL);
   g_assert (self->focus_stack.length == 0);
+#endif
 
   G_OBJECT_CLASS (ide_layout_grid_column_parent_class)->finalize (object);
 }


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