mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-01-26 05:56:14 +01:00
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:
parent
d8fe926ba4
commit
97d24b93ab
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user