After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 403823 - g_type_name core dumps instead of returning NULL as stated
g_type_name core dumps instead of returning NULL as stated
Status: RESOLVED WONTFIX
Product: glib
Classification: Platform
Component: gobject
2.12.x
Other Linux
: Normal major
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2007-02-03 04:20 UTC by Lars Rune Nøstdal
Modified: 2007-07-12 16:38 UTC
See Also:
GNOME target: ---
GNOME version: 2.15/2.16



Description Lars Rune Nøstdal 2007-02-03 04:20:26 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.

Thread NaN (LWP 1990)

  • #0 IA__g_type_name
    at gtype.c line 2631
  • #1 main
    at gtk-test.c line 30

*/


hope i'm not missing something .. i'm quite tired at the moment
Comment 1 Matthias Clasen 2007-03-06 06:38:20 UTC
Indeed, pretty bad. 
It segfaults if you pass a value > G_TYPE_FUNDAMENTAL_MAX.
Comment 2 Tim Janik 2007-07-12 16:38:24 UTC
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.