diff options
author | Iain Lane <iainl@gnome.org> | 2018-07-10 15:48:58 +0100 |
---|---|---|
committer | Iain Lane <iainl@gnome.org> | 2018-07-12 09:25:39 +0100 |
commit | 79cffbd40416ba9f976f426f43295cd37e8dabe1 (patch) | |
tree | 4d77221d70afd4323711a0f89256ac2156992f6e | |
parent | 2aacef39b1cfe4cc5eade704db05ffe1516be22e (diff) | |
download | glib-type-safe-g-clear-pointer-1425.tar.gz |
gmem.h: Use __typeof__() in the g_clear_pointer() macrotype-safe-g-clear-pointer-1425
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
-rw-r--r-- | glib/gmem.h | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/glib/gmem.h b/glib/gmem.h index 5cccb045c..891ded9d2 100644 --- a/glib/gmem.h +++ b/glib/gmem.h @@ -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: |