Skip to content

gobject: Fix strict aliasing warnings with g_set_object()

When calling g_set_object() for a type derived from GObject, GCC 9.2 was giving the following strict aliasing warning:

../../source/malcontent/libmalcontent-ui/user-controls.c:1001:21: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
 1001 |   if (g_set_object (&self->user, user))
/opt/gnome/install/include/glib-2.0/gobject/gobject.h:744:33: note: in definition of macro ‘g_set_object’
  744 |   (g_set_object) ((GObject **) (object_ptr), (GObject *) (new_object)) \
      |                                 ^~~~~~~~~~

This was due to the (GObject **) cast.

Pass the pointer through a union to squash this warning. We already do some size and type checks of the dereferenced type, which should catch casual errors. The g_object_ref() and g_object_unref() calls which subsequently happen inside the g_set_object() function also do some dynamic type checks.

Add a test.

Signed-off-by: Philip Withnall withnall@endlessm.com

Merge request reports