GLib source id wrap around



Folks,

Sorry of this has been discussed before.
It seems to me that source id wrap around is not handled properly.
After looking at the source of g_source attach() it was pretty clear 
to me that return 0 means failure. However source id is allocated
by simply incrementing context->next_id, which is unsigned int and is 
initialized to 1. So eventually it will wrap around and become zero.
Am I missing something here ?

Here is the relevant portion of g_source_attach()
/**
 * ...
 * Return value: the ID for the source within the #GMainContext
 **/
guint
g_source_attach (GSource      *source,
                 GMainContext *context)
{
  guint result = 0;
  GSList *tmp_list;

  g_return_val_if_fail (source->context == NULL, 0);
  g_return_val_if_fail (!SOURCE_DESTROYED (source), 0);
  
  if (!context)
    context = g_main_context_default ();

  LOCK_CONTEXT (context);

  source->context = context;
  result = source->source_id = context->next_id++;

  ...

  return result;
}
(latest CVS. HEAD branch.)

I'd say we need to add something like following to that function.
        /* Handle wrap around. id 0 is invalid */
        if (!context->next_id)
                context->next_id = 1;

--

Also it looks like some glib functions don't bother checking for errors ;-).
For example g_source_new() does
          source = (GSource*) g_malloc0 (struct_size);

          source->source_funcs = source_funcs;
g_malloc0() can fail and return NULL.

Thanks

Max

http://bluez.sf.net
http://vacum.sf.net
http://vtun.sf.net




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]