Commit 0da62659 authored by Iain Lane's avatar Iain Lane

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
parent 747c2f57
......@@ -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:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment