gmem.h: Use __typeof__() in the g_clear_pointer() macro

Type punning is used on the existing implementation, which hides errors
such as:

  GSList *list = NULL;
  g_clear_pointer (&list, g_error_free);

Let's use __typeof__ to cast the passed-in pointer before it's passed to
the free function so it trips -Wincompatible-pointer-types if it's wrong.

Fixes #1425
This commit is contained in:
Iain Lane 2018-07-10 15:48:58 +01:00
parent 747c2f5720
commit 0da6265939

View File

@ -110,6 +110,16 @@ gpointer g_try_realloc_n (gpointer mem,
gsize n_blocks,
gsize n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) && !defined(__cplusplus) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_58
#define g_clear_pointer(pp, destroy) \
G_STMT_START { \
G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \
__typeof__(*(pp)) _ptr = *(pp); \
*(pp) = NULL; \
if (_ptr) \
destroy (_ptr); \
} G_STMT_END
#else /* __GNUC__ */
#define g_clear_pointer(pp, destroy) \
G_STMT_START { \
G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \
@ -127,6 +137,7 @@ gpointer g_try_realloc_n (gpointer mem,
_destroy (_p); \
} \
} G_STMT_END
#endif /* __GNUC__ */
/**
* g_steal_pointer: