From e356d59a92dfe24ff4a1c5b8300974f0427c90d0 Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Thu, 5 Mar 2020 07:02:24 +0100 Subject: [PATCH] build: Add -Wnull-dereference Sprinkle various g_assert() around the code where gcc cannot figure out on its own that a variable is not NULL and too much refactoring would be needed to make it do that. Also fix usage of g_assert_nonnull(x) to use g_assert(x) because the first is not marked as G_GNUC_NORETURN because of course GTester supports not aborting on aborts. --- gdk/wayland/gdkdevice-wayland.c | 1 + gdk/wayland/gdkdisplay-wayland.c | 4 ++++ gdk/x11/gdkdisplay-x11.c | 6 +++++- gdk/x11/gdksurface-x11.c | 2 +- gsk/gl/gskgldriver.c | 2 +- gtk/gtkaccelmap.c | 1 + gtk/gtkbuilderparser.c | 3 +-- gtk/gtkicontheme.c | 1 + gtk/gtkmenutracker.c | 2 ++ gtk/gtkpathbar.c | 1 + gtk/gtkrbtree.c | 2 ++ gtk/gtkshortcutssection.c | 1 + gtk/gtktextbtree.c | 16 +++++++++------- gtk/gtktextiter.c | 6 ++++-- gtk/gtktreemodelfilter.c | 3 +++ gtk/gtktreemodelsort.c | 2 ++ gtk/gtktreerbtree.c | 4 ++++ meson.build | 1 + 18 files changed, 44 insertions(+), 14 deletions(-) diff --git a/gdk/wayland/gdkdevice-wayland.c b/gdk/wayland/gdkdevice-wayland.c index 09fa321d16..f31ff99045 100644 --- a/gdk/wayland/gdkdevice-wayland.c +++ b/gdk/wayland/gdkdevice-wayland.c @@ -4118,6 +4118,7 @@ tablet_pad_handle_button (void *data, wp_tablet_pad, button, state)); group = tablet_pad_lookup_button_group (pad, button); + g_assert (group != NULL); n_group = g_list_index (pad->mode_groups, group); event = gdk_event_pad_button_new (state == ZWP_TABLET_PAD_V2_BUTTON_STATE_PRESSED diff --git a/gdk/wayland/gdkdisplay-wayland.c b/gdk/wayland/gdkdisplay-wayland.c index ef7b32b95e..76596da719 100644 --- a/gdk/wayland/gdkdisplay-wayland.c +++ b/gdk/wayland/gdkdisplay-wayland.c @@ -1505,15 +1505,19 @@ update_xft_settings (GdkDisplay *display) TranslationEntry *entry; entry = find_translation_entry_by_schema ("org.gnome.settings-daemon.plugins.xsettings", "antialiasing"); + g_assert (entry); antialiasing = entry->fallback.i; entry = find_translation_entry_by_schema ("org.gnome.settings-daemon.plugins.xsettings", "hinting"); + g_assert (entry); hinting = entry->fallback.i; entry = find_translation_entry_by_schema ("org.gnome.settings-daemon.plugins.xsettings", "rgba-order"); + g_assert (entry); order = entry->fallback.i; entry = find_translation_entry_by_schema ("org.gnome.desktop.interface", "text-scaling-factor"); + g_assert (entry); dpi = 96.0 * entry->fallback.i / 65536.0 * 1024; /* Xft wants 1/1024th of an inch */ } else diff --git a/gdk/x11/gdkdisplay-x11.c b/gdk/x11/gdkdisplay-x11.c index be8ddb214a..e5e367eb30 100644 --- a/gdk/x11/gdkdisplay-x11.c +++ b/gdk/x11/gdkdisplay-x11.c @@ -2673,7 +2673,11 @@ gdk_x11_display_error_trap_pop_internal (GdkDisplay *display, break; } - g_return_val_if_fail (trap != NULL, Success); + if (trap == NULL) + { + g_critical ("gdk_x11_display_error_trap_pop() called without gdk_x11_display_error_trap_push()"); + return Success; + } g_assert (trap->end_sequence == 0); /* May need to sync to fill in trap->error_code if we care about diff --git a/gdk/x11/gdksurface-x11.c b/gdk/x11/gdksurface-x11.c index 4a5c52f19c..d25e620b88 100644 --- a/gdk/x11/gdksurface-x11.c +++ b/gdk/x11/gdksurface-x11.c @@ -129,7 +129,7 @@ _gdk_x11_surface_get_toplevel (GdkSurface *surface) { GdkX11Surface *impl; - g_return_val_if_fail (GDK_IS_SURFACE (surface), NULL); + g_assert (GDK_IS_SURFACE (surface)); impl = GDK_X11_SURFACE (surface); diff --git a/gsk/gl/gskgldriver.c b/gsk/gl/gskgldriver.c index cad2007a2a..8cd32d72ab 100644 --- a/gsk/gl/gskgldriver.c +++ b/gsk/gl/gskgldriver.c @@ -658,7 +658,7 @@ gsk_gl_driver_mark_texture_permanent (GskGLDriver *self, { Texture *t = gsk_gl_driver_get_texture (self, texture_id); - g_assert_nonnull (t); + g_assert (t != NULL); t->permanent = TRUE; } diff --git a/gtk/gtkaccelmap.c b/gtk/gtkaccelmap.c index 0dda57104f..2c24d78fa8 100644 --- a/gtk/gtkaccelmap.c +++ b/gtk/gtkaccelmap.c @@ -332,6 +332,7 @@ internal_change_entry (const gchar *accel_path, { gtk_accel_map_add_entry (accel_path, 0, 0); entry = accel_path_lookup (accel_path); + g_assert (entry); entry->accel_key = accel_key; entry->accel_mods = accel_mods; entry->changed = TRUE; diff --git a/gtk/gtkbuilderparser.c b/gtk/gtkbuilderparser.c index 5440100b7f..1301e778e2 100644 --- a/gtk/gtkbuilderparser.c +++ b/gtk/gtkbuilderparser.c @@ -382,8 +382,7 @@ state_pop (ParserData *data) { gpointer old = NULL; - if (!data->stack) - return NULL; + g_assert (data->stack); old = data->stack->data; data->stack = g_slist_delete_link (data->stack, data->stack); diff --git a/gtk/gtkicontheme.c b/gtk/gtkicontheme.c index 3fe8326123..9e7567fc77 100644 --- a/gtk/gtkicontheme.c +++ b/gtk/gtkicontheme.c @@ -4082,6 +4082,7 @@ gtk_icon_theme_lookup_by_gicon (GtkIconTheme *self, /* We can't render emblemed icons atm, but at least render the base */ while (G_IS_EMBLEMED_ICON (gicon)) gicon = g_emblemed_icon_get_icon (G_EMBLEMED_ICON (gicon)); + g_assert (gicon); /* shut up gcc -Wnull-dereference */ if (GDK_IS_PIXBUF (gicon)) { diff --git a/gtk/gtkmenutracker.c b/gtk/gtkmenutracker.c index 6ec6c5ab07..c9b59d183b 100644 --- a/gtk/gtkmenutracker.c +++ b/gtk/gtkmenutracker.c @@ -234,6 +234,7 @@ gtk_menu_tracker_item_visibility_changed (GtkMenuTrackerItem *item, /* remember: the item is our model */ section = gtk_menu_tracker_section_find_model (tracker->toplevel, item, &offset); + g_assert (section); was_visible = section->items != NULL; @@ -424,6 +425,7 @@ gtk_menu_tracker_model_changed (GMenuModel *model, * position of that section within the overall menu. */ section = gtk_menu_tracker_section_find_model (tracker->toplevel, model, &offset); + g_assert (section); /* Next, seek through that section to the change point. This gives us * the correct GSList** to make the change to and also finds the final diff --git a/gtk/gtkpathbar.c b/gtk/gtkpathbar.c index c9337b23f9..b257e1b4d1 100644 --- a/gtk/gtkpathbar.c +++ b/gtk/gtkpathbar.c @@ -690,6 +690,7 @@ gtk_path_bar_scroll_down (GtkPathBar *path_bar) break; } } + g_assert (down_button); gtk_widget_get_allocation (GTK_WIDGET (path_bar), &allocation); gtk_widget_get_allocation (BUTTON_DATA (down_button->data)->button, &button_allocation); diff --git a/gtk/gtkrbtree.c b/gtk/gtkrbtree.c index 79bb9b5911..4ccff54084 100644 --- a/gtk/gtkrbtree.c +++ b/gtk/gtkrbtree.c @@ -441,6 +441,7 @@ gtk_rb_tree_remove_node_fixup (GtkRbTree *tree, gtk_rb_node_rotate_left (tree, p); w = p->right; } + g_assert (w); if (is_black (w->left) && is_black (w->right)) { set_red (w); @@ -472,6 +473,7 @@ gtk_rb_tree_remove_node_fixup (GtkRbTree *tree, gtk_rb_node_rotate_right (tree, p); w = p->left; } + g_assert (w); if (is_black (w->right) && is_black (w->left)) { set_red (w); diff --git a/gtk/gtkshortcutssection.c b/gtk/gtkshortcutssection.c index 5269ae86a6..6a95e348a1 100644 --- a/gtk/gtkshortcutssection.c +++ b/gtk/gtkshortcutssection.c @@ -693,6 +693,7 @@ gtk_shortcuts_section_reflow_groups (GtkShortcutsSection *self) n += height; } + g_assert (g); for (g = g->next; g; g = g->next) { GtkShortcutsGroup *group = g->data; diff --git a/gtk/gtktextbtree.c b/gtk/gtktextbtree.c index 4a0de2a141..1ab7878539 100644 --- a/gtk/gtktextbtree.c +++ b/gtk/gtktextbtree.c @@ -2849,8 +2849,9 @@ _gtk_text_btree_set_mark (GtkTextBTree *tree, seg = real_set_mark (tree, existing_mark, name, left_gravity, iter, should_exist, TRUE); + g_assert (seg); - return seg ? seg->body.mark.obj : NULL; + return seg->body.mark.obj; } gboolean @@ -3126,7 +3127,7 @@ _gtk_text_btree_last_could_contain_tag (GtkTextBTree *tree, { info = gtk_text_btree_get_existing_tag_info (tree, tag); - if (info->tag_root == NULL) + if (info == NULL || info->tag_root == NULL) return NULL; node = info->tag_root; @@ -3135,7 +3136,6 @@ _gtk_text_btree_last_could_contain_tag (GtkTextBTree *tree, while (node->level > 0) { - g_assert (node != NULL); /* Failure probably means bad tag summaries. */ last_node = NULL; node = node->children.node; while (node != NULL) @@ -3146,6 +3146,7 @@ _gtk_text_btree_last_could_contain_tag (GtkTextBTree *tree, } node = last_node; + g_assert (node != NULL); /* Failure probably means bad tag summaries. */ } g_assert (node != NULL); /* The tag summaries said some node had @@ -3450,6 +3451,7 @@ ensure_end_iter_segment (GtkTextBTree *tree) last_with_chars = seg; seg = seg->next; } + g_assert (last_with_chars); tree->end_iter_segment = last_with_chars; @@ -4455,10 +4457,6 @@ _gtk_text_line_next_could_contain_tag (GtkTextLine *line, while (node->level > 0) { - g_assert (node != NULL); /* If this fails, it likely means an - incorrect tag summary led us on a - wild goose chase down this branch of - the tree. */ node = node->children.node; while (node != NULL) { @@ -4466,6 +4464,10 @@ _gtk_text_line_next_could_contain_tag (GtkTextLine *line, break; node = node->next; } + g_assert (node != NULL); /* If this fails, it likely means an + incorrect tag summary led us on a + wild goose chase down this branch of + the tree. */ } g_assert (node != NULL); diff --git a/gtk/gtktextiter.c b/gtk/gtktextiter.c index c4c1bfc024..237030c5f9 100644 --- a/gtk/gtktextiter.c +++ b/gtk/gtktextiter.c @@ -209,6 +209,8 @@ gtk_text_iter_make_real (const GtkTextIter *_iter) GtkTextRealIter *iter; iter = gtk_text_iter_make_surreal (_iter); + if (iter == NULL) + return NULL; if (iter->segments_changed_stamp != _gtk_text_btree_get_segments_changed_stamp (iter->tree)) @@ -242,8 +244,8 @@ iter_init_common (GtkTextIter *_iter, { GtkTextRealIter *iter = (GtkTextRealIter*)_iter; - g_return_val_if_fail (iter != NULL, NULL); - g_return_val_if_fail (tree != NULL, NULL); + g_assert (iter != NULL); + g_assert (tree != NULL); memset (iter, 0, sizeof (GtkTextRealIter)); diff --git a/gtk/gtktreemodelfilter.c b/gtk/gtktreemodelfilter.c index 86d575f8cf..25fe8c700c 100644 --- a/gtk/gtktreemodelfilter.c +++ b/gtk/gtktreemodelfilter.c @@ -3002,6 +3002,7 @@ gtk_tree_model_filter_get_iter_full (GtkTreeModel *model, } elt = GET_ELT (siter); + g_assert (elt); if (!elt->children) gtk_tree_model_filter_build_level (filter, level, elt, FALSE); @@ -3072,6 +3073,7 @@ gtk_tree_model_filter_get_iter (GtkTreeModel *model, } elt = GET_ELT (siter); + g_assert (elt); if (!elt->children) gtk_tree_model_filter_build_level (filter, level, elt, FALSE); level = elt->children; @@ -4192,6 +4194,7 @@ gtk_tree_model_filter_convert_path_to_child_path (GtkTreeModelFilter *filter, } elt = GET_ELT (siter); + g_assert (elt); if (elt->children == NULL) gtk_tree_model_filter_build_level (filter, level, elt, FALSE); diff --git a/gtk/gtktreemodelsort.c b/gtk/gtktreemodelsort.c index 5f793b547d..49a1ac784f 100644 --- a/gtk/gtktreemodelsort.c +++ b/gtk/gtktreemodelsort.c @@ -1285,6 +1285,7 @@ gtk_tree_model_sort_get_iter (GtkTreeModel *tree_model, } elt = GET_ELT (siter); + g_assert (elt); if (elt->children == NULL) gtk_tree_model_sort_build_level (tree_model_sort, level, elt); @@ -2395,6 +2396,7 @@ gtk_tree_model_sort_convert_path_to_child_path (GtkTreeModelSort *tree_model_sor } elt = GET_ELT (siter); + g_assert (elt); if (elt->children == NULL) gtk_tree_model_sort_build_level (tree_model_sort, level, elt); diff --git a/gtk/gtktreerbtree.c b/gtk/gtktreerbtree.c index 3331d0a5cc..7552fcc61a 100644 --- a/gtk/gtktreerbtree.c +++ b/gtk/gtktreerbtree.c @@ -269,6 +269,7 @@ gtk_tree_rbtree_remove_node_fixup (GtkTreeRBTree *tree, gtk_tree_rbnode_rotate_left (tree, parent); w = parent->right; } + g_assert (w); if (GTK_TREE_RBNODE_GET_COLOR (w->left) == GTK_TREE_RBNODE_BLACK && GTK_TREE_RBNODE_GET_COLOR (w->right) == GTK_TREE_RBNODE_BLACK) { GTK_TREE_RBNODE_SET_COLOR (w, GTK_TREE_RBNODE_RED); @@ -300,6 +301,7 @@ gtk_tree_rbtree_remove_node_fixup (GtkTreeRBTree *tree, gtk_tree_rbnode_rotate_right (tree, parent); w = parent->left; } + g_assert (w); if (GTK_TREE_RBNODE_GET_COLOR (w->right) == GTK_TREE_RBNODE_BLACK && GTK_TREE_RBNODE_GET_COLOR (w->left) == GTK_TREE_RBNODE_BLACK) { GTK_TREE_RBNODE_SET_COLOR (w, GTK_TREE_RBNODE_RED); @@ -1595,6 +1597,8 @@ gtk_tree_rbtree_test_dirty (GtkTreeRBTree *tree, GtkTreeRBNode *node, gint expected_dirtyness) { + g_assert (node); + if (expected_dirtyness) { g_assert (GTK_TREE_RBNODE_FLAG_SET (node, GTK_TREE_RBNODE_COLUMN_INVALID) || diff --git a/meson.build b/meson.build index dea88448b7..6fd7d68b45 100644 --- a/meson.build +++ b/meson.build @@ -251,6 +251,7 @@ elif cc.get_id() == 'gcc' or cc.get_id() == 'clang' '-Wmissing-include-dirs', '-Wmissing-noreturn', '-Wnested-externs', + '-Wnull-dereference', '-Wold-style-definition', '-Wpointer-arith', '-Wshadow',