GNOME Bugzilla – Bug 339307
cookies aren't necessarily unique
Last modified: 2006-04-21 19:29:16 UTC
There is the teensiest chance that the cookies won't be unique simply by using a random number. Very remote but possible and might cause unreproducable problems. Here's how I've done it in g-s: static guint32 generate_cookie () { guint32 cookie; cookie = (guint32)g_random_int_range (1, G_MAXINT32); return cookie; } static guint32 listener_generate_unique_key (GSListener *listener, int entry_type) { guint32 cookie; GHashTable *hash; hash = get_hash_for_entry_type (listener, entry_type); do { cookie = generate_cookie (); } while (g_hash_table_lookup (hash, &cookie) != NULL); return cookie; } The difference is that I'm using a hash table to store the inhibit data and you use a list.
I think I would rather keep it as a list for now, but I do see your point. Should I just check for the generated number in my existing list after I get the int_range?
That'd work. Just replace g_hash_table_lookup with a g_list_find_custom or something.
Oh, it is probably good to leave zero as a reserved value for the cookie is not set. And why do you only go up to 10240 ? data->cookie = g_random_int_range (0, 10240); I've made the cookie unsigned so it is always positive, reserved zero, and made it go up to G_MAXINT32 which is within the limit of both gint and guint. What do you think?
(In reply to comment #3) > Oh, it is probably good to leave zero as a reserved value for the cookie is not set. And why do you only go up to 10240 ? A random big number :-) > I've made the cookie unsigned so it is always positive, reserved zero, and made it go up to G_MAXINT32 which is within the limit of both gint and guint. What do you think? Sounds good to me. So, general rule of cookie: * not zero * between 1 and G_MAXINT32 * unique I can live with that :-) I'll make the required changes tonight. Thanks.
2006-04-21 Richard Hughes <richard@hughsie.com> * src/gpm-inhibit.{c|h} (gpm_inhibit_generate_cookie): Make sure the cookie we generate is really unique. Fixes bug #339307. (gpm_inhibit_find_cookie, gpm_inhibit_cookie_compare_func): Create these functions so we can easily find data in the list from another cookie.