GNOME Bugzilla – Bug 615379
g_new macros crash if sizeof(struct_type) == 0
Last modified: 2012-01-10 15:28:27 UTC
The new version of the g_new family of macros will crash with a floating point exception if sizeof(struct_type) == 0. Previous versions would safely return a NULL.
Isn't a struct type with size zero an impossible non-standard abomination anyway? (And when I say non-standard, I mean non-C89.)
I cannot cite whether or not it violates standard, but it is possible with GCC. You may end up with an empty structure because all your fields happen to be #ifdef'ed out. In my case, I followed a common pattern for my objects whether they had private data or not. Neither may be a good idea, but the problem is that code that worked before now crashes.
diff --git a/glib/gmem.h b/glib/gmem.h index 2fef766..54f153b 100644 --- a/glib/gmem.h +++ b/glib/gmem.h @@ -88,7 +88,7 @@ gpointer g_try_realloc_n (gpointer mem, if (__s == 1) \ __p = g_##func (__n); \ else if (__builtin_constant_p (__n) && \ - __n <= G_MAXSIZE / __s) \ + (__s == 0 || __n <= G_MAXSIZE / __s)) \ __p = g_##func (__n * __s); \ else \ __p = g_##func##_n (__n, __s); \ @@ -102,7 +102,7 @@ gpointer g_try_realloc_n (gpointer mem, if (__s == 1) \ __p = g_##func (__p, __n); \ else if (__builtin_constant_p (__n) && \ - __n <= G_MAXSIZE / __s) \ + (__s == 0 || __n <= G_MAXSIZE / __s)) \ __p = g_##func (__p, __n * __s); \ else \ __p = g_##func##_n (__p, __n, __s); \
Committed to master.
Comment 1 seems to preempt Bug 641350