GNOME Bugzilla – Bug 403823
g_type_name core dumps instead of returning NULL as stated
Last modified: 2007-07-12 16:38:24 UTC
/* gcc -std=c99 -Wall -g `pkg-config --libs --cflags gtk+-2.0` gtk-test.c -o gtk-test */ #include <gtk/gtk.h> #include <stdlib.h> #include <time.h> // typedef gulong GType; // from gtype.h int main(int argc, char** argv) { gtk_init(0, 0); g_type_init(); gtk_window_new(0); // just to make sure everything is initialized - probably not required printf("%d %d %d\n", glib_major_version, glib_minor_version, glib_micro_version); srand(time(0)); while(1) { GType rnd = rand(); printf("trying: %lu\n", rnd); // Should not an invalid type id simply return NULL? // ( http://developer.gnome.org/doc/API/2.0/gobject/gobject-Type-Information.html#g-type-name ) char const* window_type_name2 = g_type_name(rnd); printf("window_type_name2: %p\n", window_type_name2); printf("window_type_name2: %s\n", window_type_name2); } return 0; } /* lars@ibmr52:~/programming/c$ ./gtk-test 2 12 4 trying: 991264292 Segmentation fault (core dumped) lars@ibmr52:~/programming/c$ ./gtk-test 2 12 4 trying: 692406285 Segmentation fault (core dumped) lars@ibmr52:~/programming/c$ ./gtk-test 2 12 4 trying: 1156105400 Segmentation fault (core dumped) lars@ibmr52:~/programming/c$ ./gtk-test 2 12 4 trying: 1923580361 Segmentation fault (core dumped) lars@ibmr52:~/programming/c$ ./gtk-test 2 12 4 trying: 1615687476 Segmentation fault (core dumped) lars@ibmr52:~/programming/c$ ./gtk-test 2 12 4 trying: 1314358036 Segmentation fault (core dumped) lars@ibmr52:~/programming/c$ gdb gtk-test GNU gdb 6.4.90-debian Copyright (C) 2006 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i486-linux-gnu"...Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1". (gdb) run Starting program: /home/lars/programming/c/gtk-test [Thread debugging using libthread_db enabled] [New Thread -1218333008 (LWP 1990)] 2 12 4 trying: 958584226 Program received signal SIGSEGV, Segmentation fault.
+ Trace 107572
Thread NaN (LWP 1990)
*/ hope i'm not missing something .. i'm quite tired at the moment
Indeed, pretty bad. It segfaults if you pass a value > G_TYPE_FUNDAMENTAL_MAX.
the documentation for g_type_name() has been fixed now: g_type_name(): Returns the unique name that is assigned to a type ID. Note that this function (like all other GType API) cannot cope with invalid type IDs. %G_TYPE_INVALID may be passed to this function, as may be any other validly registered type ID, but randomized type IDs should not be passed in and will most likely lead to a crash. the reason it used to recommend this function as a test for invalid type IDs is that this particular documentation bit predates the very early libgobject profilings and optimizations. failry short after the GType introduction, the IDs were redesigned to hold pointers internally, and the ability to check for invalid IDs that way was intentionally given up in favour of the speed advantages. at this point, allowing invalid type ID checks would require a global internal hash table (to not enfore checks of O(n_types) complexity). and because of the speed and memory penalty this incurr, i'll not add anything like that unless someone presents a significantly important use case. note that random type *name* checking *does* work. i.e. g_type_from_name (random_string) != 0 is valid and is expected to be the more common check programmers would need to make.