glib: Fix strict-aliasing warnings with g_clear_pointer()

gpointer* cannot be aliased with arbitrary types. In order to fix
-Wstrict-aliasing=2 warnings with the g_clear_pointer() macro, we need
to cast through char*, which is allowed to alias with anything.

Even if we don’t make GLib strict-aliasing safe, it’s important to
ensure this macro is safe, since it could be used from projects which do
compile with -fstrict-aliasing.

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

https://bugzilla.gnome.org/show_bug.cgi?id=791622
This commit is contained in:
Philip Withnall 2017-12-21 17:47:29 +00:00
parent d8fe926ba4
commit 97d24b93ab

View File

@ -113,16 +113,17 @@ gpointer g_try_realloc_n (gpointer mem,
#define g_clear_pointer(pp, destroy) \
G_STMT_START { \
G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \
/* Only one access, please */ \
gpointer *_pp = (gpointer *) (pp); \
/* Only one access, please; work around type aliasing */ \
union { char *in; gpointer *out; } _pp; \
gpointer _p; \
/* This assignment is needed to avoid a gcc warning */ \
GDestroyNotify _destroy = (GDestroyNotify) (destroy); \
\
_p = *_pp; \
_pp.in = (char *) (pp); \
_p = *_pp.out; \
if (_p) \
{ \
*_pp = NULL; \
*_pp.out = NULL; \
_destroy (_p); \
} \
} G_STMT_END